Forum Moderators: coopster

Message Too Old, No Replies

php for multiple button in a column?

         

appletea

1:50 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Hi,
I need help with radio button. I need to select multiple radio button in a column.Currently my code work fine for selecting multiple radio button in a row but not in a column. The $i++ doesn't seem to work for me. Any suggestion in making this work?Below is my code.Thanks.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<h1></h1>
<hr />
<h2>Attendance</h2>
<body>
<form name="form1" method="post" action="new3.php">
<table border='1'>
<tr>
<th>Participant Name</th>
<th>Class</th>
<th>Day 1</th>
<th>Day 2</th>
<th>Day 3</th>
<th>Day 4</th>
<th>Day 5</th>
<th>Day 6</th>
<th>Day 7</th>
<th>Day 8</th>
<th>Day 9</th>
<th>Day 10</th>
<th>Day 11</th>
<th>Day 12</th>
</tr>
<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());
$course = "SELECT m.idMember, m.name AS member_name, a.Member_idMember, cl.Course_idCourse, co.idCourse, co.name, cl.idClass FROM member m, attendance a, Class cl, Course co WHERE m.idMember = a.Member_idMember AND a.Class_idClass = cl.idClass AND
cl.Course_idCourse = co.idCourse";
$result = mysqli_query($link,$course) or die(mysqli_error($link));
?>


<?php
$i=1;
while($row = mysqli_fetch_array($result))
{
echo "<td>".$row['member_name']."</td>";
echo "<td>".$row['idClass']."</td>";
{
echo '<form name="form1" method="post" action="new5.php">
<td>
<input type="radio" name="day_present01" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present01" value="Present" /> Present</label></td>
<td><label><input type="radio" name="day_present02" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present02" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present03" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present03" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present04" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present04" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present05" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present05" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present06"value="Absent" /> Absent</label>
<label><input type="radio" name="day_present06" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present07" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present07" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present08" value="Absent"/> Absent</label>
<label><input type="radio" name="day_present08" value="Present"/> Present</label>
</td>
<td><label><input type="radio" name="day_present09" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present09" value="Present"/> Present</label>
</td>
<td><label><input type="radio" name="day_present10" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present10" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present11" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present11" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present12" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present12" value="Present" /> Present</label>
</td>';
}
echo "</tr>";
}
$i++;
echo "</table>";
mysqli_close($link);

?>
<input type = "Submit" Name = "Submit1" VALUE = "Submit">
<input type = "Submit" Name = "Save" VALUE = "Save">
</form>
</body>
</html>

penders

2:41 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The $i++ doesn't seem to work for me.


You don't appear to be doing anything with your $i variable, so that's probably why it's not 'working'. I'm guessing you are wanting to perhaps incorporate the value of $i in your form field NAMEs (?), to make each row unique? eg. Something like...

.... name="day_present_'.$i.'_12" ....


You also probably don't want your <form> element inside your while() loop. Echo this before your loop begins, otherwise you will end up with multiple form1's. (?)

You appear to have a redundant {...} (opening/closing curly brackets) inside your while loop, nested inside the while loops curly brackets.

L33t_J0rdan

3:08 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Sorry to be picky but for coding's sake can you put this in like a config1.php or something please?:

<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());
$course = "SELECT m.idMember, m.name AS member_name, a.Member_idMember, cl.Course_idCourse, co.idCourse, co.name, cl.idClass FROM member m, attendance a, Class cl, Course co WHERE m.idMember = a.Member_idMember AND a.Class_idClass = cl.idClass AND
cl.Course_idCourse = co.idCourse";
$result = mysqli_query($link,$course) or die(mysqli_error($link));
?>


This is what you sound like you're needing:

<?php
include 'config1.php';
$i = 0;
while($row = mysqli_fetch_array($result)) {
$i++;
echo "<td>".$row['member_name']."</td>";
echo "<td>".$row['idClass']."</td>";
echo '<form name="form1" method="post" action="new5.php">
<td>
<input type="radio" name="day_present01" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present01" value="Present" /> Present</label></td>
<td><label><input type="radio" name="day_present02" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present02" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present03" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present03" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present04" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present04" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present05" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present05" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present06"value="Absent" /> Absent</label>
<label><input type="radio" name="day_present06" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present07" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present07" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present08" value="Absent"/> Absent</label>
<label><input type="radio" name="day_present08" value="Present"/> Present</label>
</td>
<td><label><input type="radio" name="day_present09" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present09" value="Present"/> Present</label>
</td>
<td><label><input type="radio" name="day_present10" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present10" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present11" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present11" value="Present" /> Present</label>
</td>
<td><label><input type="radio" name="day_present12" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present12" value="Present" /> Present</label>
</td>';
}
echo "</tr>";
}
echo "</table><br />".$i;
mysqli_close($link);

