Jump to content

Stop that Loop!!


PHP_Idiot

Recommended Posts

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++;
}

 

Link to comment
https://forums.phpfreaks.com/topic/179686-stop-that-loop/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179686-stop-that-loop/#findComment-948090
Share on other sites

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.