Jump to content

search


Brendan

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"];
Link to comment
Share on other sites

  • 11 months later...
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.