duanquan1207 2014-05-14 05:23
浏览 57
已采纳

在mysql上显示带有foreach的数据数组

how to display data stored in mysql in a way foreach? I want to display data from mysql darabase using foreach. I've tried as a php file below, but not as necessary as I need.

tbl1

+-----+----------+--------+
|*id  |   name   |  type  |
+=====+==========+========+
|  1  |   car    |   a    |
+-----+----------+--------+
|  2  | motorcly |   a    |
+-----+----------+--------+
|  3  |   Bus    |   1    |
+-----+----------+--------+
|  4  |   others |   1    |
+-----+----------+--------+

file.php

<?php
include("connect.php");
$q = mysql_query("SELECT * FROM tbl1");
$trans = '[';
while($r=mysql_fetch_array($q))
{
    if(strlen($v)<15)
    {
        $trans .= '{ 
            "id" :"'.($r['id']).'",
            "name" : "'.$r['name'].'",
            "type_count" : "'.$r['type'].'"
            }';
    }
    else
    {
        $trans .= '{ 
            "id" :"'.($r['id']).'",
            "name" : "'.$r['name'].'",
            "type_count" : "'.$r['type'].'"
            }';

    }
}
$trans .= ']';
    echo json_encode($trans);
?>

This example involves a foreach but does not use mysql

tes.php

<?php

include_once './data.php'; //"with data.php????" :( not use mysql
$trans = array();

foreach ($tran_z as $trans) {
    $tmp = array();
    $tmp["id"] = $album["id"];
    $tmp["name"] = $album["name"];
    $tmp["type_count"] = count($trans["type"]);

    array_push($trans, $tmp);
}

echo json_encode($trans);
?>

how to display data stored in mysql in a way foreach?

for this

package com.trans.baru;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MenuActivity extends ListActivity {
    ConnectionDetector cd;  
    AlertDialogManager alert = new AlertDialogManager();    
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> submenu;
    JSONArray menu = null;
    private static final String URL_ALBUMS = "http://10.0.2.2/android/tes.php";

    // ALL JSON node names
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_TYPE_COUNT = "type_count";
  • 写回答

1条回答 默认 最新

  • droc60607 2014-05-14 05:29
    关注

    Just populate an array first?

    include("connect.php");
    $q = mysql_query("SELECT * FROM tbl1");
    $rs = array();
    while($r=mysql_fetch_array($q)) {
        $rs[] = $r;
    }
    foreach ($rs as $r) {
        //do stuff
    }
    

    By the way it looks like you're piecing together a JSON string by yourself, and then json_encode'ing it afterwards... Just make the array and then let json_encode do the encoding. Ie. json_encode($rs);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条