Coly010 Posted April 24, 2013 Share Posted April 24, 2013 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? Quote Link to comment Share on other sites More sharing options...
Lithium Posted April 24, 2013 Share Posted April 24, 2013 (edited) http://www.php.net/manual/en/function.mysql-fetch-row.php http://www.php.net/manual/en/function.mysql-fetch-assoc.php SELECT * FROM games ORDER BY visits DESC LIMIT 5" Edited April 24, 2013 by Lithium Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted April 24, 2013 Share Posted April 24, 2013 Tryc $r = mysqli_query($c, "SELECT * FROM games ORDER BY visits DESC LIMIT 0, 5"); Then maybe go for a while() function, then fetch your array and print your links Quote Link to comment Share on other sites More sharing options...
Coly010 Posted April 29, 2013 Author Share Posted April 29, 2013 Thanks, worked perfectly. Have another problem now though. If i want to put a different set of results, which do not have a limit on them, into a table which has four columns, how can I code it so that after every 4 it will take a new row? Quote Link to comment Share on other sites More sharing options...
Lithium Posted April 29, 2013 Share Posted April 29, 2013 add a counter for rows ($row = (isset($row)) ? ++$row : '1'; ) , add an if to when it reaches 4 change row and restart counter Quote Link to comment Share on other sites More sharing options...
Spudinski Posted April 29, 2013 Share Posted April 29, 2013 Simplified version. foreach($r2 as $r) { echo $r['meh'] . ' '; if ( (($c = array_search($a, $r)) == 0 ? 1 : ++$c) % 4 === 0) { echo PHP_EOL; } } Quote Link to comment 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.