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
https://forums.phpfreaks.com/topic/153299-solved-array-problem/
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
https://forums.phpfreaks.com/topic/153299-solved-array-problem/#findComment-805360
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.