doucheng1891 2016-02-19 06:19
浏览 87

使用来自php json encode的jquery从两个数组中获取数据

I am newbie in this thing, i have searched on internet but not work for me, maybe someone can give me solution to get data from two array from json encode

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

and this is my html code

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.');
            }
        });                 

Thank you before, Have a nice day :)

  • 写回答

2条回答 默认 最新

  • dongtao4890 2016-02-19 06:42
    关注

    The json data you send from the php is $final = array('array1'=>$viewEO, 'array2'=>$viewAcara); your looping is wrong.

    This can be done in many idea what I am suggesting is, try to use two new loops with in ajax success

    $.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);
    }
    

    and

    $.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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站