Jump to content

Using buttons to loop to the next element from the data fetched from database


dangar

Recommended Posts

I want to have a next button on my Productdetails.php page that would show the next image fetched from the database using while loop. Below is the  productdetails.php  page shows details of a particular product.
I am using simple php format, How can I go about it? I have come this far at this point.

<div class = "uniqueproduct"> 
        <?php 
            if (isset($_GET['id']))
            {
                $id = $_GET['id'];
                $query1 = mysqli_query($conn, "select * from products where PRODUCT_ID = $id");
                while ($row1 = mysqli_fetch_array($query1)) {
        ?>

            <div class = "singleproduct">
            <?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($row1['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/>'; ?>
            </div>

            <div class = "productName">
            <?php echo $row1['PRODUCT_NAME']; ?>
            </div>

            <div class = "productDescription">
            <?php echo $row1['PRODUCT_DESCRIPTION'];?>
            </div>

            <div class = "productPrice">
            <?php echo $row1['PRODUCT_PRICE']; ?>
            </div>


            <?php
            }
            }
            ?>


        <button class = "btnPicture"> ..............</button>
    </div>

 

Link to comment
Share on other sites

If you're not doing so already, you should look into using prepared statements. You'll want to avoid use the raw GET variable, at least one that's not tested, in a query. Using the raw (untested) value makes the query susceptible to SQL injection attacks, among other issues.

 

Assuming $_GET['id'] should contain a number, you can make sure it does with ctype_digit(). More information can be found here:

http://php.net/manual/en/function.ctype-digit.php

Link to comment
Share on other sites

As for your question, I assume that the button will lead to the same productdetails.php page. You could run another query to get the ID for the next record. Then just create a button (or link) to pass the value.

 

Note: MySQL doesn't always return the values the same order. The order it uses doesn't always make sense. For the next-record feature to work, you'll want to look into sorting (ORDER BY clause).

Link to comment
Share on other sites

You state that you want to use a button to get your next record and that you want to use a while loop.  Uh...  that won't work since your button is on the client and your while loop would be on the server.

 

As indicated above you will display a single record that has a key in it.  Somehow you will need to pass that key back to the server/script and use it to query for the 'next' record, whatever it is.  There would be no loop involved.

Link to comment
Share on other sites

PHP variables need to be enclosed with PHP tags. You could try something like the following:

<a href="Productdetails.php?id=<?php echo $id + 1; ?>">Next</a>

Or

<a href="Productdetails.php?id=<?php echo $id++; ?>">Next</a>

Note: you'll want to double check the ID exists before displaying the Next link.  :happy-04:

Link to comment
Share on other sites

Note: you'll want to double check the ID exists before displaying the Next link.  :happy-04:

 

Correct. There is no guarantee that the next record will be +1. A better solution is to provide the actual ID of the next record. Use a query such as this:

 

SELECT * FROM table WHERE id >= [passed_id] ORDER BY id LIMIT 2

Use a 0 for the first request to start the process. Then use the first record to display the current record. If a 2nd record is returned, use its id in the link/button to get the next record. If no second record is returned, then don't provide a link/button for the next record.

Link to comment
Share on other sites

Correct. There is no guarantee that the next record will be +1. A better solution is to provide the actual ID of the next record. Use a query such as this:

SELECT * FROM table WHERE id >= [passed_id] ORDER BY id LIMIT 2

 

Good point. I was only thinking about the last record. There's a good chance that there will be gaps in the middle, eventually.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.