Jump to content

query results based on array


doddsey_65

Recommended Posts

Im trying to set up and retrieve info based on an array.

 

Basically a user chooses to show 1 topic from forum A and 4 topics from forum B

 

the array $forums is

 

$forums => Array (2)
(
    ['1'] = String(1) "2"
    ['4'] = String(1) "4"
)

 

the key is how many topics they wish to retrieve and the value is the id of the forum.

 

The results are ordered by newest(t.t_time_posted).

 

So it should show 1 topic from the forum with an id of 2(A) and 4 topics from the forum with id of 4(B)

 

It should also show the first post within the topics

 

So i have ran this query to get their results:

 

foreach($forums as $key => $val)
{
    $sql = "SELECT f.f_name, 
            t.t_name, t.t_time_posted,
            p.p_name, p.p_content
            FROM ".TBL_PREFIX."topics t
            LEFT JOIN ".TBL_PREFIX."forums f
            ON (t.t_fid = f.f_fid)
            LEFT JOIN ".TBL_PREFIX."posts p
            ON (p.p_tid = t.t_tid)
            WHERE t.t_fid = '$val'
            ORDER BY t.t_time_posted DESC
            LIMIT $key";
            
    $sth = $db->prepare($sql);
    $sth->execute() or die($db->sthError($sth, $sql));
    
    $r = $sth->fetchAll();
    dump($r);
    
    foreach($r as $k => $v)
    {
        echo $r[$k]['t_name'];
    }
}

 

However this doesnt work well. It shows 4 posts from forum B instead of 4 topics, and doesnt show anything from forum A.

 

Any ideas?

 

EDIT:

 

I added a GROUP BY t.t_name which shows the 4 topics from forum B but still nothing from forum A

 

Tiredness(and stupidity) got the better of me on this one. I had previously deleted all topics from forum A and forgot, thats why there were no results.

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.