Jump to content

Why isn't this function displaying a certain string if mysql_num_rows == 0 ?


TeddyKiller

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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?  :P

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?  :P

 

 

1 at the beginning here

((mysql_num_rows($query)

and the other at the end

</a>');

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!

     

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.