Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP Radio Array

Posted on 2016-09-26
4
54 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 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 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 109

Accepted Solution

by:
Ray Paseur earned 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 77
binding event to form in window.open 9 39
Responsive Font Size 6 29
JS Event Does not Trigger From File 2 28
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

829 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