Results 1 to 8 of 8
  1. #1
    Join Date
    May 2013
    Posts
    14

    Unanswered: Shopping cart output repeating first entry

    I have set up a shopping cart on a website that I'm building. I can easily add items, remove items and clear the cart. What I'm having a problem with is actually displaying the items in the cart, or more to the point the relevant database entries.

    The PHP session variable holds numbers which relate to ID numbers used to identify items in a database. I need the cart to then show an image, a name, a delete option and the id number. When displaying the cart if there is more than one item the image and name are always the first item in the cart repeated for each entry, while the id number does change.

    This makes me think that I am only performing one query when in fact I need to run the query inside the foreach loop of my first code segment below. As a bit of a newby to PHP, I'm not too sure the best way to go about this. I've just added a recordset via Dreamweaver (second code).

    Could someone point in the best direction for guiding me to solve my little problem.

    Many thanks

    Shopping Cart Display:

    PHP Code:
     <?php 
        $cart 
    $_SESSION['cart'];
      if (!
    $cart) {echo "<div class='dialogue_box_inside_info_request'>
     <div class='dialogue_box_image'>
        <img src='images/basket_empty_dialogue.png' width='618' height='264' />
        </div>
    </div>"
    ;}
      else
      {

          
    $array explode(','$_SESSION['cart']);

      echo 
    "<table width='835' border = '0'>";
      foreach(
    $array as $cartId) {
      
    $ship_name $row_ships['ship_name'];
      
    $ship_image $row_ships['image'];
      echo 
    "<tr>";
      echo 
    "<td width='110' height='58' class='table_text'><img src='images/ships/$ship_image'width='83' height='53' /> </td>";
      echo 
    "<td width='620' height='58' class='table_text'>$ship_name</td>";
      echo 
    "<td width='35' height='58' class='table_text'><a href='cart.php?action=delete&ship_id=$cartId'><img src='images/trash.png' width='31' height='42' /></a></td>";
      echo 
    "<td height='58'>$cartId</td>";
      echo 
    "</tr>";
      } 
      echo 
    "</table>";

      }
       
    ?>

    Recordset placed at top of code

    PHP Code:
    <?php
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      if (
    PHP_VERSION 6) {
        
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }

    mysql_select_db($database_ships$ships);
    $query_ships "SELECT ship_id, ship_name, image FROM ship_infomation";
    $ships mysql_query($query_ships$ships) or die(mysql_error());
    $row_ships mysql_fetch_assoc($ships);
    $totalRows_ships mysql_num_rows($ships);
    ?>

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    What are you storing in $_SESSION['cart']?
    Code:
    <?php print_r($_SESSION); die(); ?>
    George
    Home | Blog

  3. #3
    Join Date
    May 2013
    Posts
    14
    My Cart is as follows:

    PHP Code:
    <?php
    require_once('Connections/ships.php');

    // Start the session
    session_start();
    // Process actions
    $cart $_SESSION['cart'];
    $action $_GET['action'];
    switch (
    $action) {
            case 
    'add':
                if (
    $cart) {
                            
    $items explode(',',$cart);
                            if (
    count($items) > 5)
                {
                
    header("Location: shipinfo_full.php") ;
                }
                else
                {
                
    $cart .= ','.$_GET['ship_id'];
                
    header("Location: shipinfo_added.php?ship_id=" .  $_GET['ship_id']) ;
                }} else {
                
    $cart $_GET['ship_id'];
                
    header("Location: shipinfo_added.php?ship_id=" .  $_GET['ship_id']) ;}
                break;
            case 
    'delete':
                if (
    $cart) {
                
    $items explode(',',$cart);
                
    $newcart '';
                foreach (
    $items as $item
                    {
                    if (
    $_GET['ship_id'] != $item
                        {
                        if (
    $newcart != ''
                            {
                            
    $newcart .= ','.$item;
                            } 
                            else 
                            {
                            
    $newcart $item;
                            }
                        }
                    }
                    
    $cart $newcart;
                
    header("Location: shipinfo.php?ship_id=" .  $_GET['ship_id']) ;    }
                break;

            }
            
    $_SESSION['cart'] = $cart;
            

    ?>

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Where does $row_ships get assigned a value?
    George
    Home | Blog

  5. #5
    Join Date
    May 2013
    Posts
    14
    er..... I assumed from the query I run at the top of the page. It's always the first thing I add to the cart that is repeated. And here was me being pleased that I done all this bit without your help :-(

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    So, once, at the top of the page, you are setting the value of this variable. It is getting a single row's value from your results...

    Agree?
    George
    Home | Blog

  7. #7
    Join Date
    May 2013
    Posts
    14
    Agree. That's why my logic was to move it into the foreach loop

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    But what you've moved in to your foreach loop is
    Code:
    $ship_name = $row_ships['ship_name'];  
    $ship_image = $row_ships['image'];
    Which is assigning the value of $row_ships to variables on each pass.
    However on each pass the value of $row_ships is not being changed!

    Have a play with this [untested] code:
    Code:
    <?php
    
    // Assigning a value to your session variable
    $_SESSION['cart'] = "1,2,3,4,5";
    
    // Implementing this in your SQL statement (filter to only return those items in the cart)
    // !!! IMPORTANT !!! Remember to check against SQL injection!!
    $sql = "SELECT ship_id, ship_name, image FROM ship_infomation WHERE ship_id IN (" . $_SESSION['cart'] . ") ORDER BY ship_name;";
    
    // Show the SQL statement
    echo $sql . "<br /><br /><br />";
    
    // Connect to database
    mysql_select_db($database_ships, $ships); 
    // Perform query and assign results to $ships variable
    $ships = mysql_query($sql, $ships) or die(mysql_error());
    
    // For each row in the recordset
    while($row = mysqli_fetch_array($ships))
    {
      // Output the values to screen
      echo $row['ship_id'] . " " . $row['ship_name'] . " " . $row['image'] . "<br />";
    }
    ?>
    George
    Home | Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •