Hi

I try to make a php session cart. The problem is that with my code I always display the same "model" no matter which model I add to the cart. I already tried to solve it since four days and getting nowhere.

First page:

<html>
<body>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div class=logofix>

</div>
<?php
require_once('navbar2.php'); 
include('connection.php');
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}



$result = mysql_query('SELECT * FROM femalemodels_t ORDER BY m_name');
echo '<div class="scrollImagesView">';

echo '<div class="table">';
echo "<table id=\"my_table\" border=1>"; 
echo '<tr class="t_headings">';

   echo '<th>Image</th>'; 
  echo '<th>ID</th>';  
echo '<th>Name</th>';
echo '<th>Address</th>'; echo '<th>Phonenumber</th>'; echo '<th>Email</th>'; echo '<th>Size</th>'; echo '<th>Eyecolor</th>'; echo '<th>Haircolor</th>'; echo '<th>Bust</th>'; echo '<th>Waist</th>'; echo '<th>Hips</th>'; echo '<th>Edit</th>'; echo '<th>Delete</th>'; echo '<th>Add to readinglist</th>'; echo "</tr>"; while($row = mysql_fetch_array($result)){ echo '<tr>'; echo '<div class="tableContent">'; echo '<td>';?> <img src="<?php echo $row['path'];?>" height="142" width="100"> <?php echo "</td>"; echo '<td>'.$row['ID'].'</td>';
echo '<td>'.$row['m_name'].'</td>';
echo '<td>'.$row['m_address'].'</td>'; echo '<td>'.$row['m_phonenumber'].'</td>'; echo '<td>'.$row['m_emailaddress'].'</td>'; echo '<td>'.$row['m_size'].'</td>'; echo '<td>'.$row['m_eyecolor'].'</td>'; echo '<td>'.$row['m_haircolor'].'</td>'; echo '<td>'.$row['m_bust'].'</td>'; echo '<td>'.$row['m_waist'].'</td>'; echo '<td>'.$row['m_hips'].'</td>';
echo '<td><a href="editfemalemodels.php?ID='.$row['ID'].'"><img src="EditIcon2.png" alt="edit link"/></a></td>'; echo '<td ><a style="color:red;" href="deletefemalemodels.php?ID='.$row['ID'].'"><img src="DeleteIcon2.png" alt="delete link"/></a></td>'; echo '<td ><a style="color:red;" href="readinglist.php?ID='.$row['ID'].'"><img src="AddIcon2.png" alt="create link"/></a></td>'; echo '</div>'; echo "</tr>"; } echo '</table>'; echo '</div>'; ?> </body> </html>

Session file code:

<?php

ob_start();
session_start();
include('connection.php'); 
// 2) When the linked to page is visited, add the specified item to the cart with a quantity of one...

// a) Validate the id received from the link.
$id = isset($_GET['ID']) ? (int)$_GET['ID'] : 0; // zero is not used/valid for a database auto-increment value that would be used as an id
if($id < 1){
    // not a valid id
    echo "The requested id is not valid";
} else {
    // the id is valid

// b) If the cart doesn't exist, create an empty one.
if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

// c) Add the item to the cart with a quantity of one, if it is not already in the cart or increment the item quantity if it is already in the cart.
if(!isset($_SESSION['cart'][$id])){
    // id is not in the cart, create an entry
    $_SESSION['cart'][$id] = 0; // initialize to zero (the next statement will set it to one)
}
$_SESSION['cart'][$id]++; // add one to the quantity (if the item was already in the cart with some quantity, this will increment the quantity)
}

// At this point, you are done with the add to cart task.
// You would either link/redirect back to the page that displays all the items
// or you could display the cart contents, by retrieving all the id's from the cart (see the array_keys() and implode() functions), then retrieve the information from the database table and loop over it to display it
// or you could incorporate this code into the main page, making a single page.
// for demo purposes, just display the session data -
header('location:viewreadinglist.php');
?>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
session_start();
include('connection.php');
  var_dump($_SESSION['cart']);
  echo '<table >'; 

 foreach($_SESSION['cart'] as $value){ 
         $query = 'SELECT * FROM femalemodels_t WHERE ID='.$value;     
$result = mysql_query($query);
$row = mysql_fetch_array($result); echo '<tr>'; echo '<td>';?> <img src="<?php echo $row['path'];?>" height="142" width="100"> <?php echo "</td>"; echo '<td>'.$row['ID'].'</td>';
echo '<td>'.$row['m_name'].'</td>';
echo '<td>'.$row['m_address'].'</td>';
echo '<td>'.$row['m_phonenumber'].'</td>'; echo '<td ><a style="color:red;" href="deletemodel.php?ID='.$row['ID'].'"><img src="DeleteIcon2.png" alt="delete link"/></a></td>'; /*echo '<td ><a style="color:red;" href="lab20_AddBookmark.php?ID='.$row['ID'].'"><img src="icon_bookmark.gif" alt="delete link"/></a></td>';*/ echo "</tr>"; } echo '</table>'; ?>

Help would be very much appreciate🙂

    As far as I can make out, you're trying to use the quantities stored in the cart and trying to use them for the ids. Instead of using the ids for the ids. Is the "same 'model'" that shows up each time the one with an id of 1?

    foreach($_SESSION['cart'] as $id=>$value){ // I'd say "$quantity" instead of "$value" myself because that would be more informative

      Hi

      yeah the "the same model that shows up is always id of 1. I just tried what you suggested, but there is no difference. Always add the same model with id=1.

        You'd have to do more than what I suggested. If you're still using the $value in your loop then you're still using the $value.

          Can you give more informations on what I have to do so that I don't always display the same item. Do I have to rewrite my code or just the part with the $value?

            Hi,
            When we are stuck in codding then here you need to make some trouble shooting way like in the line
            <php>
            echo("print_session".$SESSION['cart'][$id]);
            $
            SESSION['cart'][$id]++;
            echo("print_session".$SESSION['cart'][$id]);
            </php>
            also check session array by
            print_r($
            SESSION['cart']);
            on getting page so it checked what is stored in this array.

            that will help you find it is working or not also where you running queries
            you need to print queries
            <php>
            $query = 'SELECT * FROM femalemodels_t WHERE ID='.$value;

            echo($query);
            </php>
            it help what you are getting from it.
            please run these both and let me know what you getting we may able to help you more.
            Thanks

              You'd use [font=monospace]$id[/font], since that's where the ID is being put.

                MightyMicky;11046543 wrote:

                Can you give more informations on what I have to do so that I don't always display the same item. Do I have to rewrite my code or just the part with the $value?

                Weedpacket has explained that you are confusing the id of the product to be added with the quantity of the product to be added. More specifically, you are treating the submitted quantity as though it were the unique id of a product.

                  Write a Reply...