user4fun Posted October 22, 2009 Share Posted October 22, 2009 I want to stop a while loop after it displays 7 records. If there is only 4 records that it displayed, then it would fill in line 5, 6, and 7 with "No records"Any help would be great. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 23, 2009 Share Posted October 23, 2009 You can use a break statement to exit a loop. If you keep a counter of how many records you've shown you can check the counter after the loop ends to see if you need to show blank records. Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 here is what I have so far, it does not show the no record $i=1;while($row = mysql_fetch_array($result)) { echo $i . "-"; if (is_null($row['Ad_Name'])) { echo " No record"; } else { echo $row['Ad_Name']; } echo "</br>"; $i++; if ($i==8) { break; } }?> Link to comment Share on other sites More sharing options...
chibineku Posted October 23, 2009 Share Posted October 23, 2009 You could split it into two loops:$i=1;while($row = mysql_fetch_array($result)) { echo $i . "-"; echo $row['Ad_Name']; echo "</br>"; $i++; if ($i==8) { break; } }for($x = (8-$i); $x < 8; $x++) {echo 'No record';} Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 Tat sounds good except taht it needs some fine tuning.The results are now1-Start3End42-Start4End53-Start5End64-Start7End8No recordNo recordNo recordNo recordNo record I want the result to look like this1-Start3End42-Start4End53-Start5End64-Start7End85- No record6- No record7- No record8- No record Link to comment Share on other sites More sharing options...
chibineku Posted October 23, 2009 Share Posted October 23, 2009 }for($x = (8-$i); $x < 8; $x++) {echo $x.'No record<br />';} That one you should have got on your own, tbh. Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 I did that alraedy,, it is displaying more records than I want. That is why I asked again, sorryI am still playing with it $i=1;while($row = mysql_fetch_array($result)){echo $i . "-";echo $row['Ad_Name'];echo "</br>";$i++;if ($i==8){break;}}if ($i<8){$r = (8-$i);for($x; $x == $r; $x++) {echo $i . "-";echo "No record";echo "</br>";$i++;}} Link to comment Share on other sites More sharing options...
chibineku Posted October 23, 2009 Share Posted October 23, 2009 oh, well change instances of 8 with 7. Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 it is not displaying the No record at all Link to comment Share on other sites More sharing options...
chibineku Posted October 23, 2009 Share Posted October 23, 2009 for($x; $x ***==*** $r; $x++) Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 I really appreciate your help but your line that you gave me is exactly what I had!I am getting more lost lol Link to comment Share on other sites More sharing options...
chibineku Posted October 23, 2009 Share Posted October 23, 2009 I know it was...the asterisks enclose the area you need to think about. Compare and contrast with the original for loop I showed you. Link to comment Share on other sites More sharing options...
Kameloh Posted October 23, 2009 Share Posted October 23, 2009 I want to stop a while loop after it displays 7 records. If there is only 4 records that it displayed, then it would fill in line 5, 6, and 7 with "No records"Any help would be great. $result = mysql_query("SELECT items FROM table LIMIT 7"); // <-- Your query, limiting 7 is optional$i = 0;while ($i < 7) { $item = mysql_result($result,$i,'Ad_Name'); if (!empty($item)) { echo "<br>$item"; } else { echo "<br>No Record"; } $i++;} Hope it helps Link to comment Share on other sites More sharing options...
user4fun Posted October 23, 2009 Author Share Posted October 23, 2009 BINGO - This works just fine, thank you a bunch for getting me there $left_rows = (8-$i);for($x = 1; $x <= $left_rows; $x++) { echo $i . "-No record" . "<br />";$i++; } Link to comment Share on other sites More sharing options...
justsomeguy Posted October 23, 2009 Share Posted October 23, 2009 You could also do this, there's no reason to use a separate loop counter for the for loop:for($i; $i <= 7; $i++) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.