justinh Posted January 21, 2009 Share Posted January 21, 2009 <?php session_start(); $item = $_GET['item']; function outputItem($item){ for($i = 1; isset($_SESSION['$item2']); ++$i){ $item2 = "item".$i; echo $_SESSION['$item']."<br />"; } $_SESSION['$item2'] = $item; echo $item; ?> I'm trying to make an ajax shopping cart, so i need to check if a session is set, if it is, display it, check if the next session is set, display it, so on and so on until the session is not set, this is when we assign the $_GET string to the unset session, and display it. I'm wondering If my code is correct. My server is down at the moment, so I can't upload it to see if it would work. Sorry if I didn't explain my intent well enough. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 Okay I tested the script, didn't work. I modified it a little and came up with this. <?php session_start(); $_SESSION['item1'] = "Duck Hunt"; function outputItem($item){ for($i = 1; isset($_SESSION['$item.$i']); ++$i){ echo $_SESSION['$item.$i']."<br />"; ++$t; } //Once second parameter in for loop returns false $_SESSION['$item.$i'] = $item; echo $item; } $item = "Awesome Job!"; outputItem($item); ?> This is just outputting "Awesome Job!". Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 Sorry found a mistake <?php session_start(); $_SESSION['item1'] = "Duck Hunt"; function outputItem($item){ for($i = 1; isset($_SESSION['item.$i']); ++$i){ echo $_SESSION['item.$i']."<br />"; } //Once second parameter in for loop returns false $_SESSION['item.$i'] = $item; echo $item; } $item = "Awesome Job!"; outputItem($item); ?> still doesn't work though =/ Quote Link to comment Share on other sites More sharing options...
cytech Posted January 21, 2009 Share Posted January 21, 2009 Hello, Its only outputting "Awesome Job!" becuase your passing that string to the function and then echoing it out. Nothing is changing to the $item variable it self. Give this a run, see if it helps a bit. session_start(); $_SESSION['items']['item1'] = "Duck Hunt baby!"; $_SESSION['items']['item2'] = "Duck Hunt #3!"; function outputItem($item){ $total_items = count($_SESSION['items']); // total number of items $next_item_id = $total_items+1; // will be 3 in this example for($i = 1;$i<=$total_items;$i++){ echo $_SESSION['items']['item'.$i]."<br />"; } // store the new item $_SESSION['items']['item'.$next_item_id] = $item; echo $item."<br />"; } $item = "Awesome Job!"; outputItem($item); Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 Awesome thanks, works like a charm. =) Quote Link to comment Share on other sites More sharing options...
printf Posted January 21, 2009 Share Posted January 21, 2009 You shouldn't do it that way, you should use an array of items... <?php session_start (); // if no items are set or exist! if ( ! isset ( $_SESSION['items'] ) ) { // you should also check if $_GET is valid $_SESSION['items'] = array ( $_GET['item'] ); } else { // $SESSION['items'] is already set so check if // the item already exists before adding it... if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) ) { // you should also check if $_GET is valid $_SESSION['items'][] = $_GET['item']; } } // or displaying items if ( isset ( $_SESSION['items'] ) ) { // you should also check if $_GET is valid foreach ( $_SESSION['items'], AS $item ) { echo $item . "\r\n<br />\r\n"; } } else { echo 'Sorry no items exist!'; } ?> Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 thanks for the help guys here it is http://www.wmptest.com/hrfinal/additem.html tell me what you think <?php session_start(); function outputItem($item){ $total_items = count($_SESSION['items']); // total number of items $next_item_id = $total_items+1; // will be 3 in this example for($i = 1;$i<=$total_items;$i++){ echo $_SESSION['items']['item'.$i]."<br />"; } // store the new item $_SESSION['items']['item'.$next_item_id] = $item; echo $item."<br />"; } if(isset($_GET['clearlist'])){ session_destroy(); } else { $item = $_GET['item']; outputItem($item); } ?> Quote Link to comment Share on other sites More sharing options...
cytech Posted January 21, 2009 Share Posted January 21, 2009 Looks very good! I would definitly institute the concept that printf showed, checking to see if the item is already within the session - at that point you can set a quantity, so instead of showing "milk, cheese, milk" you could show "milk (x2), cheese". The next step would be to start setting up a database isntead of storing your items in a session. Sessions can easily be destoyed (as you showed in your code haha). Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 Found a problem =( If you add eggs, and then try to add it again it doesn't submit. I know this doesn't have to do with the session script above, im pretty sure it has to do with the event im using. <select id="item" onChange="addProduct();"> Is there a certain event type you suggest? Quote Link to comment Share on other sites More sharing options...
cytech Posted January 21, 2009 Share Posted January 21, 2009 Add a button that says "Add to Cart" and put an onClick on it Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 http://www.wmptest.com/hrfinal/additem.html woot =) I love AJAX Quote Link to comment Share on other sites More sharing options...
cytech Posted January 21, 2009 Share Posted January 21, 2009 Sweet! Now setup a quantity system Quote Link to comment Share on other sites More sharing options...
justinh Posted January 21, 2009 Author Share Posted January 21, 2009 Okay, I'm having a little trouble with the counting system. You can view the app here: www.wmptest.com/hrfinal/additem.html I'm just happy I got this far lol... Could anyone tell me why it is displaying funky? <?php session_start (); if(isset($_GET['clearlist'])){ session_destroy(); }else{ if ( ! isset ( $_SESSION['items'] ) ) { $_SESSION['items']=array($_GET['item'], "1" ); } else { if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) ) { $_SESSION['items'][] = $_GET['item']; } else{ $_SESSION['items'][$_GET['item']][0] = $_SESSION['items'][$_GET['item']][0] + 1; } } if ( isset ( $_SESSION['items'] ) ) { foreach ( $_SESSION['items'] AS $item ) { echo $item." x".$item[0]."<br />"; } } else { echo 'Sorry no items exist!'; } } ?> Thanks for the help in advance Quote Link to comment Share on other sites More sharing options...
justinh Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks for your input prinf.. I think I'm starting to understand how your code works. I'm trying to add a qty system now using your code, but I'm stuck on the display part. Could you please take a look at my code and tell me if im heading in the right direction. <?php session_start(); if ( ! isset ( $_SESSION['items'] ) ) { $_SESSION['items'] = array ( $_GET['item'], 1); } else { if ( ! in_array ( $_GET['item'], $_SESSION['items'] ) ) { $_SESSION['items'][] = $_GET['item']; $_SESSION['items'][$_GET['item']][] = 1; } else { $_SESSION['items'][$_GET['item']][] = $_SESSION['items'][$_GET['item']][] + 1; } } if ( isset ( $_SESSION['items'] ) ) { // Displaying Items } else { } Thanks, Justin Quote Link to comment 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.