Page 1 of 1

PHP Listbox Search

Posted: Fri Oct 17, 2008 5:22 am
by Arkane
Hi guys, I have a question regarding the listbox in php. I've managed to load a single select listbox with names from a sql database. I've now added a standard text box and submit button that refreshes the page. If the user types John in the text box and clicks the button, I need to search through the listbox and if the name John is found, it needs to be selected. I'd appreciate any help you guys can offer. Thanks.

Re: PHP Listbox Search

Posted: Fri Oct 17, 2008 5:31 am
by aceconcepts
What you need to do is take the value entered by the user:

Code: Select all

 
if(isset($_POST['submit']))
{
$value=$_POST['value'];
}
 
With this value you need to query your database and get the matching ID for this value.

With this ID you would then loop through you listbox content and indicate the corresponding ID by setting "selected=selected":

Code: Select all

<option value="..." <? if($row['id']==$id_from_database){ echo 'selected="selected"'; } ?>>Something</option>
This should help.

Re: PHP Listbox Search

Posted: Fri Oct 17, 2008 6:03 am
by Arkane
Thanks man, appreciate it. So basically, what you're saying is that I can't cycle through the list box items like you would in Delphi like:

for c:= 1 to ItemCount do
begin
if ListBox[c] = SearchString then
.....
end;

Instead I need to cycle through the database and get the position of the matching Text, and then use that value to select the item in the listbox. Am I on the right track?

Also, is the $id_from_database an integer type?

Thanks again

Re: PHP Listbox Search

Posted: Fri Oct 17, 2008 6:11 am
by aceconcepts
Yeh, you could do it how you demonstrated. Your method would literally search for the value entered by the user - it would not search for an absolute match (i.e. by unique ID).

Re: PHP Listbox Search

Posted: Fri Oct 17, 2008 6:21 am
by Arkane
cool, because i dont really need an exact. What i've been trying to do, was to go through each item in the list box and then use the StrPos command to check if the Substring the user enters is found in one of the items in the list box. Eg "Jo" should match "John" in the listbox. But I havent been able to get it working, even been googling it for a while and still no luck.

Re: PHP Listbox Search

Posted: Fri Oct 17, 2008 6:45 am
by aceconcepts
But what if you have "john" and "joe" in you list when "jo" is entered as a search?

Re: PHP Listbox Search

Posted: Mon Oct 20, 2008 1:49 am
by Arkane
I see what you're saying, you have a good point there. Lets see... if there are multiple matches.

Eg. User searches for Choc, and there are the ffg matches:
Milk Chocolate, Dark Chocolate, Chocolate Cake, etc.

Then the first match would be used. The user would most probably search via ID, eg 1077, so as the ID is included in the listbox such as "1077 - Milk Chocolate", then there wouldnt be a problem. But if they use "Choc" as a search term, then its alright for the 1st "Choc" item to be highlighted.

Re: PHP Listbox Search

Posted: Mon Oct 20, 2008 3:31 am
by aceconcepts
Have you thought about using an expandable div with a loop inside?

Re: PHP Listbox Search

Posted: Mon Oct 20, 2008 3:51 am
by Arkane
sorry, i'm still new to php so i'm not sure what that is. heck even the post command is a little difficult :) is that a simpler way of doing the search. Heres a snippet of my code. basically in the html section below where the item is added to the listbox, if the search string is found in the $plu variable then the item need to be added and selected eg.

<option value="<?php echo $plu ?> " selected='selected' ><?php echo $plu ?></option>


//////GET MENU CODES
$getstores = "Select distinct menucode, description from company_menuitems where companyindex = $company_id order by description";
$gotstores = odbc_exec($connect, $getstores);

$mcode = '0';
$d = 0;

while(odbc_fetch_row($gotstores))
{
$d = $d + 1;
$mcode = odbc_result($gotstores, 1);
$mdesc = odbc_result($gotstores, 2);

$plu = $mcode . ' - ' . $mdesc;
$PLUArray[$d] = $plu; //Assign value to array and list box (code below)

?>
<option value="<?php echo $plu ?>"><?php echo $plu ?></option>

<?php

} //While fetch gotstores - plu item loop

?>

Re: PHP Listbox Search

Posted: Mon Oct 20, 2008 4:13 am
by aceconcepts
What you'll need to do is add a conditional statement inside your loop. This statement will check the value of each "looped" record with the $_POST value passed from the form:

Code: Select all

 
$d=0;
while(odbc_fetch_row($gotstores))
{
$selected="";
$d = $d + 1;
$mcode = odbc_result($gotstores, 1);
$mdesc = odbc_result($gotstores, 2);
 
$plu = $mcode . ' - ' . $mdesc;
$PLUArray[$d] = $plu; //Assign value to array and list box (code below)
 
if($_POST['var']==$mcode)
{
   $selected='selected="selected"';
}
 
?>
<option value="<?php echo $plu; ?>" <? echo $selected; ?>><?php echo $plu; ?></option>
 
<?php
 
} //While fetch gotstores - plu item loop
?>
 

Re: PHP Listbox Search

Posted: Mon Oct 20, 2008 4:22 am
by Arkane
Thanks, appreciate the help. :)