Show value taken from database as a radio button - php

I have created a database and retrieved some values and show them in my view as a text. I want to show these values as radio buttons. How can i do this?
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo $rec->ans1."<br/>";
echo $rec->ans2."<br/>";
echo $rec->ans3."<br/>";
echo $rec->ans4."<br/>";
}
?>
Like this i have shown my values in database as texts. How can i show these like radio buttons?
Thank you.

Try this
<?php
foreach ($records as $rec) {
?>
<input type="radio" name="" value="<?php echo $rec['ans'] ?>"><?php echo $rec['ans'] ?>
<?php
}
?>
Question should come in another array

You can do this as follows:
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans1_".$rec->id."'>".$rec->ans1."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans2_".$rec->id."'>".$rec->ans2."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans3_".$rec->id."'>".$rec->ans3."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans4_".$rec->id."'>".$rec->ans4."<br/>";
}
?>

Related

I want to save all the checkbox values I select

I m trying to store checkbox value subject_id but when i store this value to database it just store the last selected checkbox inside the database... please help me to sort out the code so I can get all selected values.
I want to save all the checkbox values I selected,but if I check more then one checkbox it always save the last checkbox.
function get_class_section($day='',$section='',$class='')
{
print_r($section);
$sections = $this->db->get_where('class_routine' , array(
'day' => $day,'section_id'=>$section,'class_id'=>$class
))->result_array();
foreach ($sections as $row) {
echo "<table class=\"table table-bordered datatable\" id=\"table_export\" >";
echo "<thead>";
echo "<th>Subject</th><th colspan=\"2\">status</th>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $this->crud_model->get_subject_name_by_id($row['subject_id'],"<input type=\"text\" name=\"subject_id\" value=\"subject_id\" >");
echo "<input type=\"checkbox\" name=\"types[]\" value=\"".$row['subject_id']."\" set_checkbox('types[]', 'subject_id')>";
echo "</td>";
echo "<td>present <input type=\"checkbox\" value=\"1\" ></td>"
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
how to save all the check-boxes that i selected
You need foreach loop to get all checked checkboxes
Try This:
if(isset($_REQUEST['types']) && $_REQUEST['types']!=array())
{
foreach ($_REQUEST['types'] as $key => $value) {
// in $value,you will get checkbox value
}
}
You should use hidden element for all checkbox
Try this
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>
<input type=\"hidden\" name=\"types[]\" value=\"0\"> ";

PHP-How to get values from multiples dynamic radio button

i´m doing a little survey and i have the next problem.
I get the data from database and the name of the answer is the id of the question(question 1 id=1, question 2 id=2...)
<div id='box'>
<form action='#' method='POST'>
<?php
foreach($lista as $list){
echo "<p>".$list->getQuestion()."</p>";
$idtemp=$list->getid(); //first get question
$arr=$list->getQuestion($idtemp);
foreach ($arr as $ar) {
//second, get and draw the radio with the answers per question
echo "<input type='radio' value='".$ar[0]."' name='".$idtemp."'>".$ar[1]."</input><br>";
}
}
?>
<button type="button" >Cancel</button>
<button type='submit' value='end' name='end'>end</button>
<?php
if (isset($_POST['end'])) {
echo "<span>selected :<b> ".$_POST['radio']."</b></span>";
}
?>
</form>
</div>
I only wants to get the radios checked and sending to database,
Here, you need to use below line
echo "<input type='radio' value='".$ar[0]."' name='answer[".$temp."]'>".$ar[1]."</input><br>";
instead of this
echo "<input type='radio' value='".$ar[0]."' name='".$temp."'>".$ar[1]."</input><br>";
Then use
<?php
if (isset($_POST['answer'])) {
foreach($_POST['answer'] as $key=>$value){
echo "<span>selected :<b> ".$key." ".$value."</b></span>";
}
}
?>

stuck with code of php to add while loop condition

i m trying to increment value 1 using while loop condition, i m developing quiz generation system, but stuck here and not found any solution, please help and tell what to do....
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$i++;
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech1; ?>'><?php echo $qech1."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech2; ?>'><?php echo $qech2."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech3; ?>'><?php echo $qech3."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech4; ?>'><?php echo $qech4."<br>"; ?><br>
<?php
}
?>
you seen above code, it is $i = 1; and then i echo $i as well in 4 fields of radio. it retrives around 3 questions from database and each question has 4 values, but all of the total of 12 values of 3 questions has same name that is 1, it is not increment using $i++ not sure why ?
here is out put
<span style='font-size:18px;font-weight:550px; '>english</span><br><input type='radio' name='1' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='jkj'>jkj<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<span style='font-size:18px;font-weight:550px; '>adnan </span><br><input type='radio' name='1' value='jhwjksahajsh'>jhwjksahajsh<br><br>
<input type='radio' name='2' value='kjhjhjh'>kjhjhjh<br><br>
<input type='radio' name='2' value='kjhkjjh'>kjhkjjh<br><br>
<input type='radio' name='2' value='hjkjhj'>hjkjhj<br><br>
<span style='font-size:18px;font-weight:550px; '>sdfsd</span><br><input type='radio' name='1' value='gfgf'>gfgf<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gf'>gf<br><br>
You can use a Heredoc string to help you with this. I made a simple example that will output what you want in a simple way. Take a look:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
echo <<<HTML
<input type='radio' name='$i' value='$qech1'>$qech1<br><br>
<input type='radio' name='$i' value='$qech2'>$qech2<br><br>
<input type='radio' name='$i' value='$qech3'>$qech3<br><br>
<input type='radio' name='$i' value='$qech4'>$qech4<br><br>
HTML;
$i++;
}
The reason it isn't incrementing is because you are calling $i right after incrementing it.
while ... {
$i++
...
..name="$i"...
}
Right now, it goes through the loop once and prints all names as 2. The next time all of them would be 3, etc.
What you want is more of a foreach loop.
I assigned basic values just for my testing purposes. I personally would eliminate assigning $quech1 = $rows[optiona]. You can just work with the array of $rows with the foreach loop.
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$correct=$rows['correct'];
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
You'll see from my inspect element, that you are now getting incremented names:
However, if I'm not mistaken. Don't you generally want all of the names to be the same for a group of radio buttons? You'll want unique ID's. And then have all buttons that are to be grouped together with the same name. This makes it so only one button in that group can be selected. If that is what you're trying to accomplish, use the revised code:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$name = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$name++;
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' id='<?php echo $i; ?>' name='<?php echo $name; ?>'value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
Only change is that $i is being assigned to id now. And we'll declare a $name counter as well. We'll increment that in the while loop, so that it will only be incremented once for each set of button options. And then you use $name in the name attribute for the button.
That's what makes sense to me. But without more information, we don't really know exactly what you're trying to accomplish. :)

