Ordering my search results

Hi,
At the moment my member search works, however when I search for a member called ‘Will’ it gives the result ‘Sam’ first because in his biography he has the work Will, and he is above Will in the MySQL table. Is there a way to give the results that usernames match first, then the results that the biographies match. Here is a link t my search bar: http://www.blu-byte.co.cc/members/search/quick.php/. Try searching for Will, you will see what I mean.

Here is my current search code:

[php]
$searchqu = mysql_query(“SELECT * FROM $tbl_name WHERE usr OR bio LIKE ‘%$search%’”);

//Draw table titles

echo “

”;
echo “”;

echo “

”;
echo “”;
echo “”;
echo “”;

while($rows = mysql_fetch_array($searchqu))
{

$usr = $rows[‘usr’];
$bio = $rows[‘bio’];

if ($usr)
{

echo “

”;
echo “”;
echo “”;
echo “”;

}
else
{

echo “

Sorry but we weren’t able to find any members for the search $search.

”;

}

}

echo “

”;
echo “
”;
echo "

Name

";
echo “
”;
echo "

Biography

";
echo “
”;
echo "

$usr

";
echo “
”;
echo "

$bio

";
echo “
”;

}
[/php]

Many thanks in advance.

Anyone?

can you do it by ORDER BY usr

I will give that a go!

Just tried it and there has been no affect, could you please show me where you would put the ORDER BY, I might have positioned it wrongly!

Thanks in advance! :slight_smile:

You would use in in the mysql query

$searchqu = mysql_query(“SELECT * FROM $tbl_name WHERE usr OR bio LIKE ‘%$search%’ ORDERBY usr”);

Or maybe you could use ksort() function in php

Are you sure? Wouldn’t there be a space?
$searchqu = mysql_query(“SELECT * FROM $tbl_name WHERE usr OR bio LIKE ‘%$search%’ ORDER BY usr”);
The above code has no affect.
Thanks.

Ah the sort function? Could you implement that in to my code? It would be a big help.
Thanks.

sorry ORDER BY does have a space in it maybe put ASC or DESC

ORDER BY usr ASC

Thinking about it I cannot think how you could sort
with php because how would it know the search word ?

You would need the searched word and then use that to sort by otherwise it would ASC, DESC, key

but you have to give me anything more than a result from your search.

But you could try

[php]
$usr = ksort($rows[‘usr’]);
$bio = ksort($rows[‘bio’]);[/php]

To get the searched word wouldn’t you just use the variable $search, $search is equal to $_POST[‘Seach’] from a form.
Is that any help?

Thanks for your time, it is a great help!

Try this query

[php]$searchqu = mysql_query(“SELECT * FROM $tbl_name WHERE usr OR bio LIKE ‘%$search%’ ORDER BY usr LIKE ‘%$search%’ DESC”);[/php]

Thanks, trying it now!

I have one other query. How would I duplicate a file named ‘profile.php’ to a specific directory, then renaming it.

Thanks for all your help, credit will be given.

You can also try there are so many different sort() functions

sort($searchqu);

The other one works! Well I am glad I have got that one cleared up! Could you help me with that other problem?

How would I duplicate a file named 'profile.php' to a specific directory, then renaming it.

Thanks so much!

Is there anything I can do for you?

There is only one problem. When I search for users that don’t have their names in their biography they don’t show up?

Thanks

bump

you can mofify your query to search for more fields and not just the name. if they dont have a name on there biosgraphy it will match others things

Okay could you implement that in my code to make ot make a little more sense?

Many thanks!

[php]
$searchqu = mysql_query(“SELECT * FROM $tbl_name WHERE (usr LIKE ‘%$search%’ OR bio LIKE ‘%$search%’ OR field_name LIKE ‘%$search%’) ORDER BY usr LIKE ‘%$search%’ DESC”);
[/php]

and just keep adding more stuff onto it until you get as many results as you want

Thanks so much!!

Sponsor our Newsletter | Privacy Policy | Terms of Service