Well, I've following Php code which generate a table with radio button so that user can select only one radio button in EVERY row. But to select only one radio button in Every row it's must be a unique name in EVERY row, Right ? But I can't get any idea how do i set a unique name in while loop. Can you guys give me any idea or solutions ?
Php code:
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>Check All <input type= 'checkbox'
onclick='checkAll(this)' /></td>";
echo "</tr>";
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>
<input type='radio' name='ch[]' value='1' /> <input type='radio' name='ch[]'
value='1' />
</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
Thanks for your help.
Update:
echo "<td class='tdhead2' valign='top'>";
echo '<input type="radio" name="ch'.$counter++.'[]" value="1">';
echo " ";
echo '<input type="radio" name="ch'.$counter.'[]" value="0">';
echo "</td>";
you can set a variable counter:
$counter = 1;
while($res2 = mysql_fetch_array($sql2)){
echo '<input type="radio" name="ch'.$counter++.'[]" value="'.$counter++.'">';
}
Instead of making the name just ch[] you can make the name ch[SOME_UNIQUE_VALUE] for each row. Usually something like an id number is used. Perhaps $roll if that is unique to each student. That will also allow you to know which id the radio button pertains to. On form submit, you can foreach($_POST['ch'] as $id=>$value) and get the id out.
Edit:
Example code:
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch_{$counter}[]' value='0' /> ";
echo "<input type='radio' name='ch_{$counter}[]' value='1' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
Please note, unless you do like I suggested above, you will not be able to know which student each radio group goes to. You can assume that the radio buttons on group row 1 belongs to the student in row 1, but you might not know which student is in row 1. Which is why I suggest you put a unique id that goes with each student with each radio button instead of a counter.
You can store a counter $count and increase it after each iteration. Or if you have some unique id from the database per row (for example the primary_key), you can use that instead.$id = $res['id']
Then concat that value to the name=ch".$id."...
you should use student name as an unique array and put that value in the name=""
and it will auto increment according to the name and you can only choose single option at single row.
try this :
<input type='radio' name='<?php echo $res2['sname'];?>' value='0' /> ";
<input type='radio' name='<?php echo $res2['sname'];?>' value='1' />";
this solutio will work.because it work's for me in an attendance system.
Related
I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";
I am writing an attendance system, where the name of the person working in a department will be shown and user will select respective attendance through radio buttons. I want to write the attendance record in a mysql data base.
For this i tried to prepare an array where i am unable to pass the value of selected radio button. Please help.
The sample code is as under:
<?php
$qry1="select * from master where office='ECZO_IT' order by cadre desc,srno";
$result1=mysql_query($qry1,$con);
$sl=1;
echo "<form method='post' action=''>";
echo "<table align='center' border='1' cellpadding='0' cellspacing='0' style='border- collapse: collapse' bordercolor='#111111' width='800' id='AutoNumber1' height='219'>";
echo "<tr><th colspan='14' bgcolor='#C0C0C0' align='center'>Attendance Report</th></tr>";
echo "<tr bgcolor='#C0C0C0' align='center'><th>Sl.</th>";
echo "<th >SR No.</th>";
echo "<th>Name</th>";
echo "<th>Designation</th>";
echo "<th colspan='10' align='center'>Attendance</th></tr>";
while ($data=mysql_fetch_row($result1))
{
echo "<tr><td align='center'>$sl</td>";
echo "<td align='center'>$data[0]</td>";
echo "<td>$data[1]</td>";
echo "<td align='center'>$data[4]</td>";
echo "<td bgcolor='#C0C0C0' align='center'>Present</td>";
echo "<td><input type='radio' name='r1[$sl]' value='PRESENT' id='at' checked></td>";
echo "<td bgcolor='#C0C0C0' align='center'>Training</td>";
echo "<td><input type='radio' name='r1[$sl]' value='TRAINING' id='at'></td>";
echo "<td bgcolor='#C0C0C0' align='center'>Tour</td>";
echo "<td><input type='radio' name='r1[$sl]' value='TOUR' id='at'></td>";
echo "<td bgcolor='#C0C0C0' align='center'>Leave</td>";
echo "<td><input type='radio' name='r1[$sl]' value='LEAVE' id='at'></td>";
echo "<td bgcolor='#C0C0C0' align='center'>Absent</td>";
echo "<td><input type='radio' name='r1[$sl]' value='ABSENT' id='at'></td></tr>";
$out[]= array ('srno' => $data['0'], 'ename' => $data[1], 'att' => $_POST['r1']);
$sl=$sl+1;
}
echo "</table>";
echo "<p align='center'><input type='submit' name ='s1' value='Submit'></p>";
echo "</form>";
foreach($out as $row)
{
echo $row['srno'].'----'.$row['ename'].'----'.$row['att']."<br/>";
}
?>
Please suggest if there is any other way out
Notes:
I tried to change the array as under
$out = array ('srno' => $data['0'], 'ename' => $data[1], 'att' => $_POST['r1']);
before modification the result was like this:
343587----SUNIL PRASAD OJHA----Array
343607----MUKESH KUMAR JHA----Array
343701----PRABHAT RANJAN----Array
After modification the result was as under:
3----3----3
P----P----P
I also tried to check output through the following code in foreach loop:
echo var_dump($out['att']);
but the result shows NULL only
Please help
I can access your radio button's value through:
var_dump($out[0]['att']);
Correct your array this way:
$out = array ('srno' => $data['0'], 'ename' => $data[1], 'att' => $_POST['r1']); //removed [] from $out
to accces it as:
echo $out['att'][1];
The 1 is because your radio buttons are an input array and your $sl value is 1
I have no idea why you need the foreach loop but you can also use it this way:
$data[1]='test';
$data2[1]='test2';
$out= array ('srno' => $data, 'ename' => $data2, 'att' => $_POST['r1']);
$i=1;
foreach($out as $key=>$val)
{
echo $key.'='.$val[$i]."<br/>";
}
//Prints: srno=test
// ename=test2
// att=TRAINING
I've a following html table which name contain array. How can i check this array if it's value is empty ?
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
Currently i'm checking it with following code, it's not working but i know the name is array and it's must be compare with any array function. Can you guys give me a idea ?
if(isset($_POST['ch']))
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
Regards.
Update: (Full Code)
$action = htmlspecialchars($_SERVER['PHP_SELF'])."?class=$class_from";
echo "<form method='post' action='$action' name='attendence'/>";
echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>";
echo "<tr>";
echo "<td class='tdhead' valign='top' width='200'><b>Student Name</b></td>";
echo "<td class='tdhead' valign='top' width='250'><b>Roll No</b>
</td>";
echo "<td class='tdhead' valign='top' width='250'><b>Class Name</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'><b>Present / Not present</b>
</td>";
echo "<td class='tdhead' valign='top' width='200'>
Present All <input type= 'checkbox'
onclick='checkAll(this)'</td>";
echo "</tr>";
//start the counter variable
$counter = 1;
while($res2 = mysql_fetch_array($sql2))
{
$id = (int) $res2['id'];
$sname = inputvalid($res2['sname']);
$roll = inputvalid($res2['roll']);
$class = inputvalid($res2['class']);
echo "<tr>";
echo "<td class='tdhead2' valign='top'>$sname</td>";
echo "<td class='tdhead2' valign='top'>$roll</td>";
echo "<td class='tdhead2' valign='top'>$class</td>";
echo "<td class='tdhead2' valign='top'>";
//echo each radio with the counter for this row
echo "<input type='radio' name='ch[$roll][$sname][$class]' value='1' /> ";
echo "<input type='radio' name='ch[$id][$sname][$class]' value='0' />";
echo "</td>";
echo "<td class='tdhead2' valign='top'> </td>";
echo "</tr>";
//add one to the counter
$counter++;
}
echo "<tr>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'> </td>";
echo "<td class='tdhead2'><input type='submit' value='Record' name='Submit'
class='submit' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
if(isset($_POST['Submit']) && $_POST['Submit'] == "Record")
{
if(isset($_POST['ch']))
{
$ch = array_filter($_POST['ch']);
if (empty($ch))
{
echo "<div class='error>Select attendence field. </div>";
}
}
if(count($ch) == 0)
{
echo "<div class='error>Select attendence field. </div>";
}
else
{
foreach ($_POST['ch'] as $roll => $arr1)
{
$roll;
foreach ($arr1 as $name => $arr2)
{
$name;
foreach ($arr2 as $class => $value)
{
$class;
$value;
$sql = mysql_query("INSERT INTO e_attendence VALUES('', '$name', '$roll', '$class',
'$value', '$current_date')");
}
}
}
}
if($sql)
echo "<div class='success'>Succesfully recorded.
</div>";
}
empty() method work for finding whether the array is empty or not.
But for Your solution use below:-
if(isset($_POST['ch']) && $_POST['ch']=='1')
{
$ch = $_POST['ch'];
if(empty($ch))
echo "<div class='error>Select attendence field. </div>";
}
if You want to show a error message when no radio is selected then use below code:-
if(!isset($_POST['ch']) && $_POST['ch']=='')
{
echo "You have not selected any radio button";
}
use empty() instead of isset()
check the comparison table available at the below link
http://php.net/manual/en/types.comparisons.php
try this
print_r($_POST['ch']);
Try this
if(isset($_POST['ch'])){
$ch = array_filter($_POST['ch']);
if (empty($ch)) {
echo "<div class='error>Select attendence field. </div>";
}
}
I have a dynamic table which is filled from an Application.
My problem is that I need some way to freeze the first row and the first four columns BUT using only one table, obviously because I will be posting the table back again to the application.
My Current Table Code :
....
<form name="myform" method="post" action="test123.php" onsubmit="set_value();">
//this form is used to post back the data after user updates it.
<br />
<table align="center" id="dataTable">
<thead>
<tr class="header">
...
$arratt49 = (explode(',',$response49,-1)); //sending an array from application
$dayscounter = sizeof($arratt49);
$sizemon = strlen($month1);
if($sizemon > 0)
{
echo "<th class='text ce6'>No.</th>";
echo "<th class='text ce6'>Emp code</th>";
echo "<th class='text ce6'>Name</th>";
echo "<th class='text ce6'>Department </th>";
for($kkk=0;$kkk<$dayscounter;$kkk++)
{
echo "<th class='text ce7'>";
echo $arratt49[$kkk];
echo "</br>";
echo $arratt49[$kkk+1];
echo "</th>";
$kkk = $kkk +1;
}
}
</tr>
</thead>
<tbody>
<tr>
....//Here goes the Body
for($k=0;$k<=$arrcnt;$k++)
{
echo "<td class='text ce2'>";
echo $counter;
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='code[]' size = '8' readonly='true 'value='";
echo $arratt[$k];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='name[]' size = '40' readonly='true' value='";
echo $arratt[$k+1];
echo "'";
echo "</td>";
echo "<td class='text ce2'><INPUT type='text' name='dept[]' size = '12' readonly='true 'value='";
echo $arratt[$k+2];
echo "'>";
echo "</td>";
Already tried :
Big Four method
and also This
Any Good Ideas?
Again I CANNOT use more than one table method.
You can use datatables plugin FixedColumns, just set parameter "iLeftColumns" to the number of columns needed to be fixed from the left side of table. Header already fixed by default.
I'm stumped. Here's my relevant code:
while($data = mysql_fetch_array($getdata)){
$myid = $data["myid"]; //unique entry identifier for table rows
$method = $data["method"];
$category = $data["category"];
$company = $data["company"];
$datestarted = $data["datestarted"];
$initialfee = $data["initialfee"];
$ongoingfee = $data["ongoingfee"];
echo "</tr>";
echo "<tr>";
echo "<td class='mymenu' width='14%' height='60px'>";
echo $method;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $category;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $company;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $datestarted;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $initialfee;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $ongoingfee;
echo "</td>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
}
What's happening here, is that a table is going to be displayed and each row in that table will look as coded above. There could be any number of entries, but probably in a range of 1-6. At the end of each entry is a remove button. Once clicked, the user is forwarded to a page where that entry will then be subject to removal from the table. My problem is identifying which entry the user wants to remove based on which "remove" button they clicked. If the number of rows weren't dynamic, I could just give each input button a unique name ahead of time and that could help. How can I uniquely identify what row I need to remove on the removal page after a remove button is clicked? I can't use sessions because the session variable $myid would just change each time a new row were made. Pulling from a database won't help obviously because I lack a unique identifier on the following page.
I have an idea of setting the name of each button equal to each unique $myid, which would make that unique $myid the only $_POST variable that would travel to the removal page depending on the button pressed. On the removal page I would just set $myid equal to that post variable. However, even if it's the only post variable available, how do I reference it to be equal to $myid on the removal page without knowing the name of it? Because I won't know the name of the $_POST variable that traveled to the removal page.
The answer's probably simple, but for whatever reason it isn't coming to me on this night.
Thanks for your help. Honestly don't know what I would do without this forum.
Create a new form for each row, with a hidden field containing the ID of the record you want to delete.
echo "<form>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='hidden' value='$id'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
echo "</form>";
At first I want to say your tr tag is not well placed, and create a form to solve your problem
Try this:
while($data = mysql_fetch_array($getdata)){
$myid = $data["myid"]; //unique entry identifier for table rows
$method = $data["method"];
$category = $data["category"];
$company = $data["company"];
$datestarted = $data["datestarted"];
$initialfee = $data["initialfee"];
$ongoingfee = $data["ongoingfee"];
echo "<tr>";
echo "<td class='mymenu' width='14%' height='60px'>";
echo $method;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $category;
echo "</td>";
echo "<td class='mymenu' width='20%' height='60px'>";
echo $company;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $datestarted;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $initialfee;
echo "</td>";
echo "<td class='mymenu' width='12%' height='60px'>";
echo $ongoingfee;
echo "</td>";
echo "<td class='mymenu' width='10%'>";
echo "<form>";
echo "<input type='hidden' value='".$id."'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
try this #Lock forgot to put " in form.
<?php
echo "<form>";
echo "<table><tr>";
echo "<td class='mymenu' width='10%'>";
echo "<input type='hidden' value='$id'>";
echo "<input type='submit' name='' class='button' value='Remove'>";
echo "</td>";
echo "</tr></table>";
echo "</form>";
?>