Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Radio Array

Posted on 2016-09-26
4
Medium Priority
?
87 Views
Last Modified: 2016-10-11
Hi,

I have a DB query thats pulling many rows from a table, each row has an id and an email address, firstname and lastname, I'm trying to print out the query showing each row, then once i have this let the user select 1 of 3 radios buttons,
1. for wipes
2. nappies
3. both

once they have selected for each row then submit the form, if they have choosen radio button 1 i want to run one selection of code, if they have selected radio button 2 for nappies i want to run another selection or code for those rows.

the code below shows what i'm printing out for each row from the db, but how do I do a foreach when submited and break them apart, i'll need the ID or the email address passed through to know who going for what option ?, they can only select one option per row so i'm using radio buttons.

<form name='form1' id='form1' method='post' action=''>
        <?php while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){ ?>
        <div>
          <div style="float:left; width:160px"><?php echo $row['child_firstname'] . " " . $row['child_lastname']; ?></div>
          <div style="float:left; width:100px">
             <input type="radio" name="item[<?php echo $row['id']; ?>]" value="wipes">
          </div>
          <div style="float:left; width:100px">
            <input type="radio" name="item[<?php echo $row['id']; ?>]" value="nappies">
          </div>
          <div style="float:left; width:100px">
            <input type="radio" name="item[<?php echo $row['id']; ?>]" value="both">
          </div>
          <div style="clear:both"></div>
        </div>
        <?php } ?>

Open in new window


Thanks Simon
0
Comment
Question by:pc-buddy
  • 2
4 Comments
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 41817270
Not sure I have understand the requirement exactly but you can try this.
$item = isset($_POST['item']) ? $_POST['item'] : false;
if (is_array($item)) {
  foreach($item as $rowselection) {
    switch($rowselection) {
      case 'wipes':
        handleWipes();
      break;
      case 'nappies':
        handleNappies();
      break;
      default:
        handleBoth();  
      break;
    }
  }
}
function handleNappies() 
{
   // Handle Nappie code here
}

function handleWipes() 
{
   // Handle Wipes code here
}

function handleBoth() 
{
   // Handle Both code here
}

Open in new window

0
 

Author Comment

by:pc-buddy
ID: 41818613
Hi Julian,

I'll go have a play, thanks :)
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 41828405
For this demo I stripped out all of the parts that are not relevant to processing an array of radio buttons in a post request.   Feel free to experiment with it on my server.  HTH, ~Ray
https://iconoun.com/demo/temp_pc_buddy.php
<?php // demo/temp_pc_buddy.php
/**
 * https://www.experts-exchange.com/questions/28972537/PHP-Radio-Array.html
 */
error_reporting(E_ALL);

// SHOW THE POST ARRAY
var_dump($_POST);

// USE THE POST ARRAY
if (!empty($_POST))
{
    foreach ($_POST['item'] as $key => $value)
    {
        echo PHP_EOL . "<br>POST ITEM $key POINTS TO '$value' ";
    }
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<EOD
<form method='post'>

<input type="radio" name="item[1]" value="wipes">
<input type="radio" name="item[1]" value="nappies">
<input type="radio" name="item[1]" value="both">
<br>

<input type="radio" name="item[2]" value="wipes">
<input type="radio" name="item[2]" value="nappies">
<input type="radio" name="item[2]" value="both">
<br>

<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
 

Author Closing Comment

by:pc-buddy
ID: 41837996
Awesome help, thanks guys
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question