How to use while loop to loop a set of radio button groups? I have several questions that retrieve from database and each question has five different ratings named 1, 2, 3, 4 and 5. I create radio button group for the rating and using while to loop them. I know I have to create a unique name for radio buttons for each row but how to increment the value automatically for each row? I didn’t retrieve any value from database when I create the radio button group. All the data is arrange in the table. Besides, I need to save each selected value into database. I am using php. Anyone got ideas for my question. Thanks for your kindly reply.

Recommended Answers

All 19 Replies

How to use while loop to loop a set of radio button groups? I have several questions that retrieve from database and each question has five different ratings named 1, 2, 3, 4 and 5. I create radio button group for the rating and using while to loop them. I know I have to create a unique name for radio buttons for each row but how to increment the value automatically for each row? I didn’t retrieve any value from database when I create the radio button group. All the data is arrange in the table. Besides, I need to save each selected value into database. I am using php. Anyone got ideas for my question. Thanks for your kindly reply.

Post your code.

Part of the code...

<td><table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>
<?php 

while($row = mysql_fetch_array($result))
{ 
<td width="350" align="center" width="20"><?php echo $row[no];?></td>
<td width="350"><?php echo $row[question];?></td>
<td align="center" width="500">
<input type="radio" name="rating'.++$row["no"].'" value="1"/>1
<input type="radio" name="rating'.++$row["no"].'" value="2"/>2
<input type="radio" name="rating'.++$row["no"].'" value="3"/>3
<input type="radio" name="rating'.++$row["no"].'" value="4"/>4
<input type="radio" name="rating'.++$row["no"].'" value="5"/>5</td>

<td align='center' width="50">
<input name="amount'.$row[no].'" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark'.$row[no].'" cols="30" id="title"></textarea></td>
</tr>
<?php }?>
</td>
</tr>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</div></p>

create a variable before the loop and set its value to 1

inside the loop, call your radio button something like my_button_X where X is the value created above.

then add one to the variable before ending the loop.

You could also send the final value of the variable as a hidden field which can be useful when processing the radio button inputs.

How to use while loop to loop a set of radio button groups? I have several questions that retrieve from database and each question has five different ratings named 1, 2, 3, 4 and 5. I create radio button group for the rating and using while to loop them. I know I have to create a unique name for radio buttons for each row but how to increment the value automatically for each row? I didn’t retrieve any value from database when I create the radio button group. All the data is arrange in the table. Besides, I need to save each selected value into database. I am using php. Anyone got ideas for my question. Thanks for your kindly reply.

<td><table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
<td width="350" align="center" width="20"><?php echo $i;?></td>
<td width="350"><?php echo $row[question];?></td>
<td align="center" width="500">
<input type="radio" name="rating<?=$i?>" value="1"/>1
<input type="radio" name="rating<?=$i?>" value="2"/>2
<input type="radio" name="rating<?=$i?>" value="3"/>3
<input type="radio" name="rating<?=$i?>" value="4"/>4
<input type="radio" name="rating<?=$i?>" value="5"/>5</td>

<td align='center' width="50">
<input name="amount<?=$i?>" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark<?=$i?>" cols="30" id="title"></textarea></td>
</tr>
<?php $i++;}?>
</td>
</tr>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</div></p>

try like this.

create a variable before the loop and set its value to 1

inside the loop, call your radio button something like my_button_X where X is the value created above.

then add one to the variable before ending the loop.

You could also send the final value of the variable as a hidden field which can be useful when processing the radio button inputs.

Thanks for the reply...

<td><table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
<td width="350" align="center" width="20"><?php echo $i;?></td>
<td width="350"><?php echo $row[question];?></td>
<td align="center" width="500">
<input type="radio" name="rating<?=$i?>" value="1"/>1
<input type="radio" name="rating<?=$i?>" value="2"/>2
<input type="radio" name="rating<?=$i?>" value="3"/>3
<input type="radio" name="rating<?=$i?>" value="4"/>4
<input type="radio" name="rating<?=$i?>" value="5"/>5</td>

<td align='center' width="50">
<input name="amount<?=$i?>" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark<?=$i?>" cols="30" id="title"></textarea></td>
</tr>
<?php $i++;}?>
</td>
</tr>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</div></p>

try like this.

Thanks for the reply and it really works for me coz now I can select more than one radio button...but now how can I save all the data into the database?I declared the variable as $rating, $amount and $remark but it doesnt work as what I expected coz the data weren't save into the database...am I wrongly declared the variable?

Thanks for the reply and it really works for me coz now I can select more than one radio button...but now how can I save all the data into the database?I declared the variable as $rating, $amount and $remark but it doesnt work as what I expected coz the data weren't save into the database...am I wrongly declared the variable?

show your code.

Part of the code...

$rating = $_POST['rating'];
$amount = $_POST['amount'];
$remark = $_POST['remark'];
$sql = "INSERT INTO survey (rating, amount, remark) VALUES ('$rating', '$amount', '$remark')";
mysql_query($sql);
<?
extract($_POST);
print_r ($rating);
print_r ($amount);
print_r ($remark);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="formx" action="" method="post">
<table>
<tr>
<td>
<table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>

<?php
$i=1;
while($i<10)
{
?>
<tr>
<td> <?=$i?> </td>
<td width="350">question<?=$i?></td>
<td align="center" width="500">
<input type="radio" name="rating[<?=$i?>]" value="1"/>1
<input type="radio" name="rating[<?=$i?>]" value="2"/>2
<input type="radio" name="rating[<?=$i?>]" value="3"/>3
<input type="radio" name="rating[<?=$i?>]" value="4"/>4
<input type="radio" name="rating[<?=$i?>]" value="5"/>5</td>


<td align='center' width="50"><input name="amount[<?=$i?>]" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark[<?=$i?>]" cols="30" id="title"></textarea></td></tr>
<?
$i++;
}
?>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>

</table>
</div></p>
</td></tr></table>
</form>

</body>
</html>

try this now. i don't know how you are validating fields. thats why i am taking count of $rating . this is for just reference to you.

<?
extract($_POST);
//print_r ($rating);
//print_r ($amount);
//print_r ($remark);
//$select=mysql_query("select * from questions ");
//$count=mysql_num_rows($select);

for($j=1;$j<=count($rating);$j++)
{
  echo "INSERT INTO survey (rating, amount, remark) VALUES ('$rating[$j]', '$amount[$j]', '$remark[$j]')";
  echo "<br>";
      }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="formx" action="" method="post">
<table>
<tr>
<td>
<table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>

<?php
$i=1;
while($i<10)
{
?>
<tr>
<td> <?=$i?> </td>
<td width="350">question<?=$i?></td>
<td align="center" width="500">
<input type="radio" name="rating[<?=$i?>]" value="1"/>1
<input type="radio" name="rating[<?=$i?>]" value="2"/>2
<input type="radio" name="rating[<?=$i?>]" value="3"/>3
<input type="radio" name="rating[<?=$i?>]" value="4"/>4
<input type="radio" name="rating[<?=$i?>]" value="5"/>5</td>


<td align='center' width="50"><input name="amount[<?=$i?>]" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark[<?=$i?>]" cols="30" id="title"></textarea></td></tr>
<?
$i++;
}
?>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>

</table>
</div></p>
</td></tr></table>
</form>

</body>
</html>

thanks for the reply and it really works...but all the data can saved into the database when I use single php file...since I am using the OO method, so I need to have separate php file...but the problem is the data are not saved into the database....although I had try all kinds of methods to solve this problem but the data still cannot save into the database...is anyone got the comment on this problem?thanks for the help...
here are some code...

class_survey.php

class_survey
{
     function insert_survey($rating, $amount, $remark)
    {	
        for($j=1;$j<=count($rating);$j++)
       {	
             $sql = "INSERT INTO survey (rating, amount, remark) VALUES ('$rating[$j]', '$amount[$j]', '$remark[$j]')";
               mysql_query($sql);			}
       }
   }
}

survey_form.php

<body>
<form name="survey" id="survey" method="post" action="survey_code.php">
<table>
<tr>
<td>
<table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>

<?php
$i=1;
while($i<10)
{
?>
<tr>
<td> <?=$i?> </td>
<td width="350">question<?=$i?></td>
<td align="center" width="500">
<input type="radio" name="rating[<?=$i?>]" value="1"/>1
<input type="radio" name="rating[<?=$i?>]" value="2"/>2
<input type="radio" name="rating[<?=$i?>]" value="3"/>3
<input type="radio" name="rating[<?=$i?>]" value="4"/>4
<input type="radio" name="rating[<?=$i?>]" value="5"/>5</td>


<td align='center' width="50"><input name="amount[<?=$i?>]" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark[<?=$i?>]" cols="30" id="title"></textarea></td></tr>
<?
$i++;
}
?>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>

</table>
</div></p>
</td></tr></table>
<input name="rating" type="hidden" id="rating" value="<?php echo $rating ?>">
<input name="score" type="hidden" id="amount" value="<?php echo $amount ?>">
<input name="remark" type="hidden" id="remark" value="<?php echo $remark ?>">

</form>
</body>

survey_code.php

<?php 
include("class_survey.php");

$rating = $_REQUEST['rating'];
$amount = $_REQUEST['amount'];
$remark = $_REQUEST['remark'];
		
$survey_result = new survey();

$survey_result->insert_survey($rating, $amount, $remark);
?>

try this now. i don't know how you are validating fields. thats why i am taking count of $rating . this is for just reference to you.

<?
extract($_POST);
//print_r ($rating);
//print_r ($amount);
//print_r ($remark);
//$select=mysql_query("select * from questions ");
//$count=mysql_num_rows($select);

for($j=1;$j<=count($rating);$j++)
{
  echo "INSERT INTO survey (rating, amount, remark) VALUES ('$rating[$j]', '$amount[$j]', '$remark[$j]')";
  echo "<br>";
      }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="formx" action="" method="post">
