Jump to content

Drop Down filling from query not working


bigfuzzydope

Recommended Posts

I have a table where I want to have multiple columns searchable. I can get the dropdown to only work with one column. Is there a way this can be done? What works is below...

Now there are two questions really. One is how can I select a series of columns that all have the same name except for a number of the end.. such at input1, input2, input3 etc

and the second question is how would it be best to get all the selected columns to show in the dropdown? If I deviate from a single choice (say input2) the dropdown box is empty.



<html>
<head>
<title>Background Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
< connection info removed>
<?php
$query = "SELECT DISTINCT input2 FROM `searchtest`";
$result = mysql_query($query)
or die ("Could not execute query.<br>".mysql_error()."<br>");

$option="<select name=\"input2\"><option value=\"\"></option>";
while ($row = mysql_fetch_array($result))
$option="$option <option value=\"$row[input2]\">$row[input2]</option>";
$option="$option </select>";
//echo("<br>result=$result<br>row=$row<br>");
//echo("query=$query");
?>
<form name="form1" method="post" action="<?=$PHP_SELF?>">
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Trait</td>
<td><div align="center">Min Level</div></td>
<td><div align="center">Max Level</div></td>
<tr>

<td width="40%"><?=$option?></td>
<td width="15%" align="center"><select name="Min" id="Min">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
<td width="15%" align="center"><select name="Max" id="Max">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
</table>
<div align="center">
<input type="submit" name="Select" id="Select" value="Select">
</div>

<?php
if($_POST[Select]=="Select"){
//echo ("$input2<br>");
//echo ("$Min<br>");
//echo ("$Max");
$query = "SELECT name,input2val FROM `searchtest` WHERE (input2='$input2' AND (input2val >= $Min AND input2val <= $Max)) ORDER BY input2val DESC";
$result = mysql_query($query) or die ("Could not execute query.<br>".mysql_error()."<br>");
{
?>
<!-- Table that displays the results -->
<hr>
<table width="200" border=1 align="center">
<caption>
<p align="center"><?="$input2"?></p>
</caption>
<tr>
<td>Name</td>
<td align="center">Level</td>
</tr>
<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo "<TR>";
$row_array = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo "<TD>" . $row_array[$j] . "</td>";
}
echo "</tr>";
}
}
} ?>
</table>
</form>
Link to comment
Share on other sites

What exactly are you trying to do here? If you want all the data to have the same query you must post the information before you can query it, or it won't get the results. You have to post the form in order to use the data it has.

[code]if($_POST[Select]=="Select")[/code]

That row, I'd change to

[code]if(isset($_POST['Select']))[/code]

Seems like to me, that you're confusing alot of things, but let me know what you're trying to do exactly.
Link to comment
Share on other sites

[!--quoteo(post=350997:date=Mar 2 2006, 11:05 AM:name=Christopher Robin)--][div class=\'quotetop\']QUOTE(Christopher Robin @ Mar 2 2006, 11:05 AM) [snapback]350997[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What exactly are you trying to do here? If you want all the data to have the same query you must post the information before you can query it, or it won't get the results. You have to post the form in order to use the data it has.

[code]if($_POST[Select]=="Select")[/code]

That row, I'd change to

[code]if(isset($_POST['Select']))[/code]

Seems like to me, that you're confusing alot of things, but let me know what you're trying to do exactly.
[/quote]


The form I am working with has two text input fields "input1" and "input2" that when submitted go into their own columns of the same name, this where they can list say two of their favorite fruits, one in each. what I am trying to do is to create a dropdown so I can pull data from both columns so I can then generate a list all the users that have listed a distinct fruit name. (as well as a desired range of quantity) As the code is I can only pull one column into the dropdown generated by the $option.

That help?
Link to comment
Share on other sites

So basically all you really want, is for someone to be able to select two different types of fruit, and list the members who have those fruits listed in their say.. profile or of what they like.

Well all you really have to do is, post each drop down. Say the first drop down is named input1 and the second input2, just do this on the next page or the page the code exists on.

$input1=$_POST['input1'];
$input2=$_POST['input2'];

This will post the information within the drop down. So say you were making an if statment.

[code]if ($input1=='Apple')
   {
     do something here
   }
elseif ($input1=='Orange')
   {
     do something here
   }[/code]
Link to comment
Share on other sites

[!--quoteo(post=351230:date=Mar 2 2006, 10:26 PM:name=Christopher Robin)--][div class=\'quotetop\']QUOTE(Christopher Robin @ Mar 2 2006, 10:26 PM) [snapback]351230[/snapback][/div][div class=\'quotemain\'][!--quotec--]
So basically all you really want, is for someone to be able to select two different types of fruit, and list the members who have those fruits listed in their say.. profile or of what they like.

Well all you really have to do is, post each drop down. Say the first drop down is named input1 and the second input2, just do this on the next page or the page the code exists on.

$input1=$_POST['input1'];
$input2=$_POST['input2'];

This will post the information within the drop down. So say you were making an if statment.

[code]if ($input1=='Apple')
   {
     do something here
   }
elseif ($input1=='Orange')
   {
     do something here
   }[/code]
[/quote]


Not quite. take a table that looks like this.
name | fruit1 |fruit1num |fruit2 |fruit2num
James |apple |3 |pear |2
Jim |orange |4 |plum |1
Jill | kiwi |2 |apple |6


I want to be able to create a single dropdown that will list the distinct entries from both colums fruit 1 and fruit 2. As my script is now I can only get one column to list in the dropdown.

The end result I am aiming for is to be able to select from a single drop down any fruit name from any number of colums (in this example 2) as users are given the option to list in two different text fields to list any fruit. Then to be able to search for names possessing a fruit from the dropdown and specifiy a range of quantity (represented by the fruit1num amd fruit2num columns) of that fruit and it return a list of who has it.

For example in my sample table (if this worked) I would see the following list in the dropdown:
apple, pear, orange, plum, kiwi

I can then pick a fruit specify a spread of quantity 1-10 and it will return who has it and in what number.

for example if I selected: apples in a quantity of 1-5, It would return James 3.
but if I selected: apples in a quantity of 3-8, it would return James 3 and Jill 6.

maybe this explains better what I am trying to do. my script works but only for 1 column not multiple.
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.