Retrieving Nested array in php using _POST

I have set the below in my HTML form. How do I retrieve it back in my PHP page(ON Submit)?
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
How do I retrieve my answer back ?
I use POST method for my form. Help me!!
Adding Details.
<?php
echo "hello";
$answer = _POST("answer");
echo $answer-0;
echo $answer[0];
foreach ($answer as $i => $value) {
echo $value;
echo $i;
}
?>
answer-0=3&answer-1=1
Above Values shows that The parameters are set(Get Method). But In POST METHOD I am not able to access them.
Can Someone help me? I still havent resolved the issue.
<form method="post">
<?php
foreach ($_POST as $i => $value) {
echo "$i $value<br/>";
}
echo $_POST['answer-1'];
echo "<br>";
echo $_POST['answer-2'];
$rows = array(
"1" => "bar",
"2" => "foo",
);
foreach ($rows as $i => $value) {
echo "<br/>What do you think about : ";
echo "<li>$value<br/>";
echo "1<input type='radio' value='1' name='answer-$i' /> ";
echo "2<input type='radio' value='2' name='answer-$i' /> ";
echo "3<input type='radio' value='3' name='answer-$i' /> ";
echo "4<input type='radio' value='4' name='answer-$i' /> ";
echo "5<input type='radio' value='5' name='answer-$i' /> ";
}
?>
<input type="submit"/>
</form>
Looks more like, what you need.
So you need to use $_POST array with square brackets (it's an array). This code will build an array of $answers for you, assuming you still have $rows available.
$answers = array();
foreach ($rows as $i => $value) {
$answers[$i] = $_POST["answer-$i"];
}
print_r($answers);

Check a checkbox when element exists in the database

I have a table that has different names of competences and another one in which we have the competence_ID and the user_ID that has that competence. I have a form in which I submit the competences that apply to a certain user. Now, I want to retrieve these competences and check boxes in case they are valid.
I use this code, but it does not seem to work.
<?php
include "db.php";
$employees=mysql_query("SELECT * FROM asiakas");
echo "<form method='post' action='' id='employeesselection'><select name='select_employee' id='select_employee'>";
while($row=mysql_fetch_array($employees)){
$selected = ($row['Id'] == $_POST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
}
echo "</select><input type='hidden' name='action' value='selectedemployee'>
<input type='submit' value ='submit' name='submit'><br/>";
if(isset($_POST['select_employee'])){
$specific=mysql_query("SELECT Id, Sukunimi,Etunimi from asiakas WHERE Id=".$_POST['select_employee']);
$sp=mysql_fetch_array($specific);
}
if(isset($sp['Etunimi']) && isset($sp['Sukunimi'])){
$comp=mysql_query("SELECT * FROM Competences");
echo "<br/>select competences for: ". $sp['Etunimi'];
$id= $sp['Id'];
$result=mysql_query("SELECT distinct c_ID from User_Competence WHERE e_ID=".$id);
while($test=mysql_fetch_array($result))
{
echo $test['c_ID'];
}
echo "<table><th>valid?</th><th>Competence description</th>";
$counter=0;
$traverse=0;
while($compi=mysql_fetch_array($comp)){
$checked='';
if($test[$traverse]==$counter){
$checked="checked";
$traverse++;
}
$counter ++;
echo "<tr><td><input type='checkbox'" .$checked." name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";
}
echo "</table>";
echo "<input type='hidden' name='action' value='selectchecked'>";
echo "<input type='submit' value='submit checks'>";
}
if(isset($_POST) && !empty($_POST)){
print_r($_POST);
}
if(isset($_POST['action']) && $_POST['action']='selectchecked'){
if (isset($_POST['c'])){
$s = $_POST['c'];
foreach ($s as $k => $v) {
if (is_array($v)) {
// array_push($s, implode('', array($v [$what])));
}
};
echo $abc= implode(',', $s);
for ( $a=0;$a<count($s);$a++){
$ar=explode(',',$abc);
echo $var= $ar[$a];
$q=mysql_query("INSERT INTO user_competence(c_ID, e_ID) VALUES ('".$var."','".$id."')");
}
}
}
echo "</form>";
?>
If you put checked as an attribute on the <input type="checkbox"> it will be checked, even if you leave the actual value of the attribute empty.
So change the following parts from...
$checked="checked";
...
echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' ...
Into...
$checked="checked='checked'";
...
echo "<tr><td><input type='checkbox' ".$checked." name='c[]' ...
Which will mean if the $test[$traverse$] is true, checked='checked' will be placed into the HTML, otherwise it will be left out.

Categories