每行仅显示 3 个 foreach 结果 [英] display only 3 foreach result per row

查看:32
本文介绍了每行仅显示 3 个 foreach 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下脚本

$output="<table class='products'><tr>"; 
while($info = mysql_fetch_array( $data )) { 
    //Outputs the image and other data
    $output.= "<td>
    <img src=http://localhost/zack/sqlphotostore/images/"   .$info['photo'] ." width=323px ></img> 
    <b>Name:</b> ".$info['name'] . "
    <b>Email:</b> ".$info['email'] . " 
    <b>Phone:</b> ".$info['phone'] . "</td> "; 
}
$output.="<tr></table>";
print $output;
?>

它以长水平线显示所有结果我如何打破结果以便它们显示3 次后在新行中.

it shows all results in long horizontal line how do i break the results so that they show in new row after 3 count.

推荐答案

添加一个计数器并在每次达到 3 的倍数时开始新行.

Add a counter and start a new row every time it reaches a multiple of 3.

$counter = 0;
while($info = mysql_fetch_array($data)) {
   if ($counter++ % 3 == 0) {
       if ($counter > 0) {
           $output .= "</tr>";
       }
       $output .= "<tr>";
   }
   // stuff
}
if ($counter > 0) {
    $output .= "</tr>";
}

$output .= "</table>";

请注意:它可能无法回答您的问题,但您应该停止使用 mysql_* 函数.它们正在被弃用.而是使用 PDO(自 PHP 5.1 起支持)或 mysqli(自 PHP 4.1 起支持).如果您不确定使用哪个,阅读这篇文章.

Please note: It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

这篇关于每行仅显示 3 个 foreach 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