bdmovies Posted October 5, 2007 Share Posted October 5, 2007 I've got an array that contains the column names of a table (all hand written in, it's not populated by a query) and then a nice display name...I have a drop down box that displays the array, my question is how can I tell my query to search based on the array key of the array, not the value (which is what is displayed in the drop box. The code below will explain. <option name="search_drop_box"> <?php $search_type = array ("case_number" => "Case Number", "uniqueID" => "Our ID", "defendant" => "Defendant", "addressone" => "Address One"); ksort($search_type); foreach( $search_type as $col_name => $display_name) { echo ' <select>$display_name</select>'; } $search_value = $_POST['search_value']; mysql_query = "SELECT * FROM case_information WHERE $_POST['search_drop_box'] = '$search_value'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/ Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 just a note: i assume you're trying to build an HTML form <SELECT> with that code, but if so it's not right. you need to define the <SELECT>, then add <options>, ala: echo "<SELECT NAME='select1'>"; foreach( $search_type as $col_name => $display_name) { echo "<option value='$col_name'>$display_name</option>"; } echo "</SELECT>"; Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362543 Share on other sites More sharing options...
bdmovies Posted October 5, 2007 Author Share Posted October 5, 2007 I do have all the HTML done, I just didn't retype it, I'm away from my work computer and was working on memory, but I didn't even think to use value, that totally makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362547 Share on other sites More sharing options...
bdmovies Posted October 5, 2007 Author Share Posted October 5, 2007 ok, now I'm getting this: Unknown column 'Array' in 'Where' Clause, here's the source code <form action="" method="post"> <?php function show_search_table() { echo ' <table border="0" style="background-color:#CCCCCC"> <tr><td> Search by:</td> <td> <select name="search_id"> '; $search_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID"); ksort($search_options); foreach( $search_options as $search_variables) { echo "<option value=$search_options>$search_variables</option>"; } echo ' </select> </td> <td><input type="text" name="search_value" /></td> <td><input type="submit" name="search" /></td> </tr></table> '; } $search_id = $_POST['search_id']; $search_value = $_POST['search_value']; $submit = $_POST['search']; if (isset($submit)) { $query = "SELECT * FROM case_information WHERE $search_id = '$search_value'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo " <table border=0> <tr><td class=case_information colspan=2>ISSUED IN THE COUNTY OF <B>{$row['county_issued']}</b><br> IN THE STATE OF <b>{$row['state_issued']}</b><br> COURT CASE NUMBER <b>{$row['case_number']}</b> </tr> <tr><td class=style_box>{$row['plaintiff']}<br><small>Plaintiff</small> <br><b>vs.</b><br> {$row['defendant']}<br><small>Defendant</small></td></tr> </table> "; } elseif(!isset($sumbit)) { show_search_table(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362569 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 remove the $ in front of $search_id in the WHERE clause: $query = "SELECT * FROM case_information WHERE $search_id = '$search_value'"; to $query = "SELECT * FROM case_information WHERE search_id = '$search_value'"; Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362579 Share on other sites More sharing options...
bdmovies Posted October 5, 2007 Author Share Posted October 5, 2007 I tried that, then I got 'unknown column in where clause' . The row the query selects is based on the value of the dropdown box, which is $search_id. Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362590 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 what is the name of the column are you trying to compare it to? SELECT * FROM table WHERE column_name = '$avalue' What should column_name be? Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362610 Share on other sites More sharing options...
bdmovies Posted October 5, 2007 Author Share Posted October 5, 2007 The column_name is based on whatever the user selects from the drop down box. Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362618 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 ah, okay. i think i see your problem: echo "<option value=$search_options>$search_variables</option>"; $search_options is an array. you're saying the value of this option is an array. you probably want to do something more like this: $search_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID"); ksort($search_options); foreach( $search_options as $search_value=>$search_variables) { echo "<option value='$search_value'>$search_variables</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362623 Share on other sites More sharing options...
bdmovies Posted October 5, 2007 Author Share Posted October 5, 2007 YAY! That worked. Any chance you can explain what you did? Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362635 Share on other sites More sharing options...
BlueSkyIS Posted October 5, 2007 Share Posted October 5, 2007 yes, you were looping over the values of the array, but inserting the array itself as the value of each OPTION: foreach ($somearray AS $array_value) { echo "<OPTION VALUE='$somearray' /////WRONG ... you need to use the keys and values of the array ala: foreach ($somearray AS $a_key=>$array_value) { echo "<OPTION VALUE='$a_key' /// that's better... Quote Link to comment https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/#findComment-362665 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.