NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page PHP Page Generator

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-07-2008, 04:07 AM THREAD STARTER               #1 (permalink)
i love automation
 
xrvel's Avatar
Join Date: Nov 2007
Location: xrvel.com
Posts: 1,620
xrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant futurexrvel has a brilliant future
 




Smile PHP Page Generator


Hi,

Here is a simple class to generate pages (for paging) for website.

Class source
PHP Code:
<?php
class page_generator {

    var 
$current_page_format;
    var 
$go_next_format;
    var 
$go_previous_format;
    var 
$hyperlink_format;
    var 
$page_url_format;

    var 
$all_records;
    var 
$current_page;
    var 
$flag;
    var 
$item_per_page;
    var 
$max_displayed_pages;
    var 
$page_parameter;
    var 
$page_string_next;
    var 
$page_string_previous;
    var 
$temp;

    function 
page_generator() {
        
$this->init();
    }

    function 
set_all_records($i) {
        
$this->all_records $i;
    }

    function 
get_limit_clause() {
        
$this->read_current_page();
        return array(
            
'from' => (($this->temp['current_page']-1)*$this->item_per_page),
            
'to' => $this->item_per_page
        
);
    }

    function 
get_page_string() {

        
$i 1;

        
$this->read_current_page();

        
$s '';

        
$pagenum ceil($this->all_records/$this->item_per_page);


        if (
$this->temp['current_page'] > 1) {
            
$s .= sprintf($this->go_previous_format,sprintf($this->page_url_format,$this->page_parameter.'='.($this->temp['current_page']-1)));
        }

        
$temp range(1$pagenum);

        
$temp array_chunk($temp$this->max_displayed_pages);
        
$temp_max count($temp);
        for (
$i 0;$i<$temp_max;$i++) {
            if (
in_array($this->temp['current_page'], $temp[$i])) {
                
$min_draw $temp[$i][0];
                
$max_draw $temp[$i][count($temp[$i])-1];
                break;
            }
        }

        for (
$i=1;$i<=$pagenum;$i++) {
            if (
$min_draw <= $i && $i <= $max_draw) {
                if (
$i != $this->temp['current_page']) {
                    
$s .= sprintf($this->hyperlink_format,sprintf($this->page_url_format,$this->page_parameter.'='.$i),$i);
                } else {
                    
$s .= sprintf($this->current_page_format,$i);
                }
            }
            if (
$i $max_draw) {
????: NamePros.com http://www.namepros.com/code/521588-php-page-generator.html
                break;
            }
        }

        if (
$this->temp['current_page']<$pagenum && $pagenum!=0) {
            
$s .= sprintf($this->go_next_format,sprintf($this->page_url_format,$this->page_parameter.'='.($this->temp['current_page']+1)));
        }
        return(
trim($s));
    }

    function 
init() {
        
$this->all_records 0;
        
$this->current_page 0;
        
$this->current_page_format '&nbsp;&nbsp;<b>%s</b>&nbsp;&nbsp;';
        
$this->go_next_format ' <a href="%s">&raquo;</a> ';
        
$this->go_previous_format ' <a href="%s">&laquo;</a> ';
        
$this->flag = array();
        
$this->flag['ALL_RECORDS'] = false;
        
$this->hyperlink_format '&nbsp;&nbsp;<a href="%s">%s</a>&nbsp;&nbsp;';
        
$this->item_per_page 5;
        
$this->max_displayed_pages 5;
        
$this->page_parameter='page';
        
$this->page_string_next='Next';
        
$this->page_string_previous='Previous';
        
$this->page_url_format='';
        
$this->temp = array();
        
$this->temp['current_page'] = 1;
    }

    function 
my_intval(&$s) {
        
$s intval($s);
    }

    function 
my_trim(&$s) {
        
$s trim($s);
    }

    function 
read_current_page() {
        if (isset(
$_GET[$this->page_parameter])) {
            
$page=intval($_GET[$this->page_parameter]);
        } else {
            
$page=1;
        }
        if (
$page 1) {
            
$page 1;
        }
        
$this->temp['current_page'] = $page;
    }

    
/*
     * Get & set
     */

    
function current_page_format($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->current_page_format));
        } else {
            
$this->current_page_format=$s;
        }
    }

    function 
go_next_format($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->go_next_format));
        } else {
            
$this->go_next_format=$s;
        }
    }

    function 
go_previous_format($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->go_previous_format));
        } else {
            
$this->go_previous_format=$s;
        }
    }

    function 
hyperlink_format($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->hyperlink_format));
        } else {
            
$this->hyperlink_format=$s;
        }
    }

    function 
item_per_page($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
intval($this->item_per_page));
        } else {
            
$this->item_per_page=intval($s);
        }
    }

    function 
max_displayed_pages($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
intval($this->max_displayed_pages));
        } else {
            
$this->max_displayed_pages=intval($s);
        }
    }

    function 
page_parameter($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->page_parameter));
        } else {





            
$this->page_parameter=$s;
        }
    }

    function 
page_url_format($s='') {
        
$this->my_trim($s);
        if (
$s=='') {
            return(
trim($this->page_url_format));
        } else {
            
$this->page_url_format=$s;
        }
    }


}
?>
Usage :
For example :
  1. Normal SQL query is
    Code:
    SELECT * FROM example
  2. You want each page to display 8 data.
  3. You want to display max 10 page links (to avoid too long page link)
  4. The name of your current file is "display_data.php"

You can use this :
PHP Code:
<?php
// Let's get the total number of data
$q "SELECT COUNT(*) FROM example";
$q mysql_query($q);
$r mysql_fetch_row($q);
$total_data $r[0];

// Now here is the page generator
$pg = new page_generator();
$pg->set_all_records($total_data);
$pg->item_per_page(8);
$pg->max_displayed_pages(10);
$pg->page_url_format("display_data.php?%s");//page location
$page_navigation $pg->get_page_string();// here is the page link
$limit_clause $pg->get_limit_clause();// limit clause
????: NamePros.com http://www.namepros.com/showthread.php?t=521588
unset($pg);
?>
Next, just before your code to read data from database
PHP Code:
<?php
$q 
"SELECT * FROM example LIMIT ".$limit_clause['from']." , ".$limit_clause['to'];
$q mysql_query($q);
?>
And you can display the page link anytime with this code
PHP Code:
<?php echo $page_navigation?>
I hope this code can help.
__________________
xrvel is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 11:50 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger