Need drop down menu to have existing value as selected default

I am currently working on what I consider a basic Helpdesk system
in order to try and improve my coding skills.I’m not overly experienced.

I have come across the following problem.

I currently see a list of incidents assigned to me on the home page once logged in.
I can click on a button to edit the details of a specific incident.
This brings me to a new page showing all the details, some of which I can change with a drop down menu.

To change the status of an incident I use the following code…

echo “”;
echo “”;
echo status_dropdown();
echo “”;

The status_dropdown() function exists in a seperate page (functions.php)

function status_dropdown()
{

$options = mysql_query(“select * from status”);
while($single_option = mysql_fetch_array($options))
{echo “<option value=’” . $single_option[‘status_id’] ."’>" . $single_option[‘status’] ."";}
}

This all works but I have spotted a issue which could lead to problems down the road.

When I edit an incident and try to change the status of that incident (e.g.from ‘Assigned’ to ‘Closed’)
The dropdown menus do not have the existing value (‘Assigned’) selected as default but show the first value (‘Unassigned’) generated by the while loop from the ‘Status’ table.

This means if I want to edit a specific incident I have to check each dropdown menu to make sure they are set to the correct value (even though I may only want to change one value) before I save the changes.

I hope this makes sense and you can help me.

Thanks,
Seán

It makes complete sense…

You will need to query the status of the ticket you are currently viewing… I don’t know the sql statement for that, because I know know how you database is structured.

[php]function status_dropdown()
{
/Get the current/
/*You’ll need to change the query to be correct. */
$CurrentStatusQuery = mysql_query(“select Current_status_ID from Myticket tabel where ticketid = currentticketid”);
$result = mysql_query(CurrentStatusQuery );
$Current_Status_ID = mysql_fetch_object($result);

$options = mysql_query(“select * from status”);

while($single_option = mysql_fetch_array($options))
{
if ($single_option[‘status_id’] == $Current_Status_ID )
{
/*Notice I added the word selected to this option because it matched */
echo “<option value=’” . $single_option[‘status_id’] ."’ selected>" . $single_option[‘status’] ."";
}
else
{
echo “<option value=’” . $single_option[‘status_id’] ."’>" . $single_option[‘status’] ."";
}

}[/php]

And as others will tell you, you should convert this to PDO, because you’re using obsolete mySql functions.

Sponsor our Newsletter | Privacy Policy | Terms of Service