【问题标题】:displays the data array with foreach on mysql在 mysql 上使用 foreach 显示数据数组
【发布时间】:2014-05-14 05:23:01
【问题描述】:

如何以foreach的方式显示存储在mysql中的数据? 我想使用 foreach 显示来自 mysql darabase 的数据。 我试过作为下面的 php 文件,但不是我需要的。

tbl1

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

文件.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);
?>

这个例子涉及到一个foreach但是没有用到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);
?>

如何以foreach的方式显示mysql中存储的数据?

为此

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

【问题讨论】:

  • 为什么不在while 中使用&lt;tr&gt; 并将其显示在其中?为什么json_encode呢?还是你想要json格式的输出?
  • 但是对于这个私有静态最终字符串 URL_ALBUMS = "10.0.2.2/androidr/tes.php";; // 所有 JSON 节点名称 private static final String TAG_ID = "id";私有静态最终字符串 TAG_NAME = "名称";私有静态最终字符串 TAG_SONGS_COUNT = "type_count";

标签: php android mysql arrays foreach


【解决方案1】:

先填充一个数组?

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
}

顺便说一句,看起来您正在自己拼凑一个 JSON 字符串,然后 json_encode'ing 之后...只需创建数组,然后让 json_encode 进行编码。 IE。 json_encode($rs);

【讨论】:

  • 但是对于那个 private static final String URL_ALBUMS = "10.0.2.2/androidr/tes.php"; // 所有 JSON 节点名称 private static final String TAG_ID = "id";私有静态最终字符串 TAG_NAME = "名称";私有静态最终字符串 TAG_SONGS_COUNT = "type_count";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
相关资源
最近更新 更多