Jump to content

while loop with if's inside


bschultz

Recommended Posts

The following code is causing a maximum execution time error...

 

 

$count = count($links);  // how many items are in the array
  // echo $count;  exit;  //this echo's 16...so there's stuff in the array
$i = 0; // total count
$s = 0; // table cell count...should ever get above the value of 3

while ($i <= $count);
{
  if ($s == 0) { echo "<tr>"; }  // if at the start of a table row, start the row
    echo $links[$i]; $i++; $s++;  // print out the table cell with the proper value
      if ($s < 3) { echo "<td> </td>"; $s++; }  // if we're not at table column 3, add a new column...this also should close the row with "null" values to fill in the table
     if ($s == 3) { echo "</tr>"; $s = 0; }  // are we at table column 3?  If so, close the row.
}

 

 

I'm trying to get the values from an array into an html table...three columns wide.  Obviously, the loop is stuck, so the max execution time is running out.  I just can't see where.  Any ideas?

 

Thanks!

Link to comment
Share on other sites

$count = count($links);  // how many items are in the array
  // echo $count;  exit;  //this echo's 16...so there's stuff in the array
$i = 0; // total count
$s = 0; // table cell count...should ever get above the value of 3

while ($i <= $count)
{
  if ($s == 0) { echo "<tr>"; }  // if at the start of a table row, start the row
    echo $links[$i]; $i++; $s++;  // print out the table cell with the proper value
      if ($s < 3) { echo "<td> </td>"; $s++; }  // if we're not at table column 3, add a new column...this also should close the row with "null" values to fill in the table
     if ($s == 3) { echo "</tr>"; $s = 0; }  // are we at table column 3?  If so, close the row.
}

 

 

try removing the ; from the end of your while statement, I have done so above, I think that will fix it

Link to comment
Share on other sites

Not sure why you're indenting the way you are, but your conditionals are not nested.  You you could safe yourself execution time by combining some of your conditions and using modulus.  You Further have not prepared for the situation when your table will not have 3 cells in the last row.  When you're incrementing every iteration, you should use a for() loop

 

<?php
$count = count($links);

// begin table row
$table = '<tr>'; 

for( $i=0, $s=0; $i <= $count; $i++, $s++; )
{
   // Third cell reached, close and start a new row
  if( $s == 3 ) {
    $table .= '</tr><tr>';  
    $s = 0;
  }
  // Otherwise we create a cell
   $table .=  '<td>' . $links[ $i ] . '</td>'; 
}

// check for end of table offset
if( $s != 3 ) $table .= '<td colspan=" . ( 3 - $s) . "> </td>';

// end table
$table .= '</tr>';

Link to comment
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.