Hi what's the best way to do a 'order by' from a query without executing a query for another time? Lets say I have a search query when I have to find names or surnames using a form ,an example :
//Array for storing WHERE part of query
$where=array();
if(!empty($name)) AND !empty($surname)){
$where[]="Name LIKE '%$name%'" .
"AND Surname LIKE '%$surname%'";
}
elseif(!empty($name)){
$where[]="Name LIKE '%$name%'";
}
elseif(!empty($surname)){
$where[]="Surname LIKE '%$surname%'";
}
$where = implode(' AND ', $where);
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name DESC";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
$name = $row[0];
$surname = $row[1];
echo"$name";
echo"$surname";
}
How can I display Name or Surname in ASC mode(ORDER BY Name ASC) after my search"Name LIKE %surname%",without executing again the SQL query? I want for example when I search for a name containing "ich"word to have a list in this way:
MICHAEL
PICHAEL
XICHAEL
=> the ASC mode
XICHAEL
PICHAEL
MICHAEL
=> the DESC mode
I really don't think I'm understanding your question correctly, but I'm going to go out on a limb here and guess you just want to order by two different columns?
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name DESC, Surname ASC";
by the way you had an extra "
in your query string
Also, you may want to look into using mysql_fetch_assoc
instead of mysql_fetch_array
This is just me nitpicking now ... but instead of using:
echo"$name";
echo"$surname";
You can just write:
echo $name;
echo $surname;
Arr you talking about reversing the order of an array like this?
<?php
//Array for storing WHERE part of query
$where=array();
if(!empty($name) && !empty($surname)){
$where[]="Name LIKE '%$name%'" .
"AND Surname LIKE '%$surname%'";
}
elseif(!empty($name)){
$where[]="Name LIKE '%$name%'";
}
elseif(!empty($surname)){
$where[]="Surname LIKE '%$surname%'";
}
$where = implode(' AND ', $where);
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name";
$res = mysql_query($sql);
$names = array();
$surnames = array();
$wholeNames = array();
$x='0';
while ($row = mysql_fetch_array($res)) {
if(!empty($row['Name'])){
$names[$x] = $row['Name'];
}
if(!empty($row['Surname'])){
$surnames[$x] = $row['Surname'];
}
if(!empty($row['Name'])&&!empty($row['Surname'])){
$wholeNames[$x] = $row['Name'].' '.$row['Surname'];
}
$x++;
}
print "<pre>\n";
print_r ($names);
print_r ($surnames);
print_r ($wholeNames);
print "</pre>\n";
$reversed_names=array_reverse($names);
$reversed_surnames=array_reverse($surnames);
$reversed_wholeNames=array_reverse($wholeNames);
print "<pre>\n";
print_r ($reversed_names);
print_r ($reversed_surnames);
print_r ($reversed_wholeNames);
print "</pre>\n";
?>
Something to watch out for, if you're using LIKE with the wildcards a search for "Art" would match Bart, Arthur, carter, and any other word with "Art" in it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.