Jump to content

Switching Array's based on Selection


bdmovies

Recommended Posts

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'";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/71975-switching-arrays-based-on-selection/
Share on other sites

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>";

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();
}

?>

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>";
}	

 

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.