Hyperjase Posted January 2, 2012 Share Posted January 2, 2012 Hi all, here's my code: <?php foreach ($_SESSION['topping'] as $value) { echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notopping'>"; foreach ($_SESSION['cupcake'] as $number) { '<option name="notoppings[]" value="'.$number.'">".$number."</option>'; } echo "</select></td></tr>"; } ?> $_SESSION['cupcake'] is a value from either 6, 12, 24 or 36. What I want to do is put them into a drop down box (second foreach) as the value and the displayed value - counting up from 1 (so 1,2,3,4,5,6 or up to 12,24 etc). Also by creating this as an array, does this mean than for each topping (say Vanilla and Chocolate) the value dynamically created can be used on the next page by using $_POST['notoppings'] to display each type (two different numbers - one for Vanilla and one for Chocolate). Does that make sense? Thanks! Jason Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/ Share on other sites More sharing options...
PaulRyan Posted January 2, 2012 Share Posted January 2, 2012 You used foreach() which expects an array as the argument. Use a for() loop like this: <?php foreach ($_SESSION['topping'] as $value) { echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notopping'>"; for ($number=1; $number<=$_SESSION['cupcake']; $number++) { '<option name="notoppings[]" value="'.$number.'">".$number."</option>'; } echo "</select></td></tr>"; } ?> Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/#findComment-1303430 Share on other sites More sharing options...
Hyperjase Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks thats perfect - as I was sat thinking after I'd posted I realised it was a loop that I needed. This brings me on to a slightly different problem. Next step it displays all the selected toppings. I now need to add these figures to each of these. Here's the code that outputs the number of toppings (possible of 6) <?php if (count($_SESSION['topping']) == 1) { echo " cupcakes with the following topping: ".($notopping = $_SESSION['notopping'])." ".($topping = array_pop($_SESSION['topping'])); } else { echo " cupcakes with the following toppings: "; $last = array_pop($_SESSION['topping']); $topping = implode(', ', $_SESSION['topping']) . ' & ' .$last; echo $topping; } $_SESSION['topping2'] = $topping; ?> I was able to insert it on the single selected item but can't seem to get it working with multiple selections. Thanks, Jason Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/#findComment-1303451 Share on other sites More sharing options...
Hyperjase Posted January 2, 2012 Author Share Posted January 2, 2012 Better still as I need the figures with the toppings, how can I combine them to insert into mysql? Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/#findComment-1303452 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 You used foreach() which expects an array as the argument. Use a for() loop like this: <?php foreach ($_SESSION['topping'] as $value) { echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notopping'>"; for ($number=1; $number<=$_SESSION['cupcake']; $number++) { '<option name="notoppings[]" value="'.$number.'">".$number."</option>'; } echo "</select></td></tr>"; } ?> Regards, PaulRyan. I did originally use a while loop which seemed to work fine, I did try the code you gave me but it doesn't work. This code works, it shows as the correct value and displays fine in the drop down. Just one problem, next page I assign the $_POST['notopping'] value to a session -- when I try echoing it out it shows nothing! Here's the code: <?php foreach ($_SESSION['topping'] as $value) { echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notoppingselect'>"; $counter = 1; while ( $counter <= $_SESSION['cupcake'] ) { echo '<option name="notopping[]" value="'.$counter.'">'.$counter.'</option>'; $counter++; } echo "</select></td></tr>"; } ?> Thanks Jason Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/#findComment-1303639 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 I managed to answer my own question on that point! Next issue: I now need to combine two arrays to show as one. I have one array as 1, 2, 3 (number of each) and an array of Vanilla, Chocolate & Choc Orange. Now this code displays the topping flavour as I've just shown it, I need to insert the first array so it shows 1 x Vanilla, 2 x Chocolate & 3 x Choc Orange. I've tried a few things but can't get it to work! Here's the code the outputs the topping flavour array: <?php if (count($_SESSION['topping']) == 1) { echo " cupcakes with the following topping: ".($topping = array_pop($_SESSION['topping'])); } else { echo " cupcakes, ".$_SESSION['delcol'].", with the following toppings: "; $last = array_pop($_SESSION['topping']); $topping = implode(', ', $_SESSION['topping']) . ' & ' .$last; echo $topping; $_SESSION['topping2'] = $topping; echo "<br />";?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/254216-create-dynamic-dropdown-from-_session-variable/#findComment-1303645 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.