Jump to content

Query another table withing FOR loop of a table.


saeed_violinist

Recommended Posts

Dear Friends,

 

I have two tables. "news" and "categories"

 

I do a query to select 10 recent news, and one of its fields is "parent" which is ID of the category which the news belongs to it, and it is stored in table "categories"

 

I do like this but only the first category name will appear!

 

$query = $_DB->Query("SELECT news_id, parent, time, author, title, breif, image FROM news ORDER BY time ASC);  

 

and here is my loop :

 

        $count = $_DB->GetResultNumber($query); 

        for($i = 0; $i < $count; $i++) 
        {           
            $recentNews[$i]["news_id"] = $_DB->GetResultValue($query, $i, "news_id"); 
            $recentNews[$i]["parent_id"]  = $_DB->GetResultValue($query, $i, "parent");  

            // THIS ONLY RETURNS THE FIRST CATEGORY TITLE!!!!  second ans so on are empty  
            $tempQuery = $_DB->Query("SELECT name FROM categories WHERE category_id='".  $recentNews[$i]["parent_id"] ."'"); 
            $recentNews[$i]["category"] = $_DB->GetResultValue($tempQuery, $i, "name"); 
                                   
            $recentNews[$i]["time"]    = $_DB->GetResultValue($query, $i, "time");  
            $recentNews[$i]["author"]  = $_DB->GetResultValue($query, $i, "author");  
            $recentNews[$i]["title"]  = $_DB->GetResultValue($query, $i, "title");  
            $recentNews[$i]["breif"]   = $_DB->GetResultValue($query, $i, "breif");  
            $recentNews[$i]["image"]  = $_DB->GetResultValue($query, $i, "image");             
        } 

 

I guess I have to use JOIN but I dont know the right syntax for it :(

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.