Jump to content


php ORDER BY help

  • Please log in to reply
2 replies to this topic

#1 CanMan2004

  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 01 July 2006 - 07:47 PM

Hi all

I have a search form on my website which lets you search for a buddy, the search can be proformed as a name search using there first name, for example


The form then goes to the php page and does a query on what has been typed in, it then sorts the results by the persons surname, for example

David Adams
David Holmes
David Waferry

The query I currently perform is

$usql = "SELECT * FROM users WHERE `firstname` = '$fname' ORDER BY surname ASC";
$ushow = @mysql_query($usql,$connection) or die(mysql_error());

while ($urows = mysql_fetch_array($ushow)) {

print $urows['firstname'];
print " ";
print $urows['surname'];
print "<br><br>";


What I want to now do is to offer a surname search box on the same form, what should happen when someone sdoes a search under firstname and surname it would return back all results that match JUST the firstname (like it currently does). What I want to do though, it to show the people who matched the surname at the top of the results and then show just the people who matched the firstname below.

The firstname input box on the search form is called "firstname" and the surname one which I want to add will be called "surname".

Is this at all possible?

Loads of thanks in advance


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 July 2006 - 08:21 PM

SELECT firstname, lastname, IF(lastname='$lastname' , 0 ,1) as seq 
FROM users
WHERE firstname='$firstname' OR lastname='$lastname'
ORDER BY seq, lastname, firstname

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 CanMan2004

  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 01 July 2006 - 08:41 PM

thank you a million times over

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users