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=s ony258">Ad d</a>
</td>
<td align="center">
<a href="./additem.php?code=s anyo556">A dd</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);
?>
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=s
</td>
<td align="center">
<a href="./additem.php?code=s
</td>
</tr>
</table>
</form>
</body>
</html>
<!--THIS IS THE SECOND FILE-->
<?php
session_start();
if(isset($_GET['code'])){
if(!isset($_SESSION[$_GET[
$_SESSION[$_GET['code']] = 1;
}
else{
$_SESSION[$_GET['code']]++
}
}
print_r($_SESSION);
?>
ASKER
Thanks I'll try this, reply in a minute.
ASKER
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
1 of product1 PHP Notice: Use of undefined constant items - assumed 'items' in C:\php4win\add.php on line 7
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'])){
Change from:
if(!array_key_exists($_GET
to:
if(!array_key_exists($_GET
Oops, sorry :)
ASKER
Is this the correct version?
<?php
session_start();
if(!array_key_exists('item s', $_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";
}
?>
<?php
session_start();
if(!array_key_exists('item
if(array_key_exists('code'
if(!array_key_exists($_GET
$_SESSION['items'][$_GET['
}
else{
$_SESSION['items'][$_GET['
}
}
foreach($_SESSION['items']
print "$qty of $item\n";
}
?>
yup
ASKER
Was the version I posted the correct one?
ASKER
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=sony2 58">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
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=sony2
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.
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.
ASKER
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=sony2 58">Add</a >
Would it be:
<a href="?code=sony258">Add</ a>
<?php
session_start();
if(!array_key_exists('item s', $_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']]++;
}
}
?>
How should I change the href?
<a href="./add.php?code=sony2
Would it be:
<a href="?code=sony258">Add</
<?php
session_start();
if(!array_key_exists('item
if(array_key_exists('code'
if(!array_key_exists($_GET
$_SESSION['items'][$_GET['
}
else{
$_SESSION['items'][$_GET['
}
}
?>
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.
<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.
?php
session_start();
if(!array_key_exists('item
if(array_key_exists('code'
if(!array_key_exists($_GE
$_SESSION['items'][$_GET['
}
else{
$_SESSION['items'][$_GET['
}
}
foreach($_SESSION['items']
print "$qty of $item\n";
}
?>