dtmm0148603 2011-10-05 13:21
浏览 44
已采纳

PHP / Jquery的JSON错误

I am not sure how I do this, But baiscly what I want is to show an error message if the user does not have any friends. asking them to add some friends.

The PHP part of this code is returning [false] instead of the error message when it cant find any results in the first mysql.

Here is my jquery code

function fetchfriends(){
$.getJSON('sys/classes/core.php?task=fetchfriends&userid='+sessionparts[1], function(data) {



$.each(data, function(key, val) {
    if(val.error)
    {
    $('<div class="myfriendsbar">'+val.error+'</div>').appendTo('#mycontacts');
    return false;
    }
    else
    {

    $('<div class="myfriendsbar"><div id="friendsimage'+val.to_userid+'" style="width:32px; height:32px; float:left; margin:3px;"></div>'+val.firstname+' '+val.lastname+'<br/>'+val.username+'</div>').appendTo('#mycontacts');

    $('#friendsimage'+val.to_userid).css("background-image", "url(http://bonush.com/beta/sys/classes/photofetch.php?pic="+val.to_userid+":9177156176671)"); 

    }
  });


    });

and this is the PHP code

$number_check = mysql_query("SELECT * FROM user_contact WHERE from_userid = '{$this->userid}'");

    if(!mysql_num_rows($number_check))
    {

            $arr = array('error' => "No Friends?<br/>Search above for New users or invite some friends.");
            print json_encode($arr);
    }
    else
    {
            $friendlook = mysql_query("%PRIVATECODE%");

            $row = mysql_fetch_assoc($friendlook);
            $rows[] = $row;
            print json_encode($rows);
    }
  • 写回答

2条回答 默认 最新

  • doupai6875 2011-10-05 13:34
    关注

    the way i see it you don't need the each on your return data if there is an error, first check for an error then if its not available run an each on it to print the friends...

    function fetchfriends(){
    $.getJSON('sys/classes/core.php?task=fetchfriends&userid='+sessionparts[1], function(data) {
        if(data.error)
        {
            // your data is an array of friends, unless you have an error, then it is no array, so no need for the each to print the error.
            $('<div class="myfriendsbar">'+data.error+'</div>').appendTo('#mycontacts');
            return false;
        }
        else
        {
            //only when data.error does not exist it is filled as an array of friends you return i presume... 
            $.each(data, function(key, val) {
                $('<div class="myfriendsbar"><div id="friendsimage'+val.to_userid+'" style="width:32px; height:32px; float:left; margin:3px;"></div>'+val.firstname+' '+val.lastname+'<br/>'+val.username+'</div>').appendTo('#mycontacts');
                $('#friendsimage'+val.to_userid).css("background-image", "url(http://bonush.com/beta/sys/classes/photofetch.php?pic="+val.to_userid+":9177156176671)"); 
            });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配