Friday 16 September 2011

Php pagination, how do chnage my pagination page to produce static pages so there is less load on mysql table?

Hi



I have managed to rewrite my .php pages using mod rewrite.



My question:



How can i change my pagination main index.php page, that produces pagaination by querying my mysql database to produce static pages so that it doesnt have to hit the dbms all the time.



My main index.php page looks like this:



http://www.articlesbase.com/automotive-a鈥?/a>



My index.php page has code:



/create some variables

$pagenumber = (isset($_GET[%26quot;page%26quot;]) %26amp;%26amp; is_numeric($_GET[%26quot;page%26quot;])) ? $_GET[%26quot;page%26quot;] : 1;

$perpage = 10;

$padding = 7;

$startindex = ($pagenumber * $perpage) - $perpage;



include(%26quot;drift.php%26quot;);

include(%26quot;db/db_config.php%26quot;);

$totalcount = %26quot;SELECT COUNT(*) as 'Total' FROM test.articles%26quot;;

$rscount = mysql_query($totalcount) or die(mysql_error());

$rowcount = mysql_fetch_object($rscount);

$sql = %26quot;SELECT id,title

etc..And also has code for printing first,1,2,3,last, links at the bottom of the page.



Can mail the whole code if needed



Thanks in advance

Tovia SingerPhp pagination, how do chnage my pagination page to produce static pages so there is less load on mysql table?I find mod_rewrite clunky. There was a helpful comment in the http://uk3.php.net/manual/en/function.he鈥?/a> comments page, credit to Dylan at WeDefy dot com on

13-Oct-2007 03:17. This shows how to set the redirct type from within php.



%26lt;?php

// 301 Moved Permanently

header(%26quot;Location: /foo.php%26quot;,TRUE,301);

// 302 Found

header(%26quot;Location: /foo.php%26quot;,TRUE,302);

header(%26quot;Location: /foo.php%26quot;);

// 303 See Other

header(%26quot;Location: /foo.php%26quot;,TRUE,303);

// 307 Temporary Redirect

header(%26quot;Location: /foo.php%26quot;,TRUE,307);

?%26gt;



I'm not sure entirely what you're trying to achieve, but if you're trying to do something repeatedly, and you have to do something complicated each time, you should be caching the results (and if needs be tossing the cache when things change).



Now you can cache things a number of ways:-

The easiest is just to use a standard global variable. That won't work across sessions unless you store it in some kind of cache. memcache works, but you will need to install it as a separate app, and as a pecl extension.

Alternatively you can use the database to cache (just keep the SQL to access the cache simple). As an example, wikipedia caches all pages in a database, once it has generated the content. My application (the Loyaltynet Engine) caches all its group by and count calculations in a separate table, to speed up displaying summary data.

If you really don't want to use a database, you can use a file and parse it, but it probably won't be parsed quicker than a database (if the database is any good).

Finally you can do any of the above (or none of them) then put the website behind a squid proxy server and configure that to cache certain pages.



My preference would be to use memcache, a database cache and a squid proxy cache, but prioritise which you cache in which bit. I'm not entirely clear why you don't want to hit your dbms all the time. If in doubt give the DB more memory. Wikipedia uses multiple DBs, some read only, some master. It's quite straightforward to setup if there is only one master.



I've enabled my email so that you can contact me if you want to give me more details.
Php pagination, how do chnage my pagination page to produce static pages so there is less load on mysql table?
It's not entirely clear what you're asking.

You want to avoid querying database to produce page ? Why ?



One way to achieve that is to cache the page - i.e. once particular page (#1,#2,#3, ..) has been produced for the first time, save the resulting HTML (use ob_start, on_get_contents to do that) into a server file from your script - something like pageNN+CC.html where NN is starting index and CC is number of items per page.

So the next time you get request, check if that file exists - if it does, serve it up instead of querying the database.



Tere is nothing bad about querying database on every request - the overhead of caching can be more expensive than DBMS access.



Client browsers usually have no idea whether your pages are static or dynamic or - the thing you did to rewrite URLs (aricle/48, article/33) should be enough for most search engines to consider the pages %26quot;static%26quot; if that's your concern.