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
https://forums.phpfreaks.com/topic/47009-solved-associative-arrays/
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>

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

Archived

This topic is now archived and is closed to further replies.

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