Hi

I try to display items from a session cart. The problem is that with my code I always display the same item no matter which item I added to the cart. I try now since one week and would be grateful when someone could show me how to do it correct.

My codes:

1st.page where I have the like add the item to the cart:

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

<?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');
?>

File to display items:

<?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 appreciated.

    Write a Reply...