Brent
Brent

Reputation: 31

Get the ID of my clicked product

I'm new to this site and wondering if somebody could help. I'm creating a website using bootstrap. I added some products in my DB and have used a while loop to display them on the page.

If you click on one of the products it takes you too a new page which should display all the information from only that product.

if ($select_db) {

                $SQL = "SELECT * FROM products";
                $result = mysql_query($SQL);
                ?>

                <?php
                $c = 0;
                $id = -1; // The counter
                $n = 3; // Each Nth iteration would be a new table row
                while ($db_field = mysql_fetch_assoc($result)) {

                    $itemName = $db_field['name'];
                    $itemDescription = $db_field['description'];
                    $itemPrice = $db_field['price'];
                    $myPic = $db_field['image_name'];

                    if ($c % $n == 0 && $c != 0) { // If $c is divisible by $n...
                        echo '<div class="row" ></div>';
                    }
                    $c++;
                    $id++;
                    ?>

                    <div class="col-md-4 col-sm-4" style="background-color:lavender;" id = 1>
                        <a href="productInfo.php"> 
                            <p> <?php echo $c ?> </p>
                            <h2> <?php echo $itemName ?> </h2>
                            <p><img class="img-responsive" img src= '<?php echo $myPic ?>' alt="Oops, Image cannot be found!" height="300" width="300"/></p>
                            <h3><?php echo $itemDescription ?></h3>
                            <p><?php echo $itemPrice ?></p>
                            <div id="selector" class="btn-group">
                                <button type="button" class="btn btn-primary" id="<?php $id ?>">Add</button>
                            </div>
                            <?php
                            $productsArray[] = array(
                                "id" => $id,
                                "itemName" => $itemName,
                                "itemDescription" => $itemDescription,
                                "price" => $itemPrice
                            );
                            //$_SESSION['sessionArray']=$productsArray;
                            ?>
                        </a>
                    </div>

                    <?php

Now when I click on one of the columns it takes me to productInfo.php. I'm stuck as to how to display the product that is clicked?

I have added a variable $id which is the same as the array, example array [0] has id of 0, array [1] has id of 1. I think I'm trying to say if ID = 0 then display the results in the array at place [0]. I'm not sure if this helps?

I tried to display just the $id for now on productInfo.php but it only shows the last $id in the array

 <?php
 session_start();
 if (isset($_SESSION["id"])) {
 $newID = $_SESSION["id"];
 echo $newID;
 }
 ?>

I know this because it doesn't know which one I'm selecting. Am I making this too complicated?

Any help would be appreciated greatly!

Upvotes: 0

Views: 1730

Answers (1)

Filthy_Rich
Filthy_Rich

Reputation: 665

Within your while loop I would add:

<a href="product_page.php?id='".$id."'">View More</a>

This would concatenate the value of $id++ into the href under each item. Then all you need to do is create a landing page for product_page.php, define the $id on that page again and pull the data for that product from the db. You can do this without using any arrays.

Edit:

You would define $id on your product_page.php using $_GET['id'], as it is in the url, supplied by your href above ^

A good practice to get into whilst in the development stages would be to echo $id; to see if it is echoing the correct data from your while loop. If it echo's the correct $id then you can send the $id to the url through the href.

Let me know if this works or not.

Pulling data from db of your ID:

$sql = "SELECT * FROM table WHERE id='".$myID."'";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$data1 = $row['whatever'];

echo $data1;

Upvotes: 1

Related Questions