velvetmonster
velvetmonster

Reputation: 87

foreach loop to populate multiple radio buttons from a database in a php string

UPDATE: Thanks to the answers provided by SeanWM and Styphon, I offer the full, working code to those who are looking for a similar answer:

<? 
if (isset($_GET['id'])) {
include "connect_to_mysql.php";
$id= preg_replace('#[^0-9]#i','',$_GET['id']);
$sql=mysql_query("SELECT * FROM angler3 WHERE id='$id' LIMIT 1");
$clientCount=mysql_num_rows($sql);

    if($clientCount>0){ 

 while($row=mysql_fetch_array($sql)){
 $anglerAvidity=$row["anglerAvidity"];
 $anglerFishMode=$row["anglerFishMode"];

 $vals = array('novice', 'occasional', 'avid');
 $radio_opts = "";
foreach ($vals as $val)
{
$checked = ($val==$anglerAvidity) ? ' checked="checked"' : '';
$radio_opts .= "<input type=\"radio\" name=\"anglerAvidity\" value=\"{$val}\" {$checked}> {$val}\n";

}
$vals2 = array('boat', 'kayak or canoe', 'float tube', 'on ice', 'rod and reel', 'trap nets', 'other');
$radio_opts2 = "";
foreach ($vals2 as $val2)
{
$checked2 = ($val2==$anglerFishMode) ? ' checked="checked"' : '';
$radio_opts2 .= "<input type=\"radio\" name=\"anglerFishMode\" value=\"{$val2}\"{$checked2}> {$val2}\n";
}
$content='
<h3>Please click on any buttons that are not answered. Your responses are shown below, and new or updated responses will be saved. </h3>
<hr>
<form enctype="multipart/form-data" action="thankyouAnglerYouRevise.php?id='.$id.' " method="POST"> 
<table>
<tr ><td>I consider myself a/an:</td>
<td colspan="2" align=left>'.$radio_opts .'
</td></tr>
<tr><td colspan="3" style="border-bottom: 1px solid #BBB; height: 10px;"></td></tr>
<tr ><td colspan="3" >My normal mode of fishing is:</td></tr>
<tr ><td colspan="3">'.$radio_opts2.'
</td></tr>
</table>
<br>
<input class="button" value="Submit" type="submit">
</form>
';
require_once ('index2.html.php');
?>

ORIGINAL QUESTION:

I am trying to write an edit page using php/mysql, and have this foreach code that works:

$vals = array('novice', 'occasional', 'avid');
$radio_opts = "";
foreach ($vals as $val) {
    $checked = ($val == $anglerAvidity) ? ' checked="checked"' : '';
    $radio_opts .= "<input type=\"radio\" name=\"groupname\" value=\"{$val}\"{$checked}>   {$val}<br />\n";

}

HTML:

<tr ><td>I consider myself a/an:</td>
            <td colspan="2" align=left>'. $radio_opts .'
            </td></tr>

The radio buttons are printed and the correct value is selected.

However, I have many questions on the page, and all are called out in a string using this type of thing:

$title='Survey<br>';
$content='
<tr ><td>I consider myself a/an:</td>
<td colspan="2" align=left>'. $radio_opts .'
</td></tr>

This is all working fine, but when I try this:

$vals = array('novice', 'occasional', 'avid');
$radio_opts = "";
foreach ($vals as $val) {
    $checked = ($val == $anglerAvidity) ? ' checked="checked"' : '';
    $radio_opts .= "<input type=\"radio\" name=\"groupname\" value=\"{$val}\"{$checked}>   {$val}<br />\n";

}
$vals2 = array('kayak or canoe', 'float tube', 'on ice', 'rod and reel', 'trap nets', 'other');
$radio_opts2 = "";
foreach ($vals2 as $val2) {
    $checked2 = ($val2 == $anglerFishMode) ? ' checked="checked"' : '';
    $radio_opts2 .= "<input type=\"radio\" name=\"groupname\" value=\"{$val2}\"{$checked2}> {$val2}<br />\n";
}

with HTML

    <tr ><td>I consider myself a/an:</td>
    <td colspan="2" align=left>'.$radio_opts .'
    </td></tr>

and

    <tr ><td colspan="3" >My normal mode of fishing is:</td></tr>
    <tr ><td colspan="3">'.$radio_opts2.'
    </td></tr>

both radio button groups are displayed, but only the second group's radio button is populated from the db. If I switch the order around and call radio_opts2 first, then radio_opts is populated and radio_opts2 displays as an unselected radio group. That's why I think it's almost working if I could get the foreach to end somehow. If I end the first foreach group or call the second foreach group within the content variable I get an unexpected T_STRING error. I knew this call was bad code, but for the sake of completeness, tried it anyway.

It's got to be some dumb thing I'm doing, not ending the first foreach loop, but when I try to use endif or break I will either get nothing echoed at all for the first radio group or throw an unexpected else error message. I have reviewed all of the manuals and spent hours searching this forum for help, but I still can't figure out how to write this code.

Thanks much for any help!

Upvotes: 0

Views: 3924

Answers (2)

Styphon
Styphon

Reputation: 10447

The problem is the name of groupname. For your second set of radio buttons you need to change this or the form sees them as one group of radio buttons. Change your code to:

$radio_opts2 .= "<input type=\"radio\" name=\"groupname2\" value=\"{$val2}\"{$checked2}> {$val2}<br />\n";

Upvotes: 1

SeanWM
SeanWM

Reputation: 16989

Everything looks fine. You do, however, have the same name field for each set of radio buttons. As it stands now, both are name="groupname". You should try giving the groups different names:

$vals = array('novice', 'occasional', 'avid');
$radio_opts = "";
foreach ($vals as $val) {
    $checked = ($val == $anglerAvidity) ? ' checked="checked"' : '';
    $radio_opts .= "<input type=\"radio\" name=\"fishing_level\" value=\"{$val}\"{$checked}>   {$val}<br />\n";
    // ------------------------------------------------^

}
$vals2 = array('kayak or canoe', 'float tube', 'on ice', 'rod and reel', 'trap nets', 'other');
$radio_opts2 = "";
foreach ($vals2 as $val2) {
    $checked2 = ($val2 == $anglerFishMode) ? ' checked="checked"' : '';
    $radio_opts2 .= "<input type=\"radio\" name=\"fishing_mode\" value=\"{$val2}\"{$checked2}> {$val2}<br />\n";
    // ------------------------------------------------^
}

Upvotes: 1

Related Questions