TeddyKiller Posted April 6, 2010 Share Posted April 6, 2010 I've got a function, and I'm wanting it to display "This user has no friends" instead, it displays blank. I'm assuming it's getting rows. Can someone correct it for me? thanks. function html_friends($id) { $query = mysql_query("SELECT * FROM `friends` WHERE `user_id`='$id' ORDER BY RAND() LIMIT 12"); $html_amount_friends = ((mysql_num_rows($query) == 1) ? '<a href="#" style="float:right; margin-right:5px;">1 Friend</a><br />' : '<a href="#" style="float:right; margin-right:5px;">'.mysql_num_rows($query).' Friends</a><br />'); while($row = mysql_fetch_array($query)) { $avat = user_fetch_avatars($row['friend_id']); $html_friends = ((mysql_num_rows($query) == 0) ? 'This user has no friends' : '<a href="profile.php?uid='.$row['friend_id'].'"> <img src="resize_image.php?file='.$avat['avatar'].'&size=60" title="" border="0" /> </a>'); } return '<div style="float:left; width:400px; margin-top:10px;">' . '<div style="background:#e11919; text-align:center; color:#fff; font-size:14px; font-weight:bold; padding:5px;">' . 'Friends' . '</div>' . '<div style="border:1px solid #000; text-align:center; padding-bottom:10px;">' . $html_amount_friends . $html_friends . '</div>' . '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/ Share on other sites More sharing options...
aeroswat Posted April 6, 2010 Share Posted April 6, 2010 Have you tried to manually run that query on your database to see if it is in fact returning any rows? That way we can tell you what's wrong with it Also that part where you show the difference between 1 Friend and N# Friends can be shortened a lot more. Use your conditional instead to define whether or not an s is there at the end of Friend since everything else is exactly identical Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1037997 Share on other sites More sharing options...
AdRock Posted April 6, 2010 Share Posted April 6, 2010 Don't know of this will work but i removed a couple of brackets $html_amount_friends = (mysql_num_rows($query) == 1) ? '<a href="#" style="float:right; margin-right:5px;">1 Friend</a><br />' : '<a href="#" style="float:right; margin-right:5px;">'.mysql_num_rows($query).' Friends</a><br />'; while($row = mysql_fetch_array($query)) { $avat = user_fetch_avatars($row['friend_id']); $html_friends = (mysql_num_rows($query) == 0) ? 'This user has no friends' : '<a href="profile.php?uid='.$row['friend_id'].'"> <img src="resize_image.php?file='.$avat['avatar'].'&size=60" title="" border="0" /> </a>'; Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1037999 Share on other sites More sharing options...
aeroswat Posted April 6, 2010 Share Posted April 6, 2010 Don't know of this will work but i removed a couple of brackets $html_amount_friends = (mysql_num_rows($query) == 1) ? '<a href="#" style="float:right; margin-right:5px;">1 Friend</a><br />' : '<a href="#" style="float:right; margin-right:5px;">'.mysql_num_rows($query).' Friends</a><br />'; while($row = mysql_fetch_array($query)) { $avat = user_fetch_avatars($row['friend_id']); $html_friends = (mysql_num_rows($query) == 0) ? 'This user has no friends' : '<a href="profile.php?uid='.$row['friend_id'].'"> <img src="resize_image.php?file='.$avat['avatar'].'&size=60" title="" border="0" /> </a>'; What brackets did you remove? Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038003 Share on other sites More sharing options...
jcbones Posted April 6, 2010 Share Posted April 6, 2010 I would save the return of the function to a variable, so I wouldn't have to call it more than once. Not only that, but using num_rows() to return the amount of friends is flawed logic, unless you are specifically limiting your users to 12 friends (The LIMIT from your query). Other than that, if the query returns 0 results, the WHILE loop will NOT run. Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038010 Share on other sites More sharing options...
aeroswat Posted April 6, 2010 Share Posted April 6, 2010 I would save the return of the function to a variable, so I wouldn't have to call it more than once. Not only that, but using num_rows() to return the amount of friends is flawed logic, unless you are specifically limiting your users to 12 friends (The LIMIT from your query). Other than that, if the query returns 0 results, the WHILE loop will NOT run. This too. Good spot Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038012 Share on other sites More sharing options...
AdRock Posted April 6, 2010 Share Posted April 6, 2010 Don't know of this will work but i removed a couple of brackets $html_amount_friends = (mysql_num_rows($query) == 1) ? '<a href="#" style="float:right; margin-right:5px;">1 Friend</a><br />' : '<a href="#" style="float:right; margin-right:5px;">'.mysql_num_rows($query).' Friends</a><br />'; while($row = mysql_fetch_array($query)) { $avat = user_fetch_avatars($row['friend_id']); $html_friends = (mysql_num_rows($query) == 0) ? 'This user has no friends' : '<a href="profile.php?uid='.$row['friend_id'].'"> <img src="resize_image.php?file='.$avat['avatar'].'&size=60" title="" border="0" /> </a>'; What brackets did you remove? 1 at the beginning here ((mysql_num_rows($query) and the other at the end </a>'); Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038014 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 I would save the return of the function to a variable, so I wouldn't have to call it more than once. Not only that, but using num_rows() to return the amount of friends is flawed logic, unless you are specifically limiting your users to 12 friends (The LIMIT from your query). Other than that, if the query returns 0 results, the WHILE loop will NOT run. 12 is the limit of friends being shown on the main profile, and I believe what you stated last.. is where the problem lays. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038037 Share on other sites More sharing options...
ignace Posted April 7, 2010 Share Posted April 7, 2010 <?php function user_get_friends($id, $limit = 0, $orderby = 'rand()', $sort = 'ASC') { $id = intval($id); if (0 === $id) return array(); $result = mysql_query("SELECT * FROM friends WHERE user_id = $id ORDER BY $orderby $sort"); if (!$result) return array(); $friends = array(); $i = 1; while ($row = mysql_fetch_assoc($result)) { $friends[] = $row; if ($limit > 0 && $i === $limit) break; ++$i; } mysql_free_result($result); return $friends; } function html_template_apply($template, $data) { $keys = array_keys($data); foreach ($keys as $key) { $template = str_replace('{' . $key . '}', $data[$key], $template); } return $template; } function html_friends($id) { $friends = user_get_friends($id); $friends_count = sizeof($friends); $html_friends_amount = $friends_count > 1 ? '<a href="#" style="float:right; margin-right:5px;">' . $friends_count . ' Friends</a><br />' : ($friends_count === 1 ? '<a href="#" style="float:right; margin-right:5px;">1 Friend</a><br />' : 'This user has no friends'); $html_template_friends = '{friends_count} Friends ' . '<a href="profile.php?uid={friend_id}">' . '<img src="resize_image.php?file={avatar}&size=60" title="" border="0" />' . '</a>'; $html_friends = ''; foreach ($friends as $friend) { $friend = array_merge($friend, array('avatar' => user_get_avatar($friend['friend_id'])), array('friends_count' => $friends_count)); $html_friends = html_template_apply($html_template_friends, $friend); } return '<div style="float:left; width:400px; margin-top:10px;">' . '<div style="background:#e11919; text-align:center; color:#fff; font-size:14px; font-weight:bold; padding:5px;">' . $html_friends_amount . '</div>' . '<div style="border:1px solid #000; text-align:center; padding-bottom:10px;">' . $html_friends . '</div>' . '</div>'; } function user_get_avatar($id, $default_avatar_path = '') { static $avatars = array(); $id = intval($id); if (0 === $id) return $default_avatar_path; if (!isset($avatars[$id])) { $result = mysql_query("SELECT id, avatar FROM avatars WHERE user_id = $id"); if (!$result) return $default_avatar_path; list($id, $avatar) = mysql_fetch_row($result); $avatars[$id] = $avatar; } return $avatars[$id]; } My 2 cents Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038196 Share on other sites More sharing options...
sasa Posted April 7, 2010 Share Posted April 7, 2010 look this part of your code while($row = mysql_fetch_array($query)) {//if query return zero row you never go in while loop!! $avat = user_fetch_avatars($row['friend_id']); $html_friends = ((mysql_num_rows($query) == 0) ? 'This user has no friends' //this condition is allways false!! Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038334 Share on other sites More sharing options...
TeddyKiller Posted April 7, 2010 Author Share Posted April 7, 2010 Yeah Ignace, I didn't notice that displayed friends until I looked again. Quote Link to comment https://forums.phpfreaks.com/topic/197800-why-isnt-this-function-displaying-a-certain-string-if-mysql_num_rows-0/#findComment-1038479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.