Jump to content

From while to for loop


chomedey

Recommended Posts

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

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?

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.