問(wèn)題描述
我有如下腳本
$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;
?>
它以長(zhǎng)水平線顯示所有結(jié)果我如何打破結(jié)果以便它們顯示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.
推薦答案
添加一個(gè)計(jì)數(shù)器并在每次達(dá)到 3 的倍數(shù)時(shí)開(kāi)始新行.
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>";
請(qǐng)注意:它可能無(wú)法回答您的問(wèn)題,但您應(yīng)該停止使用 mysql_*
函數(shù).它們正在被棄用.而是使用 PDO(自 PHP 5.1 起支持)或 mysqli(自 PHP 4.1 起支持).如果您不確定使用哪個(gè),閱讀這篇文章.
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.
這篇關(guān)于每行僅顯示 3 個(gè) foreach 結(jié)果的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!