chomedey Posted February 19, 2010 Share Posted February 19, 2010 Hello all, I'm fairly new to php and seem to be having difficult understanding conditionals. This while loop (below) works fine, but I want to change it to a for loop so it only serves up 10 results (as opposed to 11, which is how many the query is getting from the db, to know whether to put a "next" button at the bottom of the screen). I know this should be fairly easy, but I can't seem to do it. Any help much appreciated. Thanks. Julian if ($r = mysql_query($query)) { while ($row = mysql_fetch_array($r)) { $entry = $row['entry']; $short_entry = shorten($entry); $id = $row['entryID']; $num_results = mysql_num_rows($r); If ((isset($num_results)) && (is_numeric($num_results))) { echo "<p><h3><a href='view_entry.php?id=".($id)."'>{$row['title']}</h3></a></p>"; echo "<p style='color: gray;'> Submitted by {$row['username']} on {$row['newdate']}<br /> Views: {$row['views']} | Category: {$row['category']} | "; If (empty($row['total_rating'])) { echo " Rating: Not yet rated<br /></p>"; } else { echo " Rating: {$row['total_rating']} out of 5 with {$row['num_votes']}"; if (($row['num_votes']) > 1) { echo ' votes '; } else { echo ' vote '; } echo "cast<br /></p>"; } echo "<p class='entry_text'>"; echo $short_entry; echo "... <a href='view_entry.php?id=".($id)."'>(read more)</a><br /><hr>"; } else { echo "There were no matches for that search term."; } Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/ Share on other sites More sharing options...
SchweppesAle Posted February 19, 2010 Share Posted February 19, 2010 I'd just set a limit on the query. If you want to get the number of rows to use in a for construct then you can use the mysql_num_rows() function though. Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014970 Share on other sites More sharing options...
shlumph Posted February 19, 2010 Share Posted February 19, 2010 Why not limit your SQL query to 10? That'd be more efficient SELECT * FROM table LIMIT 10 Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014973 Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 well u could just ad incrementer in your while loop $i = 0; while() { //somewhere in the middle of the code if($i==10) { echo "button"; } $i++; } Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014974 Share on other sites More sharing options...
chomedey Posted February 19, 2010 Author Share Posted February 19, 2010 I have set a limit on the query, a limit of 11, the extra one being necessary to see if there are more rows to be had, so I can put a "next" button at the bottom of the page that then goes and gets the next ten (plus 1 to do the same thing again). But I only want to echo 10, not the last one (until the next page, that is). Is that not the way these things are done? Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014978 Share on other sites More sharing options...
shlumph Posted February 19, 2010 Share Posted February 19, 2010 Hmm.. never thought of doing it that way. It's more mainstream to find the overall COUNT(id) to find out how many pages there are. If you're on the last page, don't print out the next button. Whatever floats your boat, though. Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014984 Share on other sites More sharing options...
Catfish Posted February 19, 2010 Share Posted February 19, 2010 i think he asking how to do a for loop with a limit of 10 loops? for ($i = 0; $i <= 9; $i++) { // loop ten times print($i.'<br/>'."\n"); } Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014986 Share on other sites More sharing options...
sader Posted February 19, 2010 Share Posted February 19, 2010 $result = mysql_query(); $num_rows = mysql_num_rows($result); if($num_rows !== false) { for($i=0; $i<10; $i++) { $row = mysql_fetch_assoc($result); } if($num_rows == 11) { echo "next >>"; } } Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1014988 Share on other sites More sharing options...
chomedey Posted February 19, 2010 Author Share Posted February 19, 2010 Thanks for this. Julian Link to comment https://forums.phpfreaks.com/topic/192664-from-while-to-for-loop/#findComment-1015054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.