Jump to content

radio button input, associative array


robincham

Recommended Posts

Using this sample array linking a product code (RP-151, RP-171, etc) with a corresponding price (90, 100, 150)

 

$fplan = array (

RP-151 => '90',

RP-171 => '100',

RP-172 => '150'

);

 

how do I set a radio button input value to both product code and price from the array? 

 

I want users to pick ONE product/price combo (hence, radio button, not checkboxes), then display on another page the selected product code and price separately, as in "You've chosen product X and the price is Y".

 

Thanx in advance for feedback.

 

Robincham

Link to comment
Share on other sites

$fplan = array (
    'RP-151' => '90',
    'RP-171' => '100',
    'RP-172' => '150'
);

$keys = array_keys($fplan);
foreach ($keys as $key) {
   echo '<input type="radio" name="plan" value="', $key, '">';
}

if (isset($_POST['plan'])) {
    $plan = $_POST['plan'];
    if (isset($fplan[$plan])) {
        echo 'You\'ve chosen product ', $plan, ' and the price is ', $fplan[$plan];
    }
}

Link to comment
Share on other sites

Ignace:

 

So, I set the array, then use

 

$keys = array_keys($fplan);

foreach ($keys as $key) {

  echo '<input type="radio" name="plan" value="', $key, '">';

}

 

to setup each separate radio button, then use 

 

if (isset($_POST['plan'])) {

    $plan = $_POST['plan'];

    if (isset($fplan[$plan])) {

        echo 'You\'ve chosen product ', $plan, ' and the price is ', $fplan[$plan];

    }

}

 

to display chosen radio button input wherever necessary, correct?

 

Thanx in advance,

 

Robincham

Link to comment
Share on other sites

Ignace:

 

OK, needed to parse your

 

$keys = array_keys($fplan);

foreach ($keys as $key) {

  echo '<input type="radio" name="plan" value="', $key, '">';

}

 

to see it will list the radio buttons one-after-the-other from the foreach loop.  However, I must place each radio button in a div that contains an image of the product and a brief product description.  How may I display each radio button separately?

 

Robincham

Link to comment
Share on other sites

$keys = array_keys($fplan);
foreach ($keys as $key) {
   $radio_button[$key] =  '<input type="radio" name="plan" value="', $key, '">';
}

 

$radio_button[product_id] where product_id is the id of the product you want to get the radio button for will have the radio button HTML.

Link to comment
Share on other sites

Ignace, Ialsoagree:

 

Thanx for your help, but I cannot display the radio buttons from a foreach loop.  I need to display each separately with other information (a product image and product description) in individual div's.  Ideas welcome on how to take array input from the single button allowed in a list of radio button, then display array 'key/value' info individually.  Carrying input from page to page is already handled via sessions.

 

Robincham

Link to comment
Share on other sites

Without more specific information on how your data is organized and being gathered, we can't give you a specific solution.

 

In general though, you need to start a loop when you have all the information you need accessible. If you're getting info from a mysql database, this might be using a while loop and doing $row = mysql_fetch_assoc($result) or something similar.

 

Whatever the case, use my previous strategy to build each div 1 at a time, store them in an array, and when you need to echo them/write them to the browser, you have them ready to go.

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.