Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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);
?>
0
EmailSurfer
Asked:
EmailSurfer
  • 6
  • 5
  • 2
1 Solution
 
Marcus BointonCommented:
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";
}
?>
0
 
EmailSurferAuthor Commented:
Thanks I'll try this, reply in a minute.
0
 
EmailSurferAuthor Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Marcus BointonCommented:
oops, missed some quotes:

?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";
}
?>
0
 
sjohnstone1234Commented:
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'])){
0
 
sjohnstone1234Commented:
Oops, sorry :)
0
 
EmailSurferAuthor Commented:
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";
}
?>
0
 
Marcus BointonCommented:
yup
0
 
EmailSurferAuthor Commented:
Was the version I posted the correct one?
0
 
EmailSurferAuthor Commented:
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
0
 
Marcus BointonCommented:
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.
0
 
EmailSurferAuthor Commented:
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']]++;
   }
}
?>
0
 
Marcus BointonCommented:
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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now