Jump to content

[SOLVED] Array problem


Ashoar

Recommended Posts

I have this small section of code. I am fetching data from a MYSQL row and wan't to display every entry in there.

Normally this worlks fine but for some reason this method isn't working.

 

Here is the bit of code:

$online = mysql_query("SELECT username FROM online") or die(mysql_error());
$userrows = mysql_num_rows($online);

for($count = 1; $count <= $userrows; $count++)
{
$name = mysql_fetch_array($online);

print "<tr class='mainrow'><td>Currently active: <A href='member_profile.php?username=$name[username]'>$name[username]</a></td></tr>";
}
print "</table>";

 

When i check, it only displays one entry and not all of them.

Link to comment
Share on other sites

For looping through mysql results use a while loop rather than a for loop

$online = mysql_query("SELECT username FROM online") or die(mysql_error());

if(mysql_num_rows($online) > 0)
{
    while($row = mysql_fetch_assoc($online))
    {
        $online[] = '<A href="member_profile.php?username='.$row['username'].'">'.$row['username'].'</a>';
    }

    echo '<tr class="mainrow"><td>Currently active: ' . implode(',', $online) . '</td></tr>';
}
else
{
    echo '<tr class="mainrow"><td>There are currently no users logged in</td></tr>';
}

echo '</table>';

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.