SalientAnimal 0 Newbie Poster

Hi All,

I have managed to get a few bits and pieces of code together, that I would like to try and combine to get the desired result for my form. The top section of the code is code that I had originally created myself and had some assistance in fixig it, the later part is code I had found that almost does what I need it to, but I am having poblems getting the code to work together and thus do what I require it to do.

What I want to achieve on my form is the following:

A user selects a "shift" from a drop-down that is created from my MySQL database.

This list is created using the following:

function createOptions($optionList, $selectedValue)
{
    $options = '';
    foreach ($optionList as $option)
    {
        $selected = ($option['value']==$selectedValue) ? ' selected="selected"' : '';
        $options .= "<option value='{$option['value']}'{$selected}>{$option['label']}</option>\n";
    }
    return $options;
}


//DETERMINE SELECTED OPTIONS PASSED ON QUERY STRING
$shift   = isset($_GET['shift'])   ? intval($_GET['shift'])   : false;
//$agent = isset($_GET['agent']) ? intval($_GET['agent']) : false;
//$tertiary_category  = isset($_GET['tertiary_category'])  ? intval($_GET['tertiary_category'])  : false;








//GENERATE OPTIONS FOR THE SHIFT OPTIONS
$query = "SELECT DISTINCT id AS value, shift AS label
          FROM shift_structure
 WHERE active_status = 1
          ORDER BY id"; 
$optionList = $dbo->query($query);
$shift_options = createOptions($optionList, $shift);

This script reloads my form:

<script language="javascript">


        function getSelectValue(selectID)
        {
            var optionObj = document.getElementById(selectID);
            return optionObj.options[optionObj.selectedIndex].value;
        }


        function reload(form)
        {
            //Adding the unselected options should work fine
            var locationURL = 'shift_form_schedule.php';
                locationURL += '?shift='   + getSelectValue('shift');
         //       locationURL += '&secondary_category=' + getSelectValue('secondary_category');
        //        locationURL += '&tertiary_category='  + getSelectValue('tertiary_category');
            //Perform the reload
            self.location = locationURL;
        }
 </script>

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

The radio button columns need to be populated with the agents name, depending on which button was selected in the form.

This is a bit of code that I found that will create my radio buttons, however, I do not know how to:
1. Include it into my form.
2. Exclude the database connection section of the code as I already have this in my includes at the top of the form.
3. AdjuSt the code to create 3 radio buttons per agent, currently is only creates 1 radio button for each agent. And I can only select one button at a time. I need to be allowed to select 1 button per agent.

<?php
require_once 'includes/myradio.php';


define('HOST', 'localhost');
define('USER', 'username');
define('PASS', 'password');
define('DBNAME', 'database');


$db = new mysqli(HOST, USER, PASS, DBNAME);


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();


while ($row = $result_db->fetch_object()) {
$aa_student[$row->shift] = $row->name;
}
myradio($aa_student, 10, 'STUDENT_RADIO', 0, 2);
}
}
$db->close();

Here is the functions for creating the radio buttons:

<?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;
}
}

?>

Please let me know if you require any additional information.