Paginated MySQL query result to static HTML

Discussion in 'PHP' started by rps111, Nov 22, 2007.

  1. #1
    I am using a PHP form to query a MySQL DB. The query result is displayed in pagination (100 results/page). I want that the pagination be converted to static html pages automatically.

    That is, instead of showing the result as www.abc.com/query.php?page=1

    it should produce results as 1.html, 2.html such that simply copying them on my site will produce pages with URL like:
    www.abc.com/files/1.html
    www.abc.com/files/2.html
    ..........and so on.
     
    rps111, Nov 22, 2007 IP
  2. chandubhai

    chandubhai Banned

    Messages:
    556
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using mod_rewrite.
     
    chandubhai, Nov 22, 2007 IP
    uglyboy likes this.
  3. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    chandubhai is right

    best way to do this what you want is use mod_rewrite to rewrite your url's to seo friendly urls
     
    kreoton, Nov 22, 2007 IP
  4. SEOWOES

    SEOWOES Peon

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Create a .htaccess file, and put in the root of the directory you want to redirect:

    RewriteEngine on
    RewriteRule ^files/(.*)/$ /query.php?page=$1 [R,L]

    Then have your links written in the format

    abc.com/files/1/
    abc.com/files/2/
    abc.com/files/3/

    etc.

    the rewrite rule then converts the /files/1/ to query.php?page=1 etc

    No need to convert anything to a static html page (seems a little pointless anyway).
     
    SEOWOES, Nov 22, 2007 IP
  5. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use mod_rewrite but he may want to write the pages to static html to avoid hitting the database for every page generation.

    If that is the case youll need to cache the pages to disk/memory using php on first display or write them to disk as theyre processing.
     
    tonybogs, Nov 23, 2007 IP
  6. rps111

    rps111 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Tonybogs you are write. I want static pages so that the load on database is reduced. Could you please help me with the code?

    Thanks for help.
     
    rps111, Nov 23, 2007 IP
  7. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #7
    u will need to use mod rewrite
     
    PowerExtreme, Nov 23, 2007 IP
  8. Kadence

    Kadence Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #8
    IMO the best thing to do is place in .htaccess is what popular systems like Wordpress, mambo/joomla, etc. use.
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    Code (markup):
    This will redirect all requests that aren't actually a physical file to index.php. Then index.php can handle the request, with something like
    $var_array = explode("/", $_SERVER['REQUEST_URI']);
    //Ignore the first element which I think is always blank, so start with $i=1
    for($i=1; $i<count($var_array); $i++)
    {
    	//process each variable if necessary
    }
    PHP:
    The actual code you should use might be a bit more complicated, and I think if your server is IIS you might need to use different server variables. Also you might want to check for magic quotes with get_magic_quotes_gpc() and strip them if true (before doing the code above), but if you don't plan to move to another server ever this is optional.

    In your case I believe $var_array[1] will be 'files' and $var_array[2] will be '1', '2', '3', etc. Then your index.php can display content based on $var_array[2].
     
    Kadence, Nov 23, 2007 IP
  9. Kadence

    Kadence Well-Known Member

    Messages:
    139
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Hmmm...I read this before posting but for some reason it didn't sink in.

    If you want actual html pages automatically generated then you'd need some sort of caching solution, yes. You can probably do a search somewhere for 'caching class'; I don't have experience with one myself but you can probably find one on pear.php.net or phpclasses.org.

    But unless server load is a huge issue for you I'd recommend the dynamic route with PHP, because it's much more flexible. Tough to edit a 100 paginated HTML pages, and odds are pretty good that someday you'll want to.
     
    Kadence, Nov 23, 2007 IP