Jump to content


Photo

search


  • Please log in to reply
2 replies to this topic

#1 Brendan

Brendan
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 28 February 2006 - 05:03 AM

I have a search engine written in php and mysql. There are two tables, one with a username and many colums with various data, but not dates, and another table with the same usernames but this one has dates. Now when you search you can change it to search and sort with certain fields in the first table, but i dont know how to make it sort with the second field, because generally you sort with the first table you query not the second.

I have it set up generally like this right now:


$sql=mysql_query("select*from firsttable $where limit $start, $end");

if(!empty($sql)){
while($row=mysql_fetch_array($sql)){
$user=$row["username"];
$blah=$row["blah"];



$sqlq=mysql_query("select*from secondtable where username='$user' ORDER BY creationdate $order");

if(!empty($sqlq)){
while($row=mysql_fetch_array($sqlq)){
$creationdate=$row["creationdate"];


#2 camdagr81

camdagr81
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationColumbus, Oh - United States

Posted 15 February 2007 - 04:38 PM

Try:

ORDER BY ID


Powered by... Ramen. Fueled by Redbull
ITTAGAKIMASU!!!!!!

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 15 February 2007 - 05:00 PM

Why not combine the query's and then you can sort it anyway you like

<?php
$order = $_POST['orderby']; // get this value from the form submitted
$sql = "SELECT * FROM firsttable AS t1 LEFT JOIN secondtable AS t2 ON t1.username = t2.username ORDER BY $order ASC";
?>

Now it doesn't matter which table the order is in, as long as it is unique to the table. If you want to sort by username, you will get an error if both tables have a field called username. You will have to sort that out before the query

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users