Jump to content

Archived

This topic is now archived and is closed to further replies.

Brendan

search

Recommended Posts

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"];

Share this post


Link to post
Share on other sites
Why not combine the query's and then you can sort it anyway you like

[code]<?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";
?>[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.