PHP_Idiot Posted October 30, 2009 Share Posted October 30, 2009 Ok now I know I'm being thick here, but I have the following loop running on my page and I want it to show the first 50 results from the array, which works fine. However at times there are less than 50 results in the array so it throws up errors for each of the blank lines. How do I limit it to 50 when there are more than 50 in the array, and show all without errors for when there are less than 50? for($i = 1; $i < 51; ++$i){ echo" <tr><td align=\"center\">$pos</td> <td align=\"center\">" . $arr[$i]['1'] . "</td> <td align=\"center\">" . $arr[$i]['2'] . "</td> <td align=\"center\">" . $arr[$i]['3'] . "</td> <td align=\"center\">" . $arr[$i]['4'] . "</td> <td align=\"center\">" . $arr[$i]['5'] . "</td> <td align=\"center\">" . $arr[$i]['6'] . "</td> </tr> "; $pos++; } Quote Link to comment https://forums.phpfreaks.com/topic/179686-stop-that-loop/ Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 Something like... $count = count($arr) > 51 ? 51 : count($arr); for($i = 1; $i < $count; ++$i){ Quote Link to comment https://forums.phpfreaks.com/topic/179686-stop-that-loop/#findComment-948076 Share on other sites More sharing options...
PHP_Idiot Posted October 30, 2009 Author Share Posted October 30, 2009 Cags your a genius thanks..it's been baffling me for ages! I don't suppose you also know how I can format the table to show a thick red line between row 40 and 41!! I need to try to differentiate between the top forty and all the rest, and ideas? Quote Link to comment https://forums.phpfreaks.com/topic/179686-stop-that-loop/#findComment-948084 Share on other sites More sharing options...
cags Posted October 30, 2009 Share Posted October 30, 2009 for($i = 1; $i < 51; ++$i){ if($i == 41) { echo" <tr><td align=\"center\">$pos</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['1'] . "</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['2'] . "</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['3'] . "</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['4'] . "</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['5'] . "</td> <td style=\"border-top: solid #F00 2px;\" align=\"center\">" . $arr[$i]['6'] . "</td> </tr>"; } else { echo" <tr><td align=\"center\">$pos</td> <td align=\"center\">" . $arr[$i]['1'] . "</td> <td align=\"center\">" . $arr[$i]['2'] . "</td> <td align=\"center\">" . $arr[$i]['3'] . "</td> <td align=\"center\">" . $arr[$i]['4'] . "</td> <td align=\"center\">" . $arr[$i]['5'] . "</td> <td align=\"center\">" . $arr[$i]['6'] . "</td> </tr>"; } $pos++; } Disclaimer: That is poor HTML design, but this is a PHP forum so I'm focusing on the logic. Both the alignment and the styling should really be done using seperate CSS. You would ideally add a class to that row that would have the upper border set using a seperate stylesheet. Quote Link to comment https://forums.phpfreaks.com/topic/179686-stop-that-loop/#findComment-948090 Share on other sites More sharing options...
PHP_Idiot Posted October 30, 2009 Author Share Posted October 30, 2009 Dude you rock! that does exactely what I need, it didn't occur to me that I could run an IF inside the FOR loop like that! and matched up with your earlier comment that sorted my $count if works a charm. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/179686-stop-that-loop/#findComment-948110 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.