Page 1 of 1

Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Thu Apr 17, 2014 2:55 am
by SalientAnimal
Hi All,

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();
The below function creates the radio buttons:

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.

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 2:52 am
by social_experiment
SalientAnimal wrote: 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
1. For the list of agent you need to compare the shift / shift date (unsure about whether you compare using a string like 'day shift' or a date value) to the shifts / shift dates of the agents within the database. Once you have the result you can place them inside an array, that will give you the list of names.

2. The second question is a bit puzzling; the script requires 3 radio buttons per agent; and your database table has 3 columns, on for each status of an agent (absent / late / present) but values of the radio buttons has to be the name or each agent? It is possibly a logic error in my thinking but if you could clarify that bit it would be helpful. My thinking is that for each of the radio buttons you would want a status ( absent / late / present ) and that status would be sent to the database?

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 3:13 am
by SalientAnimal
I have made a few changes to my structure and requirements since this post, looking for other solutions as I had not received any replies on the tread.

The changes are as follows:
My table has 4 Columns
[*]register_id - Auto increment id for the primary key
[*]agent_id - Id that is used to reference back to the particular agent in my user table
[*]event_date - The date that the event is captured / updated
[*]status - Status indicator 0 = Present, 1 = Late, 2 = Absent, 3 = AWOL

I have managed to get my radio buttons populating and updating my table as required. The only challenge / unanswered question I have left is:
[*]Selecting a date, so that events can be updated for days that may have past.
[*]Selecting only the agents working a specific shift - The drop-down menu here returns an INT which refers to a string in another reference table.

[*]The above two points need to work together, i.e. First select the date, once the date is selected, only the shifts available that day must be available in the drop-down menu.[/color]

If you like, I could possible give some sample cade?

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 5:42 am
by Celauran
SalientAnimal wrote:[*]Selecting a date, so that events can be updated for days that may have past.
Not sure I understand the problem here. Why wouldn't a date picker work?
SalientAnimal wrote:[*]Selecting only the agents working a specific shift - The drop-down menu here returns an INT which refers to a string in another reference table.
So join the other table on that ID.
SalientAnimal wrote:[*]The above two points need to work together, i.e. First select the date, once the date is selected, only the shifts available that day must be available in the drop-down menu.
You could attach an event listener to the date field and update your shifts accordingly. Alternately, multi step form.

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 5:50 am
by SalientAnimal
Celauran wrote:
SalientAnimal wrote:[*]Selecting a date, so that events can be updated for days that may have past.
Not sure I understand the problem here. Why wouldn't a date picker work?
I am not sure how to link the date picker to the next query in my code leading up to the "Shift" selection.
Celauran wrote:
SalientAnimal wrote:[*]Selecting only the agents working a specific shift - The drop-down menu here returns an INT which refers to a string in another reference table.
So join the other table on that ID.
Sorry I should of mentioned that I am ok with this part.
Celauran wrote:
SalientAnimal wrote:[*]The above two points need to work together, i.e. First select the date, once the date is selected, only the shifts available that day must be available in the drop-down menu.
You could attach an event listener to the date field and update your shifts accordingly. Alternately, multi step form.
[/quote]
Please could you elaborate on the event listener?

I have a drop-down menu, which does create the correct agents for me when selecting a shift, I just want to be able to take this one step back by adding the calendar / date picker.

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 5:52 am
by SalientAnimal
Sorry I should have also mentioned that the code I am currently using, is substantially different to that which I posted when opening this thread.

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Wed Apr 30, 2014 6:20 am
by Celauran
SalientAnimal wrote:Please could you elaborate on the event listener?

I have a drop-down menu, which does create the correct agents for me when selecting a shift, I just want to be able to take this one step back by adding the calendar / date picker.
Attach a listener to the date picker. On change, grab the value from the date picker and fire off an AJAX request to get the list of shifts available for that date. Use the AJAX response to populate your drop down.

Code: Select all

$('#your-date-field').change(function() {
	var date = $(this).val();
	$.ajax({
		url: 'some-script.php',
		type: 'POST',
		data: { date: date }
	})
	.done(function(response) {
		$('#your-drop-down').html(response);
	});
})

Re: Create Radio Buttons From MySQL and Drop-Down Selection

Posted: Fri May 09, 2014 4:42 am
by jowettgo
Wow, overwhelming codes! These sure are great concepts and scripts... these line of codes reminds me of a phpscript on which I got from codebasket . net.. would love to use these codes you mentioned as well. Thanks!