2012-12-12 46 views
-2

我正在運行一個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">&nbsp;</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

+1

添加計數器和使用[模運算符(http://php.net/manual/en/language。 operators.arithmetic.php)。 –

+0

使用模板來分離邏輯和視圖! –

回答

2

這是很簡單的。我希望我理解正確你的問題:

$i = 0; 
while (($row = mysql_fetch_object($result)) !== false) { 
    if ($i < 3) { 
     // First code 
    } else { 
     // Second code 
    } 
    $i += 1; 
} 
+1

非常感謝。這非常合理。 –

+1

我可以使用elseif做三個代碼嗎? –

+1

當然,雖然通常當'if/else'塊開始變得複雜時,你應該考慮使用[switch](http://php.net/manual/en/control-structures.switch.php)語句: – NitayArt

0

你可以用if語句的簡單的計數器和做easaly:

$count = 0; 

while(fetching) { 
    if($count < 3) { 
     // items 1, 2 and 3 
    } else { 
     // do normal 
    } 

    $count++; 
} 
2

首先,你應該避免使用任何mysql_功能,因爲它們都過時,會從PHP的未來版本中刪除。我建議使用PDO。請參閱this tutorial開始。

然後,它會簡單地做這樣的事情的情況下:

foreach($db->query($query) as $index => $row) { 
    if ($index < 3) { 
     // first 3 items 
    } else { 
     // other items 
    } 
} 
+0

Many謝謝你的幫助。 –