|
Posted by Rik on 08/20/07 21:40
On Mon, 20 Aug 2007 23:32:46 +0200, Vik Rubenfeld =
<vikr@mindspring.com.invalid> wrote:
> I have to search 2 mySQL tables, and show the user a single sorted lis=
t
> that contains all the results from both mySQL queries.
I'm curious about the data/tables, it might be possible to do all this o=
n =
a MySQL level instead of PHP, which would usually be better.
> My question is, how do you get all the resulting items from both
> databases into one single, sorted list, to present to the user?
$list =3D array();
$r1 =3D mysql_query("Enter query one here");
while($row =3D mysql_fetch_assoc($r1)) $list[] =3D $row;
$r2 =3D mysql_query("Enter query two here");
while($row =3D mysql_fetch_assoc($r2)) $list[] =3D $row;
//untested....:
$fieldname =3D 'foo'; //<- enter the fieldname to sort by
usort($list, create_function('$a,b',"if(\$a['{$fieldname}'] =3D=3D =
\$b['{$fieldname}']) return 0; return \$a['{$fieldname}'] > =
\$b['{$fieldname}'] ? 1 : -1;");
> I guess one way would be, to use PHP to copy all the results from each=
> database, into a PHP array, and then sort it. Is that how this kind of=
> thing is usually done?
The database software is optimised for this kind of stuff, so if it can =
be =
done there that would be highly preferable.
-- =
Rik Wasmus
[Back to original message]
|