caughtinweb Posted July 29, 2009 Share Posted July 29, 2009 I am new to sessions and arrays, as will become obvious. What is supposed to happen is: customer is on the page of a bead they like, a session has been started, they press a button called "add this to my want list", which takes them to the "want list" page, where the list of beads that they are interested is supposed to accumulate. They are then supposed to be able to surf back to other bead pages in the site and add more beads to their list. What is happening instead is: The "want list" page only has the most recent bead in it. The list never grows beyond one item. If they click another bead from another page, it replaces the previous bead in the list, instead of being the next bead in the list. 1st page has <?php session_start(); ?> command at the top, and shows the bead. information.inc.php (an include file called from the 1st page) <p class="orderNumber">Order # <?php echo $mainImage ?></p> <p class="redinfo"><?php echo $row['height'] ?>mm x <?php echo $row['width'] ?>mm<br /> <?php echo $row['quantity'] ?> in a pack<br /> $<?php echo $row['price'] ?> per pack<br /> <?php echo $row['pack'] ?> packs in stock<br /> <form id="form1" name="form1" method="post" action="../includes/wantlist.php"> <input type="submit" name="wantlist" value="add to want list"> <?php //put the bead into the list $_SESSION['beadiwant'] = $mainImage; ?> </form> <p> if some of the details are missing, email me at: <br /> <a href="mailto:[email protected]?subject=My want list&body=">[email protected]</a><br /> Mr. Bee can help you fill in the blanks</p> <p class="fadedblue">These beads are in limited quantities or may have been sold</p> wantlist.php <?php session_start(); // checks that form has been submitted and that name is not empty /* if ($_POST && !empty($_POST['bead number goes here'])) { // set session variable $_SESSION['name of bead i guess'] = $_POST['name of bead i guess']; } */ ?> //................................. <body> <?php echo '<p>I want...</p>'.'<p>'. $_SESSION['beadiwant'].'</p>'; // if the array $wantlist does not exist, then define it as an array and stick the 'beadiwant' into it if (!isset($_SESSION['wantlist'])) { $wantlist = array(); $wantlist[] = $_SESSION['beadiwant']; echo "Want list wasn't set yet."; $_SESSION['wantlist'] = $wantlist; } // the $wantlist array must persist accross sessions // so store the $wantlist array inside a session variable if (isset($_SESSION['wantlist'])) { $wantlist[] = $_SESSION['wantlist']; $wantlist[] = $_SESSION['beadiwant']; $_SESSION['wantlist'] = $wantlist; // print out the customer's want list foreach ($wantlist as $item) { echo $item.'<br />'; } } ?> </body> I'm at a loss. I thought maybe I have to embed one array within another one, but that just managed to make the array more complex without fixing the problem. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/167899-solved-problem-with-sessions-and-forgetful-array/ Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 <a href='addbead.php'>add this bead</a> <?php session_start(); $_SESSION['wantlist'][] = $beadiwant; ?> that's pretty much it. No conditions, nothin'. Idea is that if the user clicks the button, add the bead to the list. Of course, first code (link) will be more complex, depending on how you are passing $beadiwant to the 2nd page. But principle is still the same. Quote Link to comment https://forums.phpfreaks.com/topic/167899-solved-problem-with-sessions-and-forgetful-array/#findComment-885605 Share on other sites More sharing options...
caughtinweb Posted July 30, 2009 Author Share Posted July 30, 2009 Thank you Crayon Violent for your helpful answer. That tip worked great. The array is now remembering and I'm learning a lot more about arrays. Quote Link to comment https://forums.phpfreaks.com/topic/167899-solved-problem-with-sessions-and-forgetful-array/#findComment-886608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.