2017-10-07 14 views
0

序列在用PHP從MySQL顯示結果

 <div class="container-fluid projects-container"> 
 
      <div class="container projects-list"> 
 
\t \t \t 
 
<?php 
 
$servername = "localhost"; 
 
$username = "user_showcase"; 
 
$password = "password123"; 
 
$dbname = "data_showcase"; 
 

 
$conn = new mysqli($servername, $username, $password, $dbname); 
 
if ($conn->connect_error) { 
 
    die("Connection failed: " . $conn->connect_error); 
 
} 
 

 
$sql = "SELECT id, name, description, link, date, image, size, picid FROM projects"; 
 
$result = $conn->query($sql); 
 

 
     \t if ($result->num_rows > 0) { 
 
    while($row = $result->fetch_assoc()) { 
 
     echo " <div class='col-md-6 box' id='".$row["id"]."'><div class='project'><div class='image blurry' id='".$row["picid"]."' style='background: url(http://www.all-channels.com/projects/images/".$row["image"].") top center no-repeat;'></div><div class='description-small'> 
 
\t \t 
 
\t \t <h6>".$row["date"]."</h6> 
 
\t \t <h2>". $row["name"]. "</h2> 
 
\t \t <p>". $row["description"]. "</p> \t \t 
 
\t \t <a href=" . $row["link"] . " target='_blank' class='go'>Take a look</a></div></div></div> "; 
 
    } 
 
} else { 
 
    echo "0 results"; 
 
} 
 
\t \t \t \t $conn->close(); 
 
?> 
 

 
      </div> 
 
     </div> 
 
\t \t

需要一種方法來從MySQL數據庫中的自舉的div序列中顯示信息:

  1. 第一和表中的第二行在類col-md-6的兩個鄰居div中顯示。與顯示在另一個DIV
  2. 第三排類COL-MD-12
  3. 然後從接下來的兩行信息後,在兩個山坳-MD-6再次顯示COL-MD-12的一個和第六個是col-md-12。

我已經制作了回聲等寬列中所有結果的代碼。 只是不知道如何讓它顯示在不同的寬度divs以所需的順序。

+0

能否請您補充一點,你已經嘗試的代碼。這樣我們才能瞭解你想要達到什麼以及你嘗試過什麼。 –

+0

以上類型即3行將始終保持不變或可能有所不同 –

+0

剛剛發佈我的代碼,不確定「3行將保持不變」的含義。 – jonkata1985

回答

0

最簡單的方式實現這一目標

$counter = 1; 
while($row = $result->fetch_assoc()){ 
    /* If any particular pattern by which you will get to know whether the code is gonna go in 2 columns (col-md-6) or 1 column (col-md-12) */ 
    /* Based on your code pattern if the loop in divisible by 3 then its col-md-12 else its col-md-6. The following code does the trick for you */ 
    $columnCondition = ($counter % 3 == 0) ? 'col12' : 'col6'; 
    switch($columnCondition){ 
     case 'col6': 
      echo '<div class="col-md-6">'; 
       //Row value 
      echo '</div>'; 
      break; 

     case 'col12': 
      echo '<div class="col-md-12">'; 
       //Row value 
      echo '</div>'; 
      break; 
    } 
    $counter++; 
} 
+0

https://ibb.co/j90Aiw - 這是模式,不需要硬編碼因爲我已經在表中有95行,並且將來我需要幾乎每天都添加新行。 – jonkata1985

+0

@ jonkata1985我已更新我的代碼。請檢查 –

+0

是的,它的工作。非常感謝Channaveer :) – jonkata1985