Jump to content

Paginating multiple results in a single query


Pandemikk

Recommended Posts

Forgive me for the title, it's a bit lacking in descriptiveness. What I currently have:

 

$results = $this->registry->db->query_read("
SELECT title, threadid, forumid, dateline
FROM " . TABLE_PREFIX . "thread
WHERE postuserid = " . intval($this->profile->userinfo['userid']) . "
	AND sticky != 1
	AND forumid IN (13, 19, 39)
ORDER BY dateline DESC
LIMIT 0,50
");

 

This is fine for returning of the latest results to be readied for pagination by PHP. But I want to be able to have, say, 30 results for EACH forum ID (13, 19, 39, etc) without having to do 3 separate queries. Is this possible?

Link to comment
Share on other sites

If you have the id's in an array called $forumid[] , you can iterate through it and run the same query over and over

 

<?php
foreach($forumid as $value){
$results = $this->registry->db->query_read("
SELECT title, threadid, $value, dateline	
FROM " . TABLE_PREFIX . "thread
WHERE postuserid = " . intval($this->profile->userinfo['userid']) . "
AND sticky != 1
AND forumid IN (13, 19, 39)
ORDER BY dateline DESC
LIMIT 0,50
");

mysql($sql);



//Do what you want with the results of query



}

?>

Link to comment
Share on other sites

It could be done in a single query - in theory.  You would need to subquery each and every form table selecting the records and limiting to 30 resutls per query, then cross refference the results returned against the overall schema and produce your "master" result set, this would then need to be manipulated for pagination, and would only be relevent for a limited time.  It is, in my oppinion, over complicating a simple issue.

Link to comment
Share on other sites

I didn't mean to imply that it was too advanced for me to bother with. Simply stating that I would love to learn how to do it that way but I just can't understand it, so if you had any pointers or could lead me in the right direction that'd be much appreciated.

 

And the reason I don't want to run it x amount of times is for performance reasons. The page I'm running it on already has lots of queries and PHP processing so anything I can do to lessen the load is ideal. Plus it's good for me to start learning more advanced queries. So please don't take it as "I don't like it". It'd be much easier to do so doing it that way, but coding isn't always about doing what's easiest - though at times (perhaps this one included) that may be best.

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.