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.

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 :)

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

 

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.