Nick Singh Sahota Nick Singh Sahota - 1 year ago 121
PHP Question

Retrieve 3 unique records within 1 while loop

I know this might have a very simple solution but I just cannot seem to find it, I want to display 3 different records but at the moment I am getting the 1st record 3 times, then the 2nd and so on.

See image of results here. I specifically want 3 different results, one spread across the 12 length bootstrap div and two spread across the two 6 length bootstrap divs.

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

}

Answer Source

this is because you are doing 3 block div echo time for each loop

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

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

updated:

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++;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download