Return values based on results of another table.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

Return values based on results of another table.

Post by Vestax159 »

So I'm trying to return values from one table based on the results of another. The problem is that the way I have it now it won't allow me to use ORDER BY because it's working with one value at a time. Could anyone propose an alternate way to do this?

Code: Select all

 
$getcategories = tep_db_query("SELECT * FROM products_to_categories WHERE categories_id ='" . $category . "'")
or die ("Couldn’t execute query.");
    
 
while ($row = tep_db_fetch_array($getcategories))
{
    extract($row);
    
    $getproducts = tep_db_query("SELECT products_name products_price FROM products WHERE products_id ='" .  $products_id . "'");
 
    
    while ($rname = tep_db_fetch_array($getproducts))
        {
        extract ($rname);
        
            echo "print results";
        }
 
}
 
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Return values based on results of another table.

Post by N1gel »

You could do it all in one Query using a Join. Then add your order by clause

I presume you are using something like MySQL. Try having a look in the MySQL Manual

http://dev.mysql.com/doc/refman/5.1/en/left-join-optimization.html
Vestax159
Forum Newbie
Posts: 12
Joined: Thu Feb 18, 2010 3:24 pm

Re: Return values based on results of another table.

Post by Vestax159 »

Thanks! Worked wonderfully
Post Reply