Link to home
Start Free TrialLog in
Avatar of EmailSurfer
EmailSurfer

asked on

Looping through session files?

Hello,

I wrote this simple script to create a shopping cart, at the moment there are only two products, but this will increase to ten just for the example.

Could any demonstrate how to loop through the sessions?

Thank you

<html>
<head>
  <title>Example Script</title>
</head>

<body>

<div align="center">
<form method="get">
<table border="1" width="40%">
   <tr>
      <td align="center">
         Product 1
      </td>
      <td align="center">
         Product 2
      </td>
   </tr>
   <tr>
      <td align="center">
         <a href="./additem.php?code=sony258">Add</a>
      </td>
      <td align="center">
         <a href="./additem.php?code=sanyo556">Add</a>

      </td>
   </tr>
</table>
</form>

</body>
</html>

<!--THIS IS THE SECOND FILE-->
<?php
session_start();

if(isset($_GET['code'])){
   if(!isset($_SESSION[$_GET['code']])){
      $_SESSION[$_GET['code']] = 1;
   }
   else{
      $_SESSION[$_GET['code']]++;
   }
}

print_r($_SESSION);
?>
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

I'd put your items in a separate array within $_SESSION which will make it easy to separate from any other stuff you put in there. I've also shown how to loop through selected items and display them:

?php
session_start();
if(!array_key_exists('items', $_SESSION)) $_SESSION['items'] = array();

if(array_key_exists('code', $_GET)){
   if(!array_key_exists($_GET['code'], $_SESSION[items])){
      $_SESSION['items'][$_GET['code'] = 1;
   }
   else{
      $_SESSION['items'][$_GET['code']++;
   }
}

foreach($_SESSION['items'] as $item =>$qty) {
  print "$qty of $item\n";
}
?>
Avatar of EmailSurfer
EmailSurfer

ASKER

Thanks I'll try this, reply in a minute.
There was one error displayed:

1 of product1 PHP Notice: Use of undefined constant items - assumed 'items' in C:\php4win\add.php on line 7
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That looks like the missing quotes around "items" on line... well, it looks like 6 on Squinky's, but might be 7 in your local copy?

Change from:

if(!array_key_exists($_GET['code'], $_SESSION[items])){

to:

if(!array_key_exists($_GET['code'], $_SESSION['items'])){
Oops, sorry :)
Is this the correct version?

<?php
session_start();
if(!array_key_exists('items', $_SESSION)) $_SESSION['items'] = array();

if(array_key_exists('code', $_GET)){
   if(!array_key_exists($_GET['code'], $_SESSION['items'])){
      $_SESSION['items'][$_GET['code']] = 1;
   }
   else{
      $_SESSION['items'][$_GET['code']]++;
   }
}

foreach($_SESSION['items'] as $item =>$qty) {
  print "$qty of $item\n";
}
?>
Was the version I posted the correct one?
Thanks

Could I ask finally, rather than using a href as below, where the user clicks the link to add a item and another page is displayed, and then we have to link back to the orginal page.

<a href="./add.php?code=sony258">Add</a>      

Could I use something like this statement, and still send the value to additem.php?

<INPUT type=image height=22 width=88
                        alt="Add to Basket"  
                        src="addtobasket.gif"
                        value="sony258" border=0
                        name=code>

Thank you
I think image input tags are ugly and have varying support. You're better off just using images and wrapping them in an a tag using the same URL as the text link. You should also quote ALL your attributes.

A much neater way of getting back to the original page is not to leave it in the first place, i.e. process the item addition on the original page rather than on a separate one. The trick is to process all the things that the form might do before generating any output, and use redirects to prevent refresh reposting issues.
Thanks, if I placed this php code in the main file, with all the html.

How should I change the href?

<a href="./add.php?code=sony258">Add</a>    

Would it be:

<a href="?code=sony258">Add</a>

<?php
session_start();
if(!array_key_exists('items', $_SESSION)) $_SESSION['items'] = array();

if(array_key_exists('code', $_GET)){
   if(!array_key_exists($_GET['code'], $_SESSION['items'])){
      $_SESSION['items'][$_GET['code']] = 1;
   }
   else{
      $_SESSION['items'][$_GET['code']]++;
   }
}
?>
That will work. You can also get PHP to generate the current script reference for you:

<a href="<?php echo $_SERVER['PHP_SELF']; ?>?code=sony258">Add</a>

If you put the adding script before any output HTML, it will process the items added before they are displayed, so the page will be working with up-to-date data. Otherwise you'll find that it lags one step behind the current page.