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);
}
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)");
});
}
});
In the "error" state, the JSON result will look like this:
{
"error": "No friends? ... etc."
}
So when you do $data.each
you'll get one iteration where key
is "error"
and val
is "No friends? ..."
etc. So, when you do val.error
you're accessing the error
property of a string, which doesn't exist.