Jump to content

function to convert sql query for pagination


johnsmith153

Recommended Posts

I want a simple function that converts a sql query into two queries for pagination purposes.

 

So if the sql query is "SELECT `Name` FROM table1"

 

I would like to perform:

 

"SELECT COUNT(*) FROM table 1"

 

and

 

"SELECT `Name` FROM table1 LIMIT 10, 20"

 

(example)

 

(1) Would this work for most basic queries? (obviously if LIMIT wasn't already used)?

 

(2) What command would I use to convert "SELECT `Name` FROM table1" to "SELECT COUNT(*) FROM table 1"

 

I am aware there is more involved than this, but am just interested in the query side of things.

Link to comment
Share on other sites

Use a variable:

 

$type = 'name';
"SELECT $type FROM table1"

Or

 

$type = 'COUNT(*)';
"SELECT $type FROM table1"

 

then yo

u can decide what with an if()

if(...)
{
        $type = 'name';
}
else
{
        $type = 'COUNT(*)';
}

 

Hope this help :)

Link to comment
Share on other sites

Although there are a few, very few pagination scripts out there, they are very limited in what can do because all databases and circumstances are different, so I really doubt would ever see a simple function for this.

 

It takes manual work for your specific needs to make it all work together.

 

What webbhelp said is correct.

 

You can do it by switch/case or if/else statements and use variables to reflect any superglobals like GET,POST,RESQUEST and so on.

 

I did a few examples on other posts can check out

 

Here's a post in the forum for a pagination that controls the count for start rows

http://www.phpfreaks.com/forums/php-coding-help/results-on-multiple-pages-help/msg1506457/#msg1506457

Live Demo for the above code

http://get.blogdns.com/dynaindex/paginate.php

 

Otherwise I do have an example of code involving multi-select queries based on search terms, it won't be a working example but can get an idea how to go about it.

http://www.phpfreaks.com/forums/php-coding-help/remove-commas-from-search-string-and-pass-results-to-a-query/msg1504627/#msg1504627

 

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.