Jump to content

Create dynamic dropdown from $_SESSION variable


Hyperjase
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.