2016-04-27 43 views
1

我試圖在1循環後添加2引導類col-md-8,然後在php while循環中添加2 col-md-4類。這個過程在完整循環中應該是相同的。所以結果會是這樣的:如何在php循環中添加一個類?

enter image description here

我當前的代碼是波紋管,但它沒有顯示的結果我需要什麼,也無法得到一個想法循環將怎麼樣!

全碼:

<div class="row text-center"> 
    <h2>What we offer</h2> 
    <hr class="separator"> 
    <?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class)) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); if($x & 1) { $col='8' ; }else { $col='4' ; } ?> 
    <div class="col-sm-<?php echo $col; ?>"> 
     <div class="we-offer"> 
      <a href="area"> 
         <img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block"> 
         <h3><?php echo ucfirst($menu_class_name); ?></h3> 
        </a> 
     </div> 
    </div> 
    <?php $x++; } ?> 

</div> 

最新代碼:

<div class="row text-center"> 
    <h2>What we offer</h2> 
    <hr class="separator"> 
    <?php $get_menu_class=m ysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); $x=0; while($menu_class_result=m ysqli_fetch_array($get_menu_class)) { $menu_class_name=h tmlspecialchars($menu_class_result[ 'pcat_name']); $menu_class_image=h tmlspecialchars($menu_class_result[ 'pcat_image']); $col=((($x+1)/2)%2)? "8": "4"; ?> 
    <div class="col-sm-<?php echo $col; ?>"> 
     <div class="we-offer"> 
      <a href="area"> 
       <img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block"> 
       <h3><?php echo ucfirst($menu_class_name); ?></h3> 
      </a> 
     </div> 
    </div> 
    <?php $x++; } ?> 

</div> 

當前結果:

[[[在這裏輸入的形象描述] [2]!] 2]

+0

你在做什麼變量$ col可以請你添加更多的代碼。 – GiftZwergrapper

+0

是的,我更新了我的問題。 –

+0

你的第二個'div'在哪裏? – mitkosoft

回答

3

試試這個邏輯請:$col = ((($i+1)/2)%2)?"8":"4";

https://3v4l.org/GHGVp

正如你可以看到它輸出了預期的效果。

The col for loop 0 is col4 
The col for loop 1 is col8 
The col for loop 2 is col8 
The col for loop 3 is col4 
The col for loop 4 is col4 
The col for loop 5 is col8 
The col for loop 6 is col8 
The col for loop 7 is col4 
The col for loop 8 is col4 
The col for loop 9 is col8 
The col for loop 10 is col8 
The col for loop 11 is col4 
The col for loop 12 is col4 
The col for loop 13 is col8 
The col for loop 14 is col8 
The col for loop 15 is col4 
The col for loop 16 is col4 
The col for loop 17 is col8 
The col for loop 18 is col8 
The col for loop 19 is col4 

你只需要在代碼中

if($x & 1) { 
    $col = '8'; 
}else { 
    $col = '4'; 
} 

$col = ((($x+1)/2)%2)?"8":"4"; 
+0

不是。首先它應該顯示4,8,8,4,4,8,8,4,4等等......在你的例子中,你缺少了4.它應該是兩次:) –

+0

@shibbirahmed哦,沒有看到。給我一分鐘的病態修復 – Sharky

+0

肯定@Sharky ... –

0

來代替我只想跟蹤顯示的變量。如果先將其定義爲1,那麼只會在第一次顯示所需的第一列時切換到另一列。

那麼你不需要跟蹤$x。我不會把這稱爲一個簡單的數學問題,其他的答案在這裏不適合你的用例。

<div class="row text-center"> 
    <h2>What we offer</h2> 
    <hr class="separator"> 

    <?php 
     $get_menu_class = mysqli_query($conn, "SELECT pcat_name, pcat_image FROM product_category ORDER BY pcat_id DESC"); 
     $col = 4; 
     $displayed = 1; 

     while($menu_class_result = mysqli_fetch_array($get_menu_class)) { 
      $menu_class_name = htmlspecialchars($menu_class_result['pcat_name']); 
      $menu_class_image = htmlspecialchars($menu_class_result['pcat_image']); 
    ?> 
      <div class="col-sm-<?php echo $col; ?>"> 
       <div class="we-offer"> 
        <a href="area"> 
         <img src="<?php echo IMG_DIR."/menu_class/$menu_class_image"; ?>" alt="" class="img-responsive center-block"> 
         <h3><?php echo ucfirst($menu_class_name); ?></h3> 
        </a> 
       </div> 
      </div> 
    <?php 
      if($displayed){ 
       switch($col){ 
        case 4: 
         $col = 8; 
         break; 
        case 8: 
         $col = 4; 
         break; 
        default: 
         $col = 4; 
       } 

       $displayed = 0; 
      } else { 
       $displayed = 1; 
      } 
     } 
    ?> 

</div> 
+0

不幸的是沒有工作:( –

+0

錯誤或任何你能闡述一下? –

+0

@shibbirahmed我忘了一個愚蠢的分號。如果 –