# need php mysql query to populate a scroll list



## tmjhayward (Jul 17, 2005)

Hello everyone. I'm trying to create a page that will allow the user to enter a partial/full search term, which will be used to query the database. The results of the query will then be used to populate a scroll or drop down list. The user should then be able to select an item from the final list for ordering purposes. I'd like to do this with just html, php and mysql, and preferable no javascript. I get the search box and button, but when I click the button, I get the error that it can't find the URL. I've searched tons of sites, but maybe I'm just trying to combine too many different ways of doing this. 

My 2 scripts are attached (there may be some funky stuff in there from things I was trying at 1:30 this morning, that I didn't remove!). If anyone can tell me what I'm missing or doing wrong, or if there's a much cleaner/simpler way of doing this, I'd appreciate it. 
Thanks!
MJ


----------



## tmjhayward (Jul 17, 2005)

Okay, I figured out a way to get my initial search query to work (I called product_search.php as the POST action and took the function out of it) among a few other corrections. I can now get the output from the query, but am not sure how to put it in a form for the user to select from. Do I have to create a separate page for that since it needs to have its own POST or can I somehow continue it in the original script (ss_prodsearch_test.php)?
Thanks.
MJ


----------



## TheRobatron (Oct 25, 2007)

Can't you take the output of the search query and populate a select list with it?


```
echo "";
foreach ($results as $r) {
echo "$r";
}
echo "";
```
Or have I misunderstood what you want to do?


----------



## tmjhayward (Jul 17, 2005)

Hi Robatron. Thanks for your response. You did understand me correctly, but I ended up just using a while loop with mysql_fetch_array. instead. I tend to decide what I want to do and then go looking for how to do it - guess I'm a bit impatient and just want to get to the final product. Frequently I'll find what I need but don't necessarily have all of the appropriate building block basics necessary to understand all of it, so it gets confusing.

But I do have another question if you don't mind. The search results are being populated on a new page. However, if a search returns no results, I want to put out a message stating so, but return the user to the initial search page, WITH the message, so they can search again. I've tried using header('Location: xx') but I get the 'Cannot modify header information' error. If you're trying to go back to a page that has code on it already, I don't see how you CAN'T get this error, but again, it's probably just something I don't understand yet. 

How else can I get back to the original search page without requiring the user to click to get there?
Thanks!
MJ


----------



## TheRobatron (Oct 25, 2007)

You may be getting the error because you have already put output on the page. You can't print anything to the page before you use the header command. If that's not the reason then I'm not sure what is.

Another solution would be to make the results page on the same page as the search page:

```
$searchstring = $_REQUEST['search']
if (isset($searchstring)) {
// Display search results or if there are none, drop through to print an error message
}
if (!isset($searchstring) || $searchresults = 0) {
// Search box
```
Alternatively, you could just put a search box on your results page that pointed to itself (emulating the original search page).


----------



## tmjhayward (Jul 17, 2005)

Thanks again, Robatron. I was able to use the header() command by rearranging the code on my results page so that all of the php was at the top and the html was after the header() command. I have it redirected back to my search page if there are no results. But in this case I want to write additional text on the search page like 'Your search returned no results.' I would think the only way to do this is to pass a global of some sort from the results page back to the search page, and add the additional text based on the value of that global. And the only way I've seen that you can do that, is to use sessions. Can sessions be used without login logic? (I'll be adding that soon, but don't have it set up yet.) This site has a couple of links to tutorials for sessions, but it's a bit confusing now. Do you know of a 'quick and dirty' way to set them up for this purpose?
Thanks!
MJ


----------



## TheRobatron (Oct 25, 2007)

Thankfully there is an easy way to do this.  In the header command you can add on to the end a GET variable:

```
header: "Location: www.yoursite.com/search?noresults=true";
```
And then on you search page:

```
$noresults = $_REQUEST['noresults'];
// Place the if statement anywhere you want it to display the no results found message
if ($noresults) { // if there are no results
echo "There were no results found. Please search again.";
}
```
It's probably not the best way to do it, but it works.


----------



## tmjhayward (Jul 17, 2005)

Thanks - that worked great! I'll be using a variation of that on a few of my pages. 
Regarding my first post, I populated a form (form 1) with an array from the query based on a search string entered in a previous form. Code from form 1 page looks like this:

echo "";
echo " "; 
while ($list = mysql_fetch_array($query_result)) 
{
echo "" . $list['1']. " " . $list['2']. " " . $list['3']." ";
}
echo "";
echo "";
echo "";

But when I select one or more values from form 1 and hit submit, I want it to populate the selected values in form 2. I'm not getting any values at all returned from the $_POST.

$product_list = ($_POST['results_list'][0]);

I have tried every suggestion I could find to get it to return something - with and without [] in the 'name=' phrase, the same in the $POST command in the page for form. I've seen some things about explode/implode - is this the only way to do this? Everything says just put [] after the name="xxx" and it'll work. But it's driving me crazy!
Can you help???


----------



## TheRobatron (Oct 25, 2007)

It should work if you put [] after the name in the HTML - it's the same as submitting multiple checkboxes. You don't need to put it in the PHP; just the name will do.

```

```


```
$product_list = $_POST['results_list'];
```
I've done this before with checkboxes, so it should work fine. There shouldn't be a need to use explode() or implode() as the values aren't returned as a single string.


----------



## tmjhayward (Jul 17, 2005)

But then how to I populate the new form with this? I've tried using 'while' but I can't seem to get the syntax right. If I put in an echo after the $_POST it's only returning one value. If my $_POST command is this: $product_list = ($_POST['results_list']); (which may be incorrect) and I have the following, what do I put where the xxxxxxxxxxxs are?

echo "";
echo " ";
xxxxxxxxxxxxxxxxx
echo "";
echo "";
echo "";

Thanks again!


----------



## TheRobatron (Oct 25, 2007)

Put where the Xs are:

```
foreach ($product_list as $p) {
echo "$p";
}
```
This will print each entry in the array. If you just want to see everything that's in the array (for debugging purposes) us print_r($product_list).


----------



## tmjhayward (Jul 17, 2005)

Hi again. Sorry but that's not working. Here is the code from the first page:

echo "";
echo " "; 
while ($list = mysql_fetch_array($query_result)) // $list = name of array indexed by col name
{
echo "" . $list['1']. " " . $list['2']. " " . $list['3']." ";
}
echo "";
echo "";
echo "";

And here is my $_POST and the additional code from the 2nd page. The print_r is returning the ID from the array on the first page. Am I missing something?

$product_list = ($_POST['results_list']);

echo "";
echo " ";
foreach ($product_list as $p) 
{
echo "$p";
} 
echo "";
echo "";
echo "";
print_r($product_list);

Thanks!
MJ


----------



## TheRobatron (Oct 25, 2007)

Try it without the brackets round the $_POST (you don't need them ).


----------

