简体   繁体   中英

Display the latest 3 results from database

I need to display the latest 3 articles individually from the database (title, description, content and image).

$title_query = "SELECT title, description, content, image FROM articles ORDER BY id DESC LIMIT 10";
$title_result = mysqli_query($con, $title_query) or die(mysqli_error($con));

while($row = mysqli_fetch_array($title_result)) 
{
$title = $row['title'];
$description = $row['description'];
$content = $row['content'];
$image = $row['image'];
}

echo $title;

Currently this only gets the latest one. How can I set variables for the second and third latest one? So I can have:

$title1
$title2
$title3

etc.

Thanks

the way you construct the code, it is supposed to echo out the 3rd item's tittle. so what you should do is go through loop and keep adding them to array like below:

and since you want latest 3 items, so wht not you limit it to only 3 items like below:

$title = array();

$title_query = "SELECT title, description, content, image FROM articles ORDER BY id DESC LIMIT 3";
$title_result = mysqli_query($con, $title_query) or die(mysqli_error($con));

while($row = mysqli_fetch_array($title_result)) {
    $title[] = $row['title'];
}

print_r($title);

The request what you have made to display the latest articles individually based on the id published into the DB. You need to modify the loop what you have provide over to the question.

If you want to display multiple products outside of any looping statement you have to make it store the values in an array() and after that you can use anywhere else outside the loop .

In order to make the variable as an array you can initialize an array() either in any of the two methods as you prefer.

Method: 1 - You can initialize the array() when the query returns the TRUE value.

Method: 2 - You can initialize the array() at the top of the page too.

Initializing is based on the convenience of the developer who is developing the code.

The basic Strategy for the MYSQL Limit Statement.

MySQL: SELECT LIMIT Statement

Description: The MySQL SELECT LIMIT statement is used to retrieve records from one or more tables in MySQL and limit the number of records returned based on a limit value.

Syntax:

The syntax for the SELECT LIMIT statement in MySQL is:

SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
LIMIT row_count

And hence you need to modify the query like follows so that you can display the latest three articles as per your requirement.

<?php
$title_query = "SELECT `title`, `description`, `content`, `image` FROM `articles` ORDER BY `id` DESC LIMIT 0,3";
$title_result = mysqli_query($con, $title_query) or die(mysqli_error($con));
$counting = $title_result->num_rows;
if($counting==0)
{
    echo 'No Datas found';
}
else
{
// This part will execute if the count is greater than 0
$row=[];
while($fetch = $title_result->fetch_assoc()) {
    $row[] = $fetch;
}
}

// here you can loop through to display the data.
foreach ($row as $key => $single_data) {
?>
    Title: <?php echo $single_data['title']; ?>
    Description: <?php echo $single_data['description']; ?>
    Content: <?php echo $single_data['content']; ?>
    Image: <img src="PATH to FOLDER WHERE YOU HAVE SAVED THE IMAGE ALONG WITH <?php echo $single_data['image']; ?>" />
<?php   
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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