I'm trying to display some items on webpage from a database. I have a function that loops through the data stored in the database. Currently it displays the last item that appears within the database rather than the first or anything else. I have tried ordering the items by the id but it gives the same result. I have also tried to display individual items of the array but it just displays the first character of the last item within the database.
display items function:
<?php
function get_product_details()
{
global $db;
$ret = array();
$sql = "SELECT * FROM Products";
$res = mysqli_query($db, $sql);
while($ar = mysqli_fetch_assoc($res))
{
$ret[] = $ar;
}
return $ret;
}
?>
Area for displaying items:
<article class = "main_area2">
<h1 class = "food_h1">Menu</h1><br>
<section class = "menu_section">
<?php
$products = get_product_details();
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
}
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
This is the current display and as you can see the first and last are exactly the same (the second to eighth are currently hard-coded).
P.S. Don't worry about closing the section & article with the close tags because it's already done.

foreachloop doesn't do anything except repeatedly over-write variables. So after that loop those variables will always have the last values from the loop, guaranteed. Should the use of those variables also be inside the loop?mysqli_fetch_assoc($res)to$ret = mysqli_fetch_array($res);to get a list of all items you dont need while loop in a fuction.