2016-04-27 14 views
1

私は2つのブートストラップクラスcol-md-8を1つのループの後に追加し、次に2 col-md-4のクラスをPHPのwhileループに追加しようとしています。このプロセスはループ全体で同じでなければなりません。だから、結果は次のようになります。どのようにループ中に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

あなたの2番目の '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 

あなたはちょうど私がちょうど表示された変数を追跡するでしょう、あなたのコード内で

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

いいえ、それは最初に4,8,8,4,4,8,8,4,4、と1を表示する必要があります....あなたの例では、あなたは行方不明です。それは2回する必要があります:) –

+0

@shibbirahmed oh 、それを見ていない。私にはちょっとした不具合があります。 – Sharky

+0

確かに@Sharky ... –

0

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

を交換する必要があります。最初に1と定義した場合、最初の列が最初に設定されたことだけが表示され、次にもう一方の列に切り替わります。

その後、$xを追跡する必要はありません。私はこれを簡単な数学の問題とは呼んでいないでしょう。ここにあるOKの答えはあなたのユースケースに合っていません。

<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私は愚かなセミコロンを忘れました。 –

関連する問題