0

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).

Display page

P.S. Don't worry about closing the section & article with the close tags because it's already done.

5
  • Select query in your function and return array, then with foreach loop display data from function in this case array. Commented Mar 6, 2017 at 18:31
  • @Mario How do you mean? Provide example please. Commented Mar 6, 2017 at 18:32
  • 1
    Your foreach loop 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? Commented Mar 6, 2017 at 18:34
  • @David Sorry, I'm a little confused - I'm quite unfamiliar with PHP so I'm not sure about what I'm doing so I need some examples. Commented Mar 6, 2017 at 18:36
  • Convert 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. Commented Mar 6, 2017 at 18:38

3 Answers 3

4

You're looping over the array, but you're not doing anything with the values:

foreach($products as $ap)
{
    $name = $ap['pro_name'];
    $description = $ap['descr'];
    $price = $ap['rrp'];
}

Then after the loop, when the values are the last of the array, you finally use them once:

<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>

Instead, use the values in the loop so you can use each of them:

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>
<?php
} // end the loop after using the values
Sign up to request clarification or add additional context in comments.

Comments

3

It seems, that your HTML- generation should be inside the loop, not outside:

<?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>
          <?php 
     } ?>

Comments

0
<article class = "main_area2">
   <h1 class = "food_h1">Menu</h1>
   <br>
   <section class = "menu_section">
       <?php foreach(get_product_details() as $ap): ?>
       <div class = "section_4">
           <h1 class = "food_h2"><?php echo $ap['pro_name']; ?></h1>
           <br>
           <img src = "img/products/hassellback_pot.jpg" class = "img_dis">
           <br>
           <h1 class = "food_h2"><?php echo $ap['descr'] ?></h1>
           <br>
           <h1 class = "food_h2">£ <?php $ap['rrp']; ?></h1>
           <button class = "basket_btn" type = "button"> Buy </button>
       </div>
       <?php endforeach; ?>
   </section>
</article>

1 Comment

This answer looks like an accurate end result. It would be clearer if you describe why the asker's code isn't working and what this code does differently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.