表格最多3个循环在其他tr

时间:2014-03-06 22:20:16

标签: php html html-table

如何让它像行中的3个优惠现在我有一行,所有优惠都在一行中我只想在tr和循环中显示最大3 td。

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

1 个答案:

答案 0 :(得分:0)

$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>或更多说不再有效的HTML。