robincham Posted April 3, 2010 Share Posted April 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/ Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 $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]; } } Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036533 Share on other sites More sharing options...
robincham Posted April 3, 2010 Author Share Posted April 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036544 Share on other sites More sharing options...
robincham Posted April 3, 2010 Author Share Posted April 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036565 Share on other sites More sharing options...
ialsoagree Posted April 3, 2010 Share Posted April 3, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036566 Share on other sites More sharing options...
ialsoagree Posted April 3, 2010 Share Posted April 3, 2010 Just a quick aside, since I can't edit my post, it would probably be a good idea to initialized the array before you start using it, so before the loop... $radio_button = array(); Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036573 Share on other sites More sharing options...
robincham Posted April 3, 2010 Author Share Posted April 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036595 Share on other sites More sharing options...
ialsoagree Posted April 4, 2010 Share Posted April 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197483-radio-button-input-associative-array/#findComment-1036650 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.