<table>
<tr>
<td>
<table width='900' border='1'>
<tr>
<td><div align='center'><strong>No.</strong></div></td>
<td><div align='center'><strong>Question</strong></div></td>
<td><div align='center'><strong>Rating</strong></div></td>
<td><div align='center'><strong>Amount</strong></div></td>
<td><div align='center'><strong>Remark</strong></div></td>
</tr>

<?php
$i=1;
while($i<10)
{
?>
<tr>
<td> <?=$i?> </td>
<td width="350">question<?=$i?></td>
<td align="center" width="500">
<input type="radio" name="rating[<?=$i?>]" value="1"/>1
<input type="radio" name="rating[<?=$i?>]" value="2"/>2
<input type="radio" name="rating[<?=$i?>]" value="3"/>3
<input type="radio" name="rating[<?=$i?>]" value="4"/>4
<input type="radio" name="rating[<?=$i?>]" value="5"/>5</td>


<td align='center' width="50"><input name="amount[<?=$i?>]" type="text" id="title" value="" size="5" align="middle"></td>
<td><textarea name="remark[<?=$i?>]" cols="30" id="title"></textarea></td></tr>
<?
$i++;
}
?>
</table>
<p><div align="center">
<table width="200" border="0">
<tr>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td align="center"><input type="reset" name="Reset" value="Reset"></td>
</tr>

</table>
</div></p>
</td></tr></table>
</form>

</body>
</html>

At this point I would output my query to the browser and eyeball it to see if there is anything obviously wrong with it. If everything looks ok I would then cut and paste it directly into my database manager to see it I get any errors and if the data is inserted correctly. If everything goes ok then that eliminates the query as the problem. I would then make sure that I am outputting all errors to make sure that I am not missing something.

You can output errors by inserting the following two lines of code at the top of the script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

90% of the time you would have figured out what the problem is by now by following the previously mentioned steps but if your query works and you are stil not getting any errors (this is where it starts getting fun, the part I live for) you will have to start putting echo flags throughout your script to make sure that certain flows(ifs, for loops, whiles etc...) are behaving as predicted. Temporarily append an "or die(mysq_error())" right after your mysql_query function.

Through all of this, you should figure out what the problem is.

i agree with os_dev. try the above method.

ya...I already try to display my query before this...but still the same...now, after I insert the above code, it showed the message (part):
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 71
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 71
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 71
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 74
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 74
Notice: Undefined offset: 1 in C:\xampp\htdocs\aaa\class_survey.php on line 74
INSERT INTO survey (rating, amount, remark) VALUES ('', '', '')

after that, I try to redefined the variables and all the related items... try all the possible ways to solve the problem...but is still the same...
any comment??appreciated it...

so I assume that:
$rating[1]
$amount[1]
$remark[1]
are not set. Do a print_r() on each of these to make sure.

may I know what do you mean by $rating[1], $amount[1], $remark[1] are not set?
after I try to do print_r() on each of them, shown nothing...so I try to do

extract($_POST);
print_r($rating);
print_r($amount);
print_r($remark);

then show

Array ( [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 ) 
Array ( [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 [] => 5 )
Array ( [] => a [] => b [] => c [] => d [] => e [] => f [] => g )

but the data is not save into the database...

I try to add some code in survey_form.php after the submit button...

<input name="rating" type="hidden" id="rating" value="<?php echo $rating ?>">
<input name="amount" type="hidden" id="amount" value="<?php echo $amount ?>">
<input name="remark" type="hidden" id="remark" value="<?php echo $remark ?>">

I also try the code below

<input name="rating[]" type="hidden" id="rating[]" value="<?php echo $rating[] ?>">
<input name="amount[]" type="hidden" id="amount[]" value="<?php echo $amount[] ?>">
<input name="remark[]" type="hidden" id="remark[]" value="<?php echo $remark[] ?>">

but the result is still the same...the data are not saved...
I know there is problem during the passing values between pages...
is it??what should I do in order to solve the problem???because I already try a lot of methods to solve the problem...
thanks in advance...

so I assume that:
$rating[1]
$amount[1]
$remark[1]
are not set. Do a print_r() on each of these to make sure.

I don't know what you are trying to do with extract() since I have never needed to use it. I also don't know what you are trying to do with:

<input type="radio" name="rating[<?=$i?>]" value="1"/>

either, I have done this before:

<input type="radio" name="rating[]" value="1"/>

which will post to the page as an array and then you can iterate through the array like so:

<?php
foreach($_POST['rating'] as $rate)
{
   //$rate will contain each value in the array as the loop iterates through
}
?>

To access a single variable you just do

$query = "insert into tablename(col1, col2, col3) values('" . mysql_real_escape_string($_POST['val1']) . "', '" . mysql_real_escape_string($_POST['val2']) . "', '" . mysql_real_escape_string($_POST['val3']) . "')";

Using extract just seems to be making things too complicated. If you are still having an issue, put this at the top of your page and then post your results.

echo "<pre>";
print_r($_POST);
echo "</pre>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.