Jump to content

[SOLVED] Associative arrays


cyrilsnodgrass

Recommended Posts

Hi guys,

 

Noob to php and I have a problem with a college assignment.

 

Basically I am writing a shopping cart from scratch. I can get my first item looking good but I can never add to my cart. If I choose another item it seems to overwrite the first one. Can anyone suggest what I am doing wrong please ?????

 

Each page calls this function :-

 

function session_check()
{
   /** First use session_start function to (a) check for session existence and (b) start a session if  **/
   /** no session exists                                                                                                      **/

   session_start();

}

Then my form to add to the cart calls this function :-

function addtocart()
{

/** Add to cart                                                                       **/
  
   $item = $_POST[item];

   If (!$_SESSION[cart][$item] ) 
   {
     $_SESSION[cart][$item] = 1;
   }
   Else 
   {
     echo "in here";
     $_SESSION[cart][$item] = $_SESSION[cart][$item]+1;
   }

/** just for testing print out all values in array **/
   foreach ($_SESSION[cart] as $key => $value)
   {
      echo "$key >= $value";
   }

   
/** Build string to display cart items                                                 **/

   $disp_str = "<h2>Added to your cart: ";
   $disp_str .= $_POST[item];
   $disp_str .= "</h2><hr><p>";
   $disp_str .= "<table><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>";

/** Loop to retrieve and build table values from cart                                 **/
   foreach ($_SESSION[cart] as $key => $value)
   {  
      $disp_str .= "<tr><td><img src='images/";
      $disp_str .= $key;
      $disp_str .= ".jpg' alt='";
      $disp_str .= $key;
      $disp_str .= "'></img></td>";
      $disp_str .= "<td>$key qty = ";
      $disp_str .= "$value </td>";
      $disp_str .= "<td><input type='submit' value='Remove One'></input></td>";
      $disp_str .= "</table>";
   }
   echo $disp_str;
}


/**                                                                                                     **/
/** End Of Library Functions                                                                    **/
/**                                                                                                     **/

 

Any help would be really appreciated !!!!! :)

 

EDITED BY WILDTEEN88: Please use the code tags (


) when including code in your posts. This helps to identity text from code

Link to comment
Share on other sites

Always use quoted strings for array keys, not just the string

 

Check if the item is in the cart using isset() eg

 

<?php
session_start();

function addtocart()
{

/** Add to cart                                                                       **/
  
   $item = $_POST['item'];

   if (!isset($_SESSION['cart'][$item]) ) 
   {
     $_SESSION['cart'][$item] = 1;
   }
   else 
   {
     echo "in here";
     $_SESSION['cart'][$item]++;
   }

/** just for testing print out all values in array **/
   echo '<pre>', print_r($_SESSION['cart'], true), '</pre>';

   
/** Build string to display cart items                                                 **/

   $disp_str = "<h2>Added to your cart: ";
   $disp_str .= $_POST['item'];
   $disp_str .= "</h2><hr><p>";
   $disp_str .= "<table><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>";

/** Loop to retrieve and build table values from cart                                 **/
   foreach ($_SESSION['cart'] as $key => $value)
   {  
      $disp_str .= "<tr><td><img src='images/";
      $disp_str .= $key;
      $disp_str .= ".jpg' alt='";
      $disp_str .= $key;
      $disp_str .= "'></img></td>";
      $disp_str .= "<td>$key qty = ";
      $disp_str .= "$value </td>";
      $disp_str .= "<td><input type='submit' value='Remove One'></input></td>";
      $disp_str .= "</table>";
   }
   echo $disp_str;
}


/**
** End Of Library Functions
**/

if (isset($_POST['item'])) addtocart();   	

?>
<form method='post'>
<input type="text" name="item"><input type="submit" name="action" value="Submit">
</form>

Link to comment
Share on other sites

hi folks,

 

sorry for not being able to get this to work, but following the good advice given has not made any difference - the bleedin' cart only ever holds one item and when I try to add another item it seems to replace the previous one or maybe the session has been lost or something ?!?!?!?!!

 

One thing I was wondering - I am using IE6. Are there any php environment type variables I should be manipulating ?

 

Must admit I am getting to like php.

 

Cheers guys,

 

Cyril Snod

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.