?
Solved

Looping through session files?

Posted on 2005-02-25
13
Medium Priority
?
267 Views
Last Modified: 2006-11-17
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
Comment
Question by:EmailSurfer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13401557
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
 

Author Comment

by:EmailSurfer
ID: 13401656
Thanks I'll try this, reply in a minute.
0
 

Author Comment

by:EmailSurfer
ID: 13401691
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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 360 total points
ID: 13401711
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
 
LVL 7

Expert Comment

by:sjohnstone1234
ID: 13401721
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
 
LVL 7

Expert Comment

by:sjohnstone1234
ID: 13401725
Oops, sorry :)
0
 

Author Comment

by:EmailSurfer
ID: 13401743
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13401758
yup
0
 

Author Comment

by:EmailSurfer
ID: 13401778
Was the version I posted the correct one?
0
 

Author Comment

by:EmailSurfer
ID: 13402565
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13402775
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
 

Author Comment

by:EmailSurfer
ID: 13402905
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13403332
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question