Author Topic: Ordering my search results  (Read 427 times)

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Ordering my search results
« on: August 06, 2012, 04:46:31 AM »
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 Code: [Select]

$searchqu 
mysql_query("SELECT * FROM $tbl_name WHERE usr OR bio LIKE '%$search%'");

//Draw table titles

echo "<table border='1' cellpadding='5' cellspacing='1' style='width: 75%' >";
echo 
"<tbody>";

echo 
"<tr>";
echo 
"<td>";
echo 
"<p><span style='font-family:tahoma,geneva,sans-serif;'><span style='font-size:20px'> Name </span></span></p>";
echo 
"</td>";
echo 
"<td>";
echo 
"<p><span style='font-family:tahoma,geneva,sans-serif;'><span style='font-size:20px'> Biography </span></span></p>";
echo 
"</td>";
echo 
"</tr>";

while(
$rows mysql_fetch_array($searchqu))
{

$usr $rows['usr'];
$bio $rows['bio'];

if (
$usr)
{

echo 
"<tr>";
echo 
"<td>";
echo 
"<p><span style='font-family:tahoma,geneva,sans-serif;'><strong> $usr </strong></span></p>";
echo 
"</td>";
echo 
"<td>";
echo 
"<p><span style='font-family:tahoma,geneva,sans-serif;'> $bio </span></p>";
echo 
"</td>";
echo 
"</tr>";

}
else
{

echo 
"<p><span style='font-family:tahoma,geneva,sans-serif;'>Sorry but we weren't able to find any members for the search $search.</span></p>";

}

}

echo 
"</tbody>";
echo 
"</table>";

}


Many thanks in advance.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #1 on: August 06, 2012, 05:37:57 AM »
Anyone?

Noodles

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: 12
    • View Profile
Re: Ordering my search results
« Reply #2 on: August 06, 2012, 06:25:51 AM »
can you do it by ORDER BY usr
{I code in my sleep so when I wake up the problem is solved}
Remember no one is perfect.
Do NOT pm me unless I ask you to.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #3 on: August 06, 2012, 06:45:22 AM »
I will give that a go!

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #4 on: August 06, 2012, 06:49:06 AM »
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!  :)

Noodles

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: 12
    • View Profile
Re: Ordering my search results
« Reply #5 on: August 06, 2012, 06:52:04 AM »
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
« Last Edit: August 06, 2012, 07:00:22 AM by Noodles »
{I code in my sleep so when I wake up the problem is solved}
Remember no one is perfect.
Do NOT pm me unless I ask you to.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #6 on: August 06, 2012, 06:54:50 AM »
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.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #7 on: August 06, 2012, 06:55:35 AM »
Ah the sort function? Could you implement that in to my code? It would be a big help.
Thanks.

Noodles

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: 12
    • View Profile
Re: Ordering my search results
« Reply #8 on: August 06, 2012, 07:04:51 AM »
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 Code: [Select]
 
$usr 
ksort($rows['usr']);
$bio ksort($rows['bio']);

« Last Edit: August 06, 2012, 07:22:21 AM by Noodles »
{I code in my sleep so when I wake up the problem is solved}
Remember no one is perfect.
Do NOT pm me unless I ask you to.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #9 on: August 06, 2012, 07:38:32 AM »
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!

Noodles

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: 12
    • View Profile
Re: Ordering my search results
« Reply #10 on: August 06, 2012, 08:07:50 AM »
Try this query

PHP Code: [Select]
$searchqu mysql_query("SELECT * FROM $tbl_name WHERE usr OR bio LIKE '%$search%' ORDER BY usr LIKE '%$search%' DESC");
{I code in my sleep so when I wake up the problem is solved}
Remember no one is perfect.
Do NOT pm me unless I ask you to.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #11 on: August 06, 2012, 08:12:58 AM »
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.

Noodles

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: 12
    • View Profile
Re: Ordering my search results
« Reply #12 on: August 06, 2012, 08:18:02 AM »
You can also try there are so many different sort() functions

sort($searchqu);
{I code in my sleep so when I wake up the problem is solved}
Remember no one is perfect.
Do NOT pm me unless I ask you to.

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #13 on: August 06, 2012, 08:21:00 AM »
The other one works! Well I am glad I have got that one cleared up! Could you help me with that other problem?

Quote
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?

Ri1es

  • Regular Member
  • **
  • Posts: 35
  • Karma: 0
    • View Profile
Re: Ordering my search results
« Reply #14 on: August 06, 2012, 09:22:33 AM »
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