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
https://forums.phpfreaks.com/topic/244672-query-results-based-on-array/
Share on other sites

Archived

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

×
×
  • 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.