Re: populating <select> and searching to criteria

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



First:

Use the SQL distinct command.. it's explained here:
http://www.mysql.com/doc/en/SELECT.html
That will get you unique values. 

Then just loop through the result:

while ($d = mysql_fetch_assoc($return)) {
?><option value="<?=$d['value']?>"><?=$d['name']?></option>
<?
}

Second: 
	get the value from the other table into a variable, during each iteration of 
the loop check to see if they match, if so add 'selected'.

while ($d = mysql_fetch_assoc($return)) {
?><option value="<?=$d['value']?>"<?
if ($other_table_value == $d['value'])
	echo " selected";
?>><?=$d['name']?></option>
<?
}

Third:
If you're using MySQL, check this out:
http://www.mysql.com/doc/en/Fulltext_Search.html

If you're using something else, it's more complex I think. Someone may have an 
elegant solution, but I would do something like:

// Get total number of keywords:
$numofkeywords = count(str_replace ( " ", " ", $keywords)); 

// Split up your search words:
$search = explode(" ", $keywords);
$total_matched = 0;
// loop through the array of search terms and get number of returns.
foreach ($search as $searchword) {
	$total_matched	 += count(str_replace($searchword, $searchword, $keywords));
}
// echo out the result in percent. (to one decimal place even!) 
echo "Percent Matched: ".round(($total_matched/$numofkeywords)*100), 1)." %";

I'm sure there's a better way to get a word count from a string, but it's 
late.. I didn't try the above code, so I'm just guessing it works.. I think 
it's the right idea though. 

-Micah



On Mon October 27 2003 4:54 pm, Shannon Doyle wrote:
> Hi People,
>
> I have a three part question here. Well actually its 3 separate
> questions.
>
> First.
>
> How would I populate a select menu from a mysql databse with the entries
> from a particular field, but only showing those that are unique, ie not
> showing more than 1 of the same value.
>
> Second.
>
> How would I then have one of those options selected depending on what it
> contained in another table?
>
>
>
> Third.
>
> I need to understand how the following would be achieved.
>
> In the database there is a text field that contains a number of
> 'keywords' I need to be able to search through those individual keywords
> and return results based on a % match from the original search. Ie if I
> search for 'secretary, typing, customer relations' I need to return all
> records from the database that has one or all of those words contained
> in the text field and display them as a % match of that search, ie if 1
> match then 33% 2, 66% and so on.
>
> Any help on the above three would be fantastic.
>
> Cheers,
>
> Shannon

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux