rsammy Posted May 13, 2007 Share Posted May 13, 2007 this is my code... <tr> <td height="22"><div align="right"><font class="inputLbl">Location: </font></div></td> <td colspan="2" align="left" valign="top"><select name="location" id="location"> <?PHP $query_loc="select org_ID, org_name from orgs where org_id!=1 order by org_name"; $result_loc = mysql_query ($query_loc); if ($result_loc) { //print ("<select name='location'>\n"); print ("<option value=\"\"></option>\n"); while($row_loc = mysql_fetch_array($result_loc)) { if($row->admit_loc == $row_loc["org_name"]) { print ('<option selected value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } else { print ('<option value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } } print ("</select>"); } ?> </td> </tr> this field, Location, is one of the search filters(parameters) on a screen. What it does is display a list of all patients in a particular location(hospital). User selects a location from the drop-down list and hits on Search button on screen to display results for that location. The whole thing works fine. except that, after the search is displayed(the same screen reloads), on the list screen, this drop-field goes back to the default value - blank. it does not show the selected location. i would like to show the selected location so users know what location was chosen. help anyone? Quote Link to comment https://forums.phpfreaks.com/topic/51144-help-with-a-dropdown-field/ Share on other sites More sharing options...
chronister Posted May 13, 2007 Share Posted May 13, 2007 Basically you have to do something like is already there if($row->admit_loc == $row_loc["org_name"]) { print ('<option selected value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } else { print ('<option value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } you need to make an if / else statement comparing the search term with the option that is being looped through from the database. If they match, you need to make the echo part have a selected in it, if it does not match, then no selected value Quote Link to comment https://forums.phpfreaks.com/topic/51144-help-with-a-dropdown-field/#findComment-251799 Share on other sites More sharing options...
rsammy Posted May 13, 2007 Author Share Posted May 13, 2007 ???? thanx for replying, but sorry! i didnt get you! could you be more specific? Quote Link to comment https://forums.phpfreaks.com/topic/51144-help-with-a-dropdown-field/#findComment-251808 Share on other sites More sharing options...
chronister Posted May 13, 2007 Share Posted May 13, 2007 while($row_loc = mysql_fetch_array($result_loc)) { if($row->admit_loc == $row_loc["org_name"]) { print ('<option selected value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } else { print ('<option value="'.$row_loc["org_ID"].'">'.$row_loc["org_name"].'</option>'); } } In this chunk of code your using the while loop, to loop through all results. inside that while loop, your comparing $row->admin_loc to $row_loc["org_name"]. If they are equal, then you are printing out an <option> tag with the word selected in it. Basic HTML....in an option list, the word selected will make that item come up as the one displayed. e.g. <option value="abcdef">Option1</option> <option value="xyzabc" selected>Option2</option> This will make Option2 come up as the one displayed. In your code above, if $row->admin_loc is equal to $row_loc["org_name"] then your printing out an option item with the word selected, making it the displayed value. If they are not equal, then it just populates the rest of the option list with the items from the database. So now you have to take the item that was selected by the user, and compare it with the items being populated in the while loop. If they match, then you need to echo the word selected inside the <option > tag. This will make the item they have chosen to sort by be the one displayed. Since I don't know what the rest of your code is and what your database holds I don't know exactly what those variables represent. But that code there is almost exactly what you need to use to make the search term be displayed. You simply have to compare $_POST['location'] to the corresponding value from the database. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/51144-help-with-a-dropdown-field/#findComment-251812 Share on other sites More sharing options...
rsammy Posted May 14, 2007 Author Share Posted May 14, 2007 thanx chronister. i guess i understand this one little more clearly. still, not too sure i can do it myself. here is my code(remaining part, actually). this block is the POST block: if($client_id= $_POST["client_id"]) { $m1=$_POST["m1"]; $d1=$_POST["d1"]; $y1=$_POST["y1"]; $m2=$_POST["m2"]; $d2=$_POST["d2"]; $y2=$_POST["y2"]; $patfname=$_POST["patfname"]; $patlname=$_POST["patlname"]; $org_name=$_POST["location"]; print("LOC IS...: " .$org_name); $searchon=$_POST["searchon"]; $client_id=$_POST["client_id"]; $searchon=$_POST["searchon"]; if ($client_id == 'NULL') { $client_id= '0'; } $patdob=$_POST["patdob"]; $ActiveStatus=$_POST["ActiveStatus"]; } this is my query: $queryexec="SELECT DISTINCT admission.id, admission.pat_id, pat_dgraphics.pat_first_name, pat_dgraphics.pat_last_name, admission.admit_phy_id, admission.admit_loc, admission.admit_encounter, admission.admit_date, admission.discharge_date, DATE_FORMAT(admission.discharge_date, '%m/%d/%Y') as discharge_dates, DATE_FORMAT(admission.admit_date, '%m/%d/%Y') as admit_dates, CONCAT(DATEDIFF(curdate(), admission.admit_date) + 1, ' <b><font color=green>ACTIVE</font></b>') as days, admit_time, discharge_time FROM admission, pat_dgraphics WHERE admission.discharge_date = '0000-00-00' AND admission.admit_date <= '$dates' AND admission.admit_date >= '$datez' AND admission.pat_id=pat_dgraphics.pat_ID AND pat_dgraphics.pat_first_name like '$patfname%' AND pat_dgraphics.pat_last_name like '$patlname%' AND admission.admit_loc like '%$location%' AND admission.admission_client_id like '$client_id' AND DATE_FORMAT(pat_dgraphics.pat_dob, '%m/%d/%Y') like '$patdob' ORDER BY admit_date DESC, admit_time DESC, pat_last_name ASC, pat_first_name ASC"; These are the filters I am using for the query: <tr> <td width="15"> </td> <td rowspan="3" align="left" valign="top" width="498"> <form method="post" action="Encountersearch.php" name="Form" onSubmit="return validate()"> <table width="497" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="14" width="232"> </td> <td height="14" width="265"> </td> </tr> <tr> <td height="14" width="232"> </td> <td height="14" width="265"> </td> </tr> <tr> <td height="14"> <div align="right"><font class="inputLbl">First Name: </font></div></td> <td height="14" width="265"> <input class="txtboxLarge" type="text" name="patfname" value="<? if (isset ($patfname)){ print ("$patfname"); } ?>" > </td> </tr> <tr> <td height="11"> <div align="right"><font class="inputLbl">Last Name: </font></div></td> <td height="11" width="265"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <input class="txtboxLarge" type="text" name="patlname" value="<? if (isset($patlname)){ print ("$patlname"); } ?>" > </font></td> </tr> <tr> <td height="22"><div align="right"><font class="inputLbl">Location: </font></div></td> <td colspan="2" align="left" valign="top"><select name="location" id="location"> <?PHP $query_loc="select org_ID, org_name from orgs where org_id!=1 order by org_name"; $result_loc = mysql_query ($query_loc); if ($result_loc) { while($row_loc = mysql_fetch_array($result_loc)) { if($row->admit_loc == $row_loc["org_name"]) { print ('<option selected value="'.$row_loc["org_name"].'">'.$row_loc["org_name"].'</option>'); } else { print ('<option value="'.$row_loc["org_name"].'">'.$row_loc["org_name"].'</option>'); } $location = $row_loc["org_name"]; } print ("</select>"); } ?> </td> </tr> <tr> <td height="22"><div align="right"><font class="inputLbl">Date of Birth as (mm/dd/yyyy): </font></div></td> <td height="22"><input name="patdob" type="text" class="txtboxLarge" id="patdob" value="<? if (isset($patdob) & ($patdob != '%%')) { print ("$patdob"); } ?>" ></td> </tr> and then I display the records - row by row hope this helps. thanx Quote Link to comment https://forums.phpfreaks.com/topic/51144-help-with-a-dropdown-field/#findComment-252916 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.