Jump to content

Radio Buttons From MySQL & Drop-Down Menu


SalientAnimal

Recommended Posts

Hi All,

 

So I finally got my code to work, loading the different users as per the drop-down selection with my 4 radio buttons next to each user. The challenge I'm now having with my form however is that initially there is an error when my form is opened. This is because there are no values selected in the second query. 

 

Basically what I want to know, is how to a prevent the error from displaying? 

 

Below is the code I am using:

<?php
include_once 'includes/functions.php';
include_once 'includes/register.inc.php';	
include_once 'includes/session_management.php';
include_once 'includes/formatting.php';
include_once 'includes/db_connect.php';

// INCLUDING THE TOP LOGIN / LOGOUT PANEL
include 'includes/panel.php';

// INCLUDING THE NAVIGATION MENU
include '/nav/menu.html';
 
?>


<?php
 
//FUNCTION TO CREATE HTML FOR OPTIONS LIST
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 = $db->query($query);
$shift_options = createOptions($optionList, $shift);
 



//SELECTING THE AGENTS THAT ARE SCHEDULE FOR THE MENTIONED SHIFT 
if($shift)
{
    $query = "SELECT shift AS value, agent AS label
              FROM schedule
              WHERE id = $shift";
    $optionList = $db->query($query);
    $agent_options = createOptions($optionList, $shift);
}

?>







<html>
<head>
<title>Agent Analysis</title>
    <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_schedule3.php';
                locationURL += '?shift='   + getSelectValue('shift');
         //       locationURL += '&secondary_category=' + getSelectValue('secondary_category');
        //        locationURL += '&tertiary_category='  + getSelectValue('tertiary_category');
            //Perform the reload
            self.location = locationURL;
        }
 </script>

</title>
</head>
<body marginheight="0" topmargin="0" marginwidth="0" 
leftmargin="0" style="margin:0;padding:0" bgcolor="#B0E0E6">

    <div id="container">
		<div id="content" style="margin-top:-45px;">
		<img src="images/logo.png" alt="none"></img>
			<h1>Auxilium</h1>
			<!-- <h2>Sliding login panel with jQuery - Demo</h2>	 -->
		
		<div id="stylized" class="form">
			<form id="form" name="form" method="post" action="process/submit_agent_analysis.php">
			<h1>Agent Analysis</h1>
			
			<label>User Logged In :
			<span class="small">You are logged in as</span>
			</label>
			<input type="text" name="username" id="username" value="<?php echo htmlentities($_SESSION['username']);?>" readonly style="background-color: #C9C9C9">
			
			
			<!-- DISPLAY THE DETAILS OF THE LOGGED IN USER 
			<label>Form Reference Number :
			<span class="small">Number Reference to link queries</span>
			</label>-->
			<input type="hidden" name="voc_reference" id="voc_reference" value="<?php echo $random;?>" readonly style="background-color: #C9C9C9">			
			
			<label>Shift :
			<span class="small">Shift that is being updated</span>
			</label>
    		<select name='shift' id='shift' onChange="reload(this.form)">
       		<option value=''>--- Select a Shift ---</option>
        	<?php echo $shift_options; ?>
    		</select>			
			
			
			


<?php
$query = "	SELECT 
					DISTINCT(agent)
			, 		agent_id 
			FROM 	
					schedule
			WHERE
					shift = $shift
			AND		DATE(start) = curdate()
			";
$result = $db->query($query);
$num_results = $result->num_rows;
function db_result_to_array($result){
//---------------------------------------------------------------------


for ($count=0; $row = $result->fetch_assoc(); $count++) {
$res_array[$count]=$row;
}

return $res_array;
}
//---------------------------------------------------------------------


$irow=0;
while($row = $result->fetch_assoc())
 {

if($row['agent']==1)
$checked=" checked='checked'";
else
$checked='';

if($row['agent']==1)
$checked2=" checked='checked'";
else
$checked2='';

if($row['agent']==1)
$checked3=" checked='checked'";
else
$checked3='';

if($row['agent']==1)
$checked4=" checked='checked'";
else
$checked4='';


echo "<table><tr>";
echo			"<td><label>".$row['agent'].":";
echo			"<span class='small'>Agent's attendance status</span>";
echo			"</td>";
echo "<td><input type='radio' name='".$row['agent']."[]' value='1'".$checked."/>" ;
echo			"<span class='radiobutton'>Present</span>";
echo			"</td>";
echo "<td><input type='radio' name='".$row['agent']."[]' value='2'".$checked2."/>" ;
echo			"<span class='radiobutton'>Late</span>";
echo			"</td>";
echo "<td><input type='radio' name='".$row['agent']."[]' value='3'".$checked3."/>" ;
echo			"<span class='radiobutton'>Absent</span>";
echo			"</td>";
echo "<td><input type='radio' name='".$row['agent']."[]' value='4'".$checked4."/>" ;
echo			"<span class='radiobutton'>AWOL</span>";
echo			"</label></td>";
echo "</tr>";

$irow++;

 }
 echo "</tr></table>";

var_dump($_POST);

?>


</form>
</body>
</html>
Link to comment
Share on other sites

some suggestions -

 

1) please, when you get an error or some other symptom in front of you when you run your code, share that information by telling us what it actually was that you saw and need help with. you have three queries and dozens of php variables. we don't have a clue what you saw in front of you that would even point to the correct section of code to look at, especially since you are not just posting the relevant section of code.

 

2) the organization of your code needs some help, which is also related to just posting the relevant section of code. you have your php 'business' logic mixed in with javascript and html, along with php function definitions.

 

you are including a 'functions.php' file. why aren't the two function definitions that are scattered around in the posted code contained in the function.php file?

 

you need to separate the code that determines what to do on the page and is responsible for getting the correct data at the correct time (i.e. the 'business logic') from the code producing/including the actual html document (i.e. the 'presentation logic'). you should be able to draw a line in your code, above which is a majority of the php code, containing no html/javascript/css markup, and below which contains your <doctype> tag and all the html/javascript/css markup, with the only php code being that which is needed to output the content that the business logic determined and got the data for.

 

by having this separation, you can concentrate on the correct section of code (and post just the relevant section in a help forum.)

Link to comment
Share on other sites

the following is your posted code, rearranged as described (except for removing an extra </title> tag that is saw, i didn't fix or intentionally alter any of the code) -

<?php
include_once 'includes/functions.php';
include_once 'includes/register.inc.php';
include_once 'includes/session_management.php';
include_once 'includes/formatting.php';
include_once 'includes/db_connect.php';

// ---------------------------- these two functions should be in functions.php -------------------------
//FUNCTION TO CREATE HTML FOR OPTIONS LIST
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;
}

function db_result_to_array($result)
{
    for ($count=0; $row = $result->fetch_assoc(); $count++) {
        $res_array[$count]=$row;
    }
    return $res_array;
}

// ------------ these two includes apparently have something to do with producing the html document,
// but you had them before the <head> tag so i don't know where they belong at on this page -------------
// INCLUDING THE TOP LOGIN / LOGOUT PANEL
include 'includes/panel.php';
// INCLUDING THE NAVIGATION MENU
include '/nav/menu.html';


// ----------- the following is the business logic, that determines what to do, when to do it,
// and gets the data that the presentation logic needs ------------------------------------------------

//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 = $db->query($query);
$shift_options = createOptions($optionList, $shift);

//SELECTING THE AGENTS THAT ARE SCHEDULE FOR THE MENTIONED SHIFT
if($shift)
{
    $query = "SELECT shift AS value, agent AS label
    FROM schedule
    WHERE id = $shift";
    $optionList = $db->query($query);
    $agent_options = createOptions($optionList, $shift);
}

$query = "SELECT
    DISTINCT(agent),
    agent_id
    FROM schedule
    WHERE shift = $shift
    AND DATE(start) = curdate()
    ";
$result = $db->query($query);
$num_results = $result->num_rows;


// ---------------------------- the presentation logic is below this line ----------------------------

<html>
<head>
<title>Agent Analysis</title>
<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_schedule3.php';
    locationURL += '?shift='   + getSelectValue('shift');
    //       locationURL += '&secondary_category=' + getSelectValue('secondary_category');
    //        locationURL += '&tertiary_category='  + getSelectValue('tertiary_category');
    //Perform the reload
    self.location = locationURL;
}
</script>
</head>
<body marginheight="0" topmargin="0" marginwidth="0"
leftmargin="0" style="margin:0;padding:0" bgcolor="#B0E0E6">

<div id="container">
<div id="content" style="margin-top:-45px;">
<img src="images/logo.png" alt="none"></img>
<h1>Auxilium</h1>
<!-- <h2>Sliding login panel with jQuery - Demo</h2>     -->

<div id="stylized" class="form">
<form id="form" name="form" method="post" action="process/submit_agent_analysis.php">
<h1>Agent Analysis</h1>

<label>User Logged In :
<span class="small">You are logged in as</span>
</label>
<input type="text" name="username" id="username" value="<?php echo htmlentities($_SESSION['username']);?>" readonly style="background-color: #C9C9C9">

<!-- DISPLAY THE DETAILS OF THE LOGGED IN USER
<label>Form Reference Number :
<span class="small">Number Reference to link queries</span>
</label>-->
<input type="hidden" name="voc_reference" id="voc_reference" value="<?php echo $random;?>" readonly style="background-color: #C9C9C9">

<label>Shift :
<span class="small">Shift that is being updated</span>
</label>
<select name='shift' id='shift' onChange="reload(this.form)">
<option value=''>--- Select a Shift ---</option>
<?php echo $shift_options; ?>
</select>

<?php
$irow=0;
while($row = $result->fetch_assoc())
{
    if($row['agent']==1)
        $checked=" checked='checked'";
    else
        $checked='';

    if($row['agent']==1)
        $checked2=" checked='checked'";
    else
        $checked2='';

    if($row['agent']==1)
        $checked3=" checked='checked'";
    else
        $checked3='';

    if($row['agent']==1)
        $checked4=" checked='checked'";
    else
        $checked4='';

    echo "<table><tr>";
    echo            "<td><label>".$row['agent'].":";
    echo            "<span class='small'>Agent's attendance status</span>";
    echo            "</td>";
    echo "<td><input type='radio' name='".$row['agent']."[]' value='1'".$checked."/>" ;
    echo            "<span class='radiobutton'>Present</span>";
    echo            "</td>";
    echo "<td><input type='radio' name='".$row['agent']."[]' value='2'".$checked2."/>" ;
    echo            "<span class='radiobutton'>Late</span>";
    echo            "</td>";
    echo "<td><input type='radio' name='".$row['agent']."[]' value='3'".$checked3."/>" ;
    echo            "<span class='radiobutton'>Absent</span>";
    echo            "</td>";
    echo "<td><input type='radio' name='".$row['agent']."[]' value='4'".$checked4."/>" ;
    echo            "<span class='radiobutton'>AWOL</span>";
    echo            "</label></td>";
    echo "</tr>";

    $irow++;
}
echo "</tr></table>";
?>
</form>
</body>
</html>

perhaps start with this, and work on getting each part of the business logic to do what you want, then get each corresponding part of the presentation logic to do what you want.

Link to comment
Share on other sites

I understand what you are suggesting and will make the changes as suggested.

 

What I normally do is use the functions in the actual form before moving it to my functions.php file. This I will still do once everything is working.

 

Sorry I forgot to include the error messages, these are the messages that I get:

Notice: Trying to get property of non-object in C:\autopage_auxilium\htdocs\shift_form_schedule3.php on line 141

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\autopage_auxilium\htdocs\shift_form_schedule3.php on line 156

I still have a long way to go with what I am learning on my coding. So I do apologize for my disorganized code.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.