我正在運行一個while循環,它從mySQL中取出行並將它們回顯出來。相當標準。但是,第一個項目需要不同的HTML標記,接下來的兩個項目,然後正常進入while循環。PHP循環拆分爲兩個
目前,我while循環看起來像這樣:
while(($row = mysql_fetch_object($result)) !== false)
{
// Places ad based of predefined variable frequency
if ($ad == $adFrequency){
echo '<div class="one-advertisement-article clearfix">';
echo '<div class="grid_9 suffix_2"><img src="http://placehold.it/263x75/000000/ffffff"></div>';
echo '<div class="grid_1"><a class="navigate-right-icon ss-icon" href="#">navigateright</a></div>';
echo '</div>';
$ad = 0;
}
echo '<div class="one-news-article clearfix">';
if($row->imagelibid)
{
$imageid = intval($row->imagelibid);
$image_result = mysql_query("SELECT * FROM imagelib WHERE id = {$imageid}", $db);
$image_row = mysql_fetch_object($image_result);
$image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;
echo '<div class="grid_4"><a href="#"><img src="'.$image_url.'"></a></div>';
}
else {
echo '<div class="grid_4"><div class="news-placeholder"><span class="ss-icon">ellipsischat</span></div></div>';
}
echo '<div class="grid_7">';
echo '<h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';
$published_date = date('D, d M Y', strtotime($row->datein));
echo '<p class="published-date">'.$published_date.'</p>';
echo '</div>';
echo '<div class="grid_1">';
echo '<div class="news-item-vertical-sep"> </div>';
echo '<p></p>';
echo '</div>';
echo '</div>';
$ad++;
}
這工作得很好,但我需要的前三新聞和使用:
echo ' <div class="one-news-featured-article clearfix">';
echo ' <div class="grid_12">';
if($row->imagelibid)
{
$imageid = intval($row->imagelibid);
$image_result = mysql_query("SELECT * FROM imagelib WHERE id = {$imageid}", $db);
$image_row = mysql_fetch_object($image_result);
$image_url = "http://www.#.com/slir/w280/imagelib/" . $image_row->id . "_" . $image_row->filename;
echo '<div class="large-featured-image-container"><img src="'.$image_url.'">/div>';
}
echo ' <div>';
echo ' <h2><a href="item-news.php?id='.$row->id.'">'.$row->title.'</a></h2>';
echo ' </div>';
echo ' </div>';
echo ' </div>';
echo ' <div class="grid_6">';
任何幫助嗎?實質上,它需要將第二個代碼應用到查詢中的前三個項目,然後繼續使用當前while循環中包含的代碼 - 就像我猜測的偏移量。
感謝, [R
添加計數器和使用[模運算符(http://php.net/manual/en/language。 operators.arithmetic.php)。 –
使用模板來分離邏輯和視圖! –