Вопрос

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 tes2.php file below, but not as necessary as I need.

trans.java

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

tes.php // succsess, but not use mysql :(

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

tes2.php //not succsess :(

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

Решение

<?php
include("connect.php");
$q = mysql_query("SELECT * FROM tbl1");
$trans = array();
while($r=mysql_fetch_array($q))
{

        $trans[] = $r;

}

    echo json_encode($trans);
?>

If you want associative array just change this mysql_fetch_array($q, MYSQL_ASSOC) i while .

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