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);

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料