使用来自php json encode

时间:2016-02-19 06:19:55

标签: php jquery arrays json ajax

我是这个东西的新手,我在互联网上搜索但不能为我工作,也许有人可以给我解决方案从json编码中获取两个数组的数据

<?php
session_start();
include "config.php";

$viewEO;
$viewAcara;

$ideve=mysql_real_escape_string($_GET["id"]);
$mysql = ("SELECT id_eo,nama as namaEO,logo,deskripsi as DeskEO,email,telp from eventorg WHERE id_eo='$ideve'");
$result=mysql_query($mysql);
if (!empty($result))
{       
  while ($row=mysql_fetch_array($result))
  {
     $viewEO[] = array(
     'idEO' => $row['id_eo'],
     'namaeo' => $row['namaEO'],
     'deskripsieo' => $row['DeskEO'],
     'email' => $row['email'],
     'telpon' => $row['telp'],
     'logoeo' => $row['Logo']
     );
  }
}

$mysql2 = ("SELECT id_acara,nama,tanggal,endtanggal,lokasi,imagePath,deskripsi,id_eo from acara WHERE id_eo='$ideve'");
$result2=mysql_query($mysql2);
if (!empty($result2))
{       
  while ($row2=mysql_fetch_array($result2))
  {
    $viewAcara[] = array(
    'idacara' => $row2['id_acara'],
    'namaacara' => $row2['nama'],
    'deskripsi' => $row2['deskripsi'],
    'tanggal' => $row2['tanggal']
    );
  }
}

mysql_close($con);

$final = array('array1'=>$viewEO, 'array2'=>$viewAcara);
header('Content-Type: application/json');
echo json_encode($final);
?>

这是我的HTML代码

var arrEOS=new Array();

        $.ajax({
            url: 'phpmobile/viewdetaileo.php',
            data: { "id": tempideo},
            dataType: 'json',
            success: function(data){
                $.each(data, function(i,item){ 
                    if (arrEOS.indexOf(item.ideo)<0)
                    {   
                        $('#daftaracara').append('<li data-role="list-divider" >'+item.tanggal+'</li><li><a onclick="detailAcara('+item.idacara+')"><h2>'+item.nama+'</h2><p><strong>'+item.deskripsi+'</strong></p><p>'+item.lokasi+'</p></a></li>');  
                        arrEOS.push(item.idacara);      
                    }
                    $('ul').listview('refresh');


                    $("#img1").html('<img id="img1" src="web/'+item.logoeo+'">');
                    $("#namaeo").html(item.namaeo);
                    $("#deskEO").html(item.deskripsieo);
                    $("#telpon").html(item.telpeo);
                    $("#email").html(item.emaileo); 

                });

            },
            error: function(){
                //output.text('There was an error loading the data.');
            }
        });                 

谢谢你,祝你有愉快的一天:)

2 个答案:

答案 0 :(得分:0)

你从php发送的json数据是$final = array('array1'=>$viewEO, 'array2'=>$viewAcara);你的循环错误。

这可以在很多想法中完成我的建议,尝试在ajax成功中使用两个新循环

$.each(data.array1, function(i,item){ 

//do the operations you want to perform with these vaules
    console.log(item.idacara);
    console.log(item.namaacara);
    console.log(item.deskripsi);
    console.log(item.tanggal);
}

$.each(data.array2, function(i,item){ 
//do the operations you want to perform with these vaules
    console.log(item.logoeo);
    console.log(item.idEO);
    console.log(item.namaeo);
    console.log(item.deskripsieo);
    console.log(item.email);
}

答案 1 :(得分:0)

如果你只有两个数组,请尝试这个,

 $.each(data, function(i,item){
                if(i == "array1"){
                //for array1
                    console.log(item[0].idEo);
                }
                else{
                //for array2
                    console.log(item[0].idacara)
                }