Diverse Diverse - 1 month ago 5
HTML Question

Table max 3 <td> loop in other tr

How to make it like only 3 offers in row NOW I have one row and all offer is in one line I just want to to show max 3 td in tr and loop.

if(count($offer_list['item']) > 0)
{
$main_content .= '<table cellspacing="2" cellpadding="2" width=30>';
foreach($offer_list['item'] as $item)
{
$main_content .= '<td class="shop">
<a class="shop" style="display: block;" href="/shop/item/id/'.$item['id'].'">
<center><img src="item_images/'.$item['item_id'].'.gif"><br><font color="white"><b>'.$item['name'].'</font></b><font color="red"> <b> '.$item['points'].' points</font></b>';
if(!$logged)
{
$main_content .= '<p><b><a href="/account">[Login to buy]</a></b>';
}
else
{
$main_content .= '<br><b><font size=1 color="white">Your points balance is: '.$user_premium_points;
$main_content .= '<br><font color="white"> <fieldset class="shop"><legend>DESCRIPTION</legend>'.$item['description'].'</fieldset></font><br><font color="white"><b> [Click to buy] </b></font></form> </a></center></td>';
}
$main_content .= '';
}
$main_content .= '</tr></table>';
}





Answer
$row_count = 1;

$main_content .= '<table cellspacing="2" cellpadding="2" width=30>';
  foreach($offer_list['item'] as $item) {

    // Check, if we are at position "1". If so, start new row
    if ($row_count === 1) { $main_content .= '<tr>'; }

    $main_content .= '<td class="shop"><a class="shop" style="display: block;" href="/shop/item/id/'.$item['id'].'">
     <center><img src="item_images/'.$item['item_id'].'.gif"><br><font color="white"><b>'.$item['name'].'</font></b><font color="red"> <b> '.$item['points'].' points</font></b>';
    if(!$logged) {
      $main_content .= '<p><b><a href="/account">[Login to buy]</a></b>';
    }
    else {
      $main_content .= '<br><b><font size=1 color="white">Your points balance is: '.$user_premium_points;
      $main_content .= '<br><font color="white"> <fieldset class="shop"><legend>DESCRIPTION</legend>'.$item['description'].'</fieldset></font><br><font color="white"><b> [Click to buy] </b></font></form> </a></center></td>';
    }


    // Check, if we we are at position "3". If so, end row. (set position back to 1)
    // Otherwise increase position with one level.
    if ($row_count === 3) { 
      $row_count = 1;
      $main_content .= '</tr>';
    }
    else {
      $row_count++;
    }
  } // end foreach


// This is code for filling the "blank" table cells, if you don't have exactly some item count / 3 = 0
if ($row_count !== 3) {
  for ($i=0; $i<=(3-$row_count); $i++) {
    $main_content .= '<td>&nbsp;</td>';
  }
  $main_content .= '</tr>';
}
$main_content .= '</table>';

PS: <center> is deprecated or more to say not valid HTML anymore.

Comments