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

查看:73
本文介绍了每行仅显示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开始受支持)或阅读本文.

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天全站免登陆