Forum Moderators: coopster

Message Too Old, No Replies

Adding session variables

in a photo cart system

         

davesp

3:38 pm on Feb 19, 2004 (gmt 0)

10+ Year Member



I'm writing a 'lightbox/cart' function to a site with a lot of images. Mainly to teach myself PHP.

I have the basics working so that I can display a form with the selected thumbnail image and it's name at the top of the form. Each image page has a link which says something like (the variables are different for each page):


<?php

session_start();

$_SESSION['imagename'] = 'Nutricia booklet 201';
$_SESSION['imagefilename'] = '<IMG SRC="/Nutricia/thumbnails/tnNutricia booklet 1.jpg">';
$_SESSION['gallery'] = 'Nutricia';
$_SESSION['time'] = time();

echo '<br /><a href="/form/enquiryform.php?' . SID . '">Inquire about using this illustation</a>';
?>

and on the form page:


<?php echo $_SESSION['imagefilename'];?>
<?php echo $_SESSION['imagename'];?>

etc.

above the form and used in the code to send details in an email with formmail:

This seems to work so far...My questions are:
How do I:

Code the ability to add another image and image name to the form?

(i've been thinking along the lines of


$_SESSION['imagefilename'] = '[imagename]. Nutricia booklet 201';

On each image page to add the existing variable to the new one (concanternate?) or am I barking up the wrong tree (or just barking)

and/or,

How should I kill the session if I just want the enquiry form sent before the user returns and chooses another image to send an enquiry about?

Thanks in advance

jatar_k

4:26 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What about just dropping it into an array in the session. You could add a third session var to keep track of the number of images.

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

$_SESSION['imagefilename'][$next] = "someimgfilename";
$_SESSION['imagename'][$next] = "someimgname";

That's just off the top of my head. You can use

echo "<pre>";
print_r($_SESSION);
echo "<pre>";

to view the contents of your session. The pre tags make it so you don't have to view source to see it properly formatted.

Then when you mail it you can use a foreach to roll through the images in the session.

davesp

5:29 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Thanks for that idea. I seem to have got the code below to do what I want. I have a link to an enquiry page which has a form which displays the thumnail images and lists the titles and then sends them with the enquiry data to me. There is also a link to a reset page which destroys the session. Is this the wrong way to proceed for any reason. It is for a non secure light box function on a image gallery.


$_SESSION ['imagename'] = "$_SESSION[imagename] <br > New Image Title";
$_SESSION['imagefilename'] = "$_SESSION[imagefilename] <IMG SRC='newimagefile.jpg'>";

echo '<br /><a href="/form/enquiryform.php?' . SID . '">Equire about using this illustation</a>';

jatar_k

5:36 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If it works and you have tested it then it is a fine way to proceed. :)

davesp

6:34 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



Thanks for the encoragement! The only small point I can't figure is if my user browses back to the reset page more than once it will not function as it is cached, the same with the image pages but less onf a problem as I don't want them to add the same image twice. If i refresh the page everthing works fine.

Is there a do not cache page locally command?

jatar_k

6:44 pm on Feb 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try this
Cache Issues with Dynamic Pages [webmasterworld.com]

there are some good links in there too