在1个while循环中检索3个唯一记录

时间:2017-07-27 11:30:24

标签: php sql twitter-bootstrap mysqli

我知道这可能有一个非常简单的解决方案,但我似乎无法找到它,我想显示3个不同的记录,但此刻我得到第一个记录3次,然后是第2个,依此类推。

在此处查看结果图片:

enter image description here

特别想要3个不同的结果,一个分布在12个长度的bootstrap div中,两个分布在两个6个长度的bootstrap div上。

$query="SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC";
$result=mysqli_query($conn, $query);

$counter = 0;
$max = 3;

while (($row = mysqli_fetch_array($result)) and ($counter < $max)) {

 echo '<div class="row">';
    echo '<div class="col-md-12">';
      echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
      echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
      echo '<p>'.$row['postDesc'].'</p>';               
      echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
    echo '</div>';
 echo '</div>';     

echo '<div class="row">';
    echo '<div class="col-md-6">';
      echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
      echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
      echo '<p>'.$row['postDesc'].'</p>';               
      echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
    echo '</div>';      
    echo '<div class="col-md-6">';
      echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
      echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
      echo '<p>'.$row['postDesc'].'</p>';               
      echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
    echo '</div>';      
echo '</div>';
$counter++; 

}

2 个答案:

答案 0 :(得分:1)

这是因为你为每个循环做了3个块div echo时间

echo '<div class="row">';
.....   

echo '<div class="row">';
    echo '<div class="col-md-6">';
...      
echo '<div class="col-md-6">';
....

更新

while(($row = mysqli_fetch_array($result)) and ($counter < $max))
{
    if($counter == 0)
    {
        echo '<div class="row">';
        echo '<div class="col-md-12">';
        echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
        echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
        echo '<p>'.$row['postDesc'].'</p>';
        echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
        echo '</div>';
        echo '</div>';
    }
    elseif($counter == 1)
    {
        echo '<div class="row">';
        echo '<div class="col-md-6">';
        echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
        echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
        echo '<p>'.$row['postDesc'].'</p>';
        echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
        echo '</div>';
    }
    else
    {
        echo '<div class="col-md-6">';
        echo '<h3><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h3>';
        echo '<small style="font-size: 12px;color:#ADACAD;"> Posted on '.date('l, jS M Y H:i:s', strtotime($row['postDate'])).'</small>';
        echo '<p>'.$row['postDesc'].'</p>';
        echo '<p><a href="viewpost.php?id='.$row['postID'].'" class="btn btn-default">Read More</a></p>';
        echo '</div>';
        echo '</div>';
    }
    $counter++;
}

答案 1 :(得分:0)

你可以这样写。

你也需要处理你的循环。

$.ajax({
                url: "/Home/getallNews/",
                type: "POST",

                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (response) {
                    alert(response.responseText);
            },
                success: function (response) {
                    alert(response);
                }
            });

        });