Jump to content

Very Basic: arrays and forms


Endraca

Recommended Posts

Firstly:

Please excuse me if my post is a bit awkward, newer posted anything of this kind before.

 

This is part of a university assignment I have to do,not very complicated stuff, basically we have created 3 arrays , one for $drinks,$desserts, and $meals, for example:

 

$drinks= array (
'Spring Water'=>'7',
'Coke'=>'6',
'Coffee'=>'4',
'Tea'=>'5',
'Hot Chocolate'=>'8'
);

 

with this array we have created 3 drop down lists like this:

 

<?php  
print "<tr><td>Select your drink</td><td><select name='drink'>";
foreach($drinks as $drink=>$price2)
{
print "<option value='$drink'>$drink - R $price2</option>";
}
?>
print "</select>

 

This information is submitted to my next page where I have to add the prices of the 3 items...and there is where my problem lies

I seem to be able to use the "$drink" bit of the array and not the "$price2" bit of it. This is probably caused by my lack of understanding of arrays, but basically I want to know why, for example

 

print "{$_POST['drink']} {$_POST['price2']} </br>";

 

doesn't print the price with the drink I chose

and I can't seem to use $price2 in any calculations

So my main question after all of this is how can I get "$price2" in my next php page so I can do calculations with it?

Link to comment
Share on other sites

if its part of a university assignment shouldnt you be working out for yourself instead of asking for help on here??

 

to help you on your way - a select box will only pass one piece of information, which is what you have told it is the "option value", in your case the value of $drink, not the value of $price2

Link to comment
Share on other sites

Our lecturer said if we get stuck we are allowed to ask here (he named this forum), though if I am still stuck by tomorrow I will go and ask him

 

So what you are saying is that if I need both values (which I do) I can't use a select box to get both values?

Personally I'd rather have used check boxes for this exercise, but we are told to use a select box though, so there has to be another way for me to get the other value ($price2).

 

There was a point where I was able to print for example "Tea 5" but I still can't do calculations with "$price2". Guess I'll just ask the lecturer tomorrow, luckily it only needs to be in by Friday.

 

Link to comment
Share on other sites

if you want to pass both pieces of information across using the select box you could use

<?php
print "<tr><td>Select your drink</td><td><select name='drink'>";
foreach($drinks as $drink=>$price2)
{
print "<option value='$price2/$drink'>$drink - R $price2</option>"; //value now equals both pieces of info separated by a /
print "</select>";
?>

 

which when passed across requires the use of explode to get both pices away from each other such as

<?php
$food = $_POST['drink'];
$pieces = 	explode('/',$food); //re-separate the data
echo "$pieces[0] - $pieces[1]"; //print them out or use them where you need them
?>

 

hope that helps a bit more

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.