简体   繁体   English

用PHP显示来自MySQL的结果的顺序

[英]Sequence in displaying results from mySQL with PHP

        <div class="container-fluid projects-container">
            <div class="container projects-list">
<?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);

        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'>

        <h6>".$row["date"]."</h6> 
        <h2>". $row["name"]. "</h2>
        <p>". $row["description"]. "</p>        
        <a href=" . $row["link"] . " target='_blank' class='go'>Take a look</a></div></div></div> ";
    }
} else {
    echo "0 results";
}
                $conn->close();
?>

            </div>
        </div>

Need a way to display information from mySQL database in bootstrap divs in the sequence: 需要一种方法来按顺序在引导div中显示来自mySQL数据库的信息:

  1. First and the second row of the table to be displayed in two neighbour divs with class col-md-6 . 该表的第一行和第二行将在两个相邻的div中使用col-md-6类显示。
  2. Third row to be displayed in another div with class col-md-12 第三行将在另一个div中显示,其类别为col-md-12
  3. Then the information from the next two rows after col-md-12's one to be displayed in two col-md-6 again and sixth to be col-md-12. 然后,col-md-12之后的下两行的信息将再次显示在两列col-md-6中,第六行显示为col-md-12。

I've already made the code that echoes all results in equal width columns. 我已经制作了在相等宽度的列中回显所有结果的代码。 Just don't know how to make it display them in different width divs in desired sequence. 只是不知道如何使其按所需顺序显示在不同的宽度div中。

Simplest way to achieve this 实现此目的的最简单方法

$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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM