Jump to content

PHP Loop with MySQL help


Recommended Posts

Ok, so I'm trying to save myself a bit of time, whilst adding a feature into my site.

I have a mysqli_query(); get an array of results ordered in descending order by the visits column.

$r = mysqli_query($c, "SELECT * FROM games ORDER BY visits DESC");
$r2= mysqli_fetch_array($r2);


ok, now I only want the first 5 results and want to be able to place their id into a link like:

<a href='game.php?id={$r2['id']}'>Link</a>

and i only want the first 5, so im thinking i have to loop through them.

Now i dont know how to do this. I'm thinking :

for ( $i = 0; $i <= 5; $i++ )
echo '<a href="game.php?id={$r2[\'id\']}';


now when i do this it only repeats one of them, it doesn't lit through them.

Any ideas?

Link to comment
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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