2014-08-29 51 views
0

佈局是塊/模塊化的,所以我需要爲每個div應用一個類。問題是,當我在瀏覽器中預覽,代碼循環開始再次在情況1之後 - 它跳過從DB的項目之一,並顯示下一個項目的情況下,1使用switch語句從數據庫調用:PHP

 $result = mysqli_query($connection, "SELECT * FROM products"); 
     $index = 1; 
     while($row = mysqli_fetch_array($result)) { 

     $float = ""; 

      switch ($index) { 
       case 1: $float = "large-left"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 2: $float = "mini-left"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 3: $float = "mini-right"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 4: $float = "mini-left"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 5: $float = "mini-right"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 6: $float = "large-right"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 7: $float = "mini-left"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 8: $float = "mini-right"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 9: $float = "mini-left"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       case 10: $float = "mini-right"; 
         echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>"; 
        break; 
       default: $index = 0; 
        break; 
      };   

     $index++; 
     }; 

     mysqli_close($result); 
    ?> 

任何建議/替代方法將不勝感激!

+0

「分組」請告訴我這裏$指數是爲了什麼? – Naincy 2014-08-29 06:43:21

回答

5

你定的$指數爲1

$index = 1; 

所以,它會去,每次情況1。它不是動態的。

+0

每當聲明完成時,我都需要將它轉到案例1 - 但是當它完成時 - 它從數據庫中跳過一個產品並將下一個產品作爲案例1顯示。爲什麼要這樣做? – Gregorios 2014-08-29 07:13:52

+0

while($ row = mysqli_fetch_array($ result,MYSQL_NUM)) – Naincy 2014-08-29 07:20:32

0

如果我理解正確的話,你要的10個產品

$result = mysqli_query($connection, "SELECT * FROM products"); 
$index = 1; 
while($row = mysqli_fetch_array($result)) { 
    if ($index % 10 == 1) { 
     $float = 'large-left'; 
    } 
    else { 
     if($index % 2 == 1) { 
      $float = 'mini-right'; 
     } 
     else { 
      $float = 'mini-left'; 
     } 
    } 
    echo '<div class="' . $float . '"><img src="' . $row['product_imagepath'] . '"></div>'; 
    $index++; 
}