nethken nethken - 5 months ago 11
PHP Question

PHP - Query and loop to show different data

I'm struggling to show different news in my page. I want to show each of the news in 1 div and the limit is 4 news. Example, i have 4 different news and i want of every each of it to display separately in divs. Im new to html and php. Can someone give me ideas what the best loop and queries for this?

to easily understand what i want to do here is the picture.
enter image description here

here is what i im doing. i think im lost.
enter image description here

here is my php code for the news..

<div class="content">
<div class="container">
<div class="row">

<div class="col-xs-12 col-md-9">
<div class="fix leftbar floatleft">
<div class="fix left_sidebar">
<div class="news">
<h2><i class="fa fa-newspaper-o"></i> Latest News</h2>
<hr class="carved">

<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)) {
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>



<a href="#" class="view_news">View More News</a>

</div>
</div>
</div>
</div>

Answer

Simply like your first loop... with better format

   <?php
   include_once('connection.php');
   $sql ="SELECT * FROM news ORDER BY news_id ASC";
   $result = mysqli_query($con, $sql);

   while($row = mysqli_fetch_array($result)){
       $title = $row['news_title'];
       $date = $row['news_date'];
       $content = $row['news_content'];
   ?>
   <div class="fix single_news">
       <a href="#"><?php echo $title; ?></a>
       <div class="single_image">
             <img src="img/coveredcourt.jpg" alt="court">
        </div>
        <p><?php echo $date; ?></p>
        <p><?php echo $content; ?></p>
   </div>
    <hr class="carved"/>
  <?php
    }
  ?>   

Your loop is taking all article, so you need only one loop like this. Inside you just need to format only one article, all others will take the same format.