I'm looking for some help with creating radio buttons from a database. Here is some of the code that I have found that partially does what I need it to do with just a few changes that I need some help with:
Code: Select all
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
. $db->connect_error;
} else {
$sql = "SELECT shift, title as name FROM schedule";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
$aa_student = array();
Code: Select all
<?php
function myradio($array, $checked, $name, $return=0, $option=1) {
if (count($array) <= 0) {
return;
}
$str_radio = "";
if ($option == 1) {
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] == $checked) {
$str_radio .=
"<input type=\"radio\"
name=\"{$name}\"
value=\"{$array[$i]}\"
id=\"id{$array[$i]}\"
checked=\"checked\"/>";
$str_radio .=
"<label
for=\"id{$array[$i]}\">$array[$i]</label>";
} else {
$str_radio .=
"<input type=\"radio\"
name=\"{$name}\"
value=\"{$array[$i]}\"
id=\"id{$array[$i]}\"/>";
$str_radio .=
"<label for=\"id{$array[$i]}\">$array[$i]</label>";
}
}
}
if ($option == 2) {
foreach ($array as $value => $label) {
if ($value == $checked) {
$str_radio .=
"<input type=\"radio\"
name=\"{$name}\"
value=\"{$value}\"
id=\"id{$value}\"
checked=\"checked\"/>";
$str_radio .=
"<label for=\"id{$value}\">{$label}</label>";
} else {
$str_radio .=
"<input type=\"radio\"
name=\"{$name}\"
value=\"{$value}\" id=\"id{$value}\"/>";
$str_radio .=
"<label for=\"id{$value}\">{$label}</label>";
}
}
}
if ($return) {
return $str_radio;
} else {
echo $str_radio;
}
}
?>What is not working for me is that I need the following to happen:
I need the radio buttons to be created from a drop-down menu (Shifts), which I already have working,
On selecting the shift, I need to create:
1. A list of agents
2. A series of 3 radio buttons for each agent
The values of the radio buttons will need to be the agents name.
Then on submitting the form each agent's selected radio button needs to be populated into the relevant column in a table.
The table has 5 columns
Date - This is the date of the shift
Shift - The actual shift
Present - Radio Button 1
Late - Radio Button 2
Absent - Radio Button 3
Any help will be appreciated. Thanks guys. I have looked all over on the web for help with this, but haven't been able to find something that works for me. And yes, the section of code here is not code that I wrote myself, but rather a 3rd party code that does work to a certain degree.