TurtleInAsia Posted April 26, 2009 Share Posted April 26, 2009 Hi, I am trying to improve my old code that controls a shopping list, so that instead of having lots of code it loops until the end is detected. Can someone help me out :-) I have a list of 5 session variables like this: $_SESSION['List']['1']['Title'] $_SESSION['List']['2']['Title'] $_SESSION['List']['3']['Title] $_SESSION['List']['4']['Title'] $_SESSION['List']['5']['Title'] What I currently do is a if else statement like this if (!isset($_SESSION['List']['1']['Title'])) { $_SESSION['List']['1']['Title'] = $_POST['title']; } elseif (!isset($_SESSION['List']['2']['Title'])) { $_SESSION['List']['2']['Title'] = $_POST['title']; } elseif (!isset($_SESSION['List']['3']['Title'])) { $_SESSION['List']['3']['Title'] = $_POST['title']; } elseif (!isset($_SESSION['List']['4']['Title'])) { $_SESSION['List']['4']['Title'] = $_POST['title']; } elseif (!isset($_SESSION['List']['5']['Title'])) { $_SESSION['List']['5']['Title'] = $_POST['title']; } However this is not good because it is hard coded to a limit of 5. What i want to do it a method of automatically incrementing the number and looping until the number is empty, which will also allow me to have more than 5 entries. Hope that makes sense, any help greatly appreciated :-) Turtle Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/ Share on other sites More sharing options...
trq Posted April 26, 2009 Share Posted April 26, 2009 It doesn't really make much sense considering your storing the same data ($_POST['title']). Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/#findComment-819379 Share on other sites More sharing options...
mikesta707 Posted April 26, 2009 Share Posted April 26, 2009 that doesnt really loop? wouldnt you want something like $i = 0; while ($i < /* whatever your limit is */ ){ $_SESSION['List'][i]['Title'] = $_POST['title']; i++; } Or is that what you had before and what you want to change it from? Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/#findComment-819396 Share on other sites More sharing options...
TurtleInAsia Posted April 26, 2009 Author Share Posted April 26, 2009 Yes sorry that is my old code I would like to change. I see your new code recommendation and it looks much easier to work with, is there a way to not specify the end number but stop if it reaches a value that is not set ? I am going to try a few things out and will update my results here. Thanks again for the example. Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/#findComment-819512 Share on other sites More sharing options...
papaface Posted April 26, 2009 Share Posted April 26, 2009 $i = 0; $count = count($_SESSION['List']); while ($i < $count ){ $_SESSION['List'][$i]['Title'] = $_POST['title']; i++; } Something like that? Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/#findComment-819561 Share on other sites More sharing options...
kenrbnsn Posted April 26, 2009 Share Posted April 26, 2009 That's basically a "for" loop <?php $count = count($_SESSION['List']); for ($i=0;$i<$count;$i++) $_SESSION['List'][$i]['Title'] = $_POST['title']; ?> Ken Link to comment https://forums.phpfreaks.com/topic/155677-create-a-dynamtic-_session-variable-name-by-incremental-number/#findComment-819566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.