本文介紹了每行僅顯示 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 結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!