Jump to content

echo array depending on form selection, i'm stuck.


bine.heckmann

Recommended Posts

Hello everyone, i'm kinda stuck here although i had this working before,  i don't know what's wrong here.

 

Basically i've got a form with a drop-down menu, that menu has 3 values, x,y and z. Now that form is working with post, sending me to the next page where i have a set of arrays and a echo which should print some text based on the option chosen from the drop down menu.

 

my code in the form is this:

 

<select name="value1" id="value1">
        <option value="x">x</option>
        <option value="y">y</option>
        <option value="z">z</option>
      </select>

 

the array on the next page:

 

$arrayvalue = array("x"=>"test1", "y"=>"test2", "z"=>"test3");

 

and the echo:

<?php echo $arrayvalue["value1"];?>

 

That leaves me with an empty page. What am i doing wrong here ? am i missing something ?

 

any help is greatly appreciated.

 

Sabrina

Link to comment
Share on other sites

You're assigning the literal value 'value1' to the index of the array you're trying to echo, when you need to assign the value of $_POST['value'] to it instead. Try this, it should do what you need.

 

<?php
$arrayvalue = array("x"=>"test1", "y"=>"test2", "z"=>"test3");
$value1 = $_POST['value1'];
echo $arrayvalue[$value1];
?>

Link to comment
Share on other sites

You're assigning the literal value 'value1' to the index of the array you're trying to echo, when you need to assign the value of $_POST['value'] to it instead. Try this, it should do what you need.

 

<?php
$arrayvalue = array("x"=>"test1", "y"=>"test2", "z"=>"test3");
$value1 = $_POST['value1'];
echo $arrayvalue[$value1];
?>

 

This did work for me, thank you so much !

 

I would have another question about something which i'm not quite sure about if this would work at all, i have no code to show or something so i'm trying to explain what i'm trying to achieve the best i can.

 

I'll choose a rental scenario, let's say i have 2 Drop-Down menus, 1st one shows the rental time for example: 1 day, 2 days, 3 days and so on.  The 2nd Drop-Down Menu shows whatever is for rent, let's take cars, so the menu shows, BMW 3 series and Mercedes C-Class. Now i have prices attached to those cars like that

<?php
$arrayvalue = array("BMW"=>"50",  "Mercedes"=>"60");
$value1 = $_POST['value1'];
echo $arrayvalue[$value1];
?>

 

So that will show me the Price for 1 Day of Rental time, now the 1st drop down menu is set to 3 days or whatever, how could i link those menus together like, the echo will check first how many days have been selected and will show the appropiate price for the selection, like BMW selected which is 50bottle caps or whatever, and time selected is 3 Days, so it should show 150 Bottle caps.

 

Is that even possible without usage of a DB ?

 

Thanks in advance,

 

Sabrina

 

Link to comment
Share on other sites

One approach, assuming that the days field is called days and the car is called value1, and assuming that you can't just use simple match to get the price, like $days * daily_price:

 

$arrayvalue[1] = array("BMW"=>"50",  "Mercedes"=>"60");
$arrayvalue[2] = array("BMW"=>"100",  "Mercedes"=>"120");
$arrayvalue[3] = array("BMW"=>"125",  "Mercedes"=>"175");

$days = $_POST['days'];
$value1 = $_POST['value1'];

echo $arrayvalue[$days][$value1];

Link to comment
Share on other sites

For completeness, you can turn it around.  One way may work better depending on what else you do with the array:

 

$arrayvalue['BMW'] = array(1=>"50", 2=>"100", 3=>"125");
$arrayvalue['Mercedes'] = array(1=>"60", 2=>"120", 3=>"175");


$days = $_POST['days'];
$value1 = $_POST['value1'];

echo $arrayvalue[$value1][$days];

Link to comment
Share on other sites

For completeness, you can turn it around.  One way may work better depending on what else you do with the array:

 

$arrayvalue['BMW'] = array(1=>"50", 2=>"100", 3=>"125");
$arrayvalue['Mercedes'] = array(1=>"60", 2=>"120", 3=>"175");


$days = $_POST['days'];
$value1 = $_POST['value1'];

echo $arrayvalue[$value1][$days];

 

Works like a charm, thanks a lot!

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.