?>


But I don't know what you want to do with the i variable... Sounds like you just want to print it..

rocknbil

5:28 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Currently my code work fine for selecting multiple radio button in a row but not in a column.


Welcome aboard appletea. Even if you get this working as posted, it still won't work with more than one record, and I'll tell you why. :-)

Let's take row 1, column 1.

<td>
<input type="radio" name="day_present01" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present01" value="Present" /> Present</label></td>

OK? Along comes row 2, column 1.

<td>
<input type="radio" name="day_present01" value="Absent" /> Absent</label>
<label><input type="radio" name="day_present01" value="Present" /> Present</label></td>

Look at that . . . you have the same names for the radio buttons in both rows. So when you click day_present_01, which value will get selected and which will get de-selected in the browser? And what's your receiving script going to receive? I'm sure there's an accurate answer, but if you have 8 records, the general answer is "Who knows" but it's likely to be the last day_present_01 in the document.

You need these unique for each row. Then on your recipient script, you need to parse them out uniquely. I don't have the time to code it all out for you, but here's a starter. Note the hidden field, this will be the "counter" your recipient script reads to loop through and know when to stop counting input, looking for the values.

Additionally, you don't even need $i (as a row counter, I use it differently below as a column counter.) You're going to do something with these records on update, right? Use the record id as the identifier.

The second problem is you've got forms improperly nested inside the table. The form has to go entirely inside one cell or wrapped around the entire table or you're going to get weird results, if any at all.

The third is you're not setting any checked states on the radio buttons. But you're not selecting those values from your database, so it's a topic for another day . . .

Before looking, note the invalid nesting:

</head>
<h1></h1>
<hr />
<h2>Attendance</h2>
<body>

S/B

</head>
<body>
<hr />
<h1>Attendance</h1>

Use the h1 and style it accordingly.



<?php
$row_content=null; // Squelch concatenation errors
//
// for the hidden field
$num_rows = mysqli_num_rows($result);
//
while($row = mysqli_fetch_array($result)) {
$columns=12; // Watch this var.
$row_content .= "
"<tr><td>".$row['member_name']."</td>
<td>".$row['idClass']."</td>
";
for($i=1;$i<=$columns;$i++) {
$radio_label = 'student_' . $row['idMember'] . '_day_present';
$radio_label .= ($i<10)?'0'+$i:$i;
// The previous will give:
// student_123423_day_present06
// student_123423_day_present07....
// student_123423_day_present10 (etc.)
$row_content .= "
<td>
<!-- if you had selected a value for 'present', you would do if's at the end of
each checkbox for checked=\"checked\". Note the proper use of <label> -->
<input type=\"radio\" name=\"$radio_label\" id=\"$radio_label\-absent\" value=\"Absent\" />
<label for=\"$radio_label\-absent\">Absent</label>
<input type=\"radio\" name=\"$radio_label\" id=\"$radio_label\-present\" value=\"Present\" />
<label for=\"$radio_label\-present\">Present</label>
</td>";
}
$row_content .= "</tr>";
}
//
mysqli_close($link);
//
if ($row_content) {
echo "
<form name=\"form1\" method=\"post\" action=\"new5.php\">
<input type=\"hidden\" name=\"tot_records\" value=\"$num_rows\">
<!-- if the below is constant through your scripts, don't need a hidden -->
<input type=\"hidden\" name=\"columns\" value=\"$columns\">
<table border=\"1\">
$row_content
</table>
</form>
";
}
else { echo "<p>No results found</p>"; }
?>


The previous typed out on the fly and untested, may contain errors . . . but should get you started.

Edit: a note, $row['idMember'] may be inaccurate as there many be multiple records if they have multiple classes. The point is, you can use some database value to assign a unique value to each radio button array, maybe combine member and class or something.

[edited by: rocknbil at 5:44 pm (utc) on Jun 16, 2010]

Readie

5:39 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$USERNAME = 'root';
$PASSWORD = '';

Strongly suggest you set a password for your root, and cease using the root user within your PHP scripts. Rather, set up a new user specifically for your PHP scripts.