doddsey_65 Posted August 13, 2011 Share Posted August 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244672-query-results-based-on-array/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.