Вопрос

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";
Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top