Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.