Page 1 of 1

class stucture help

Posted: Thu Jan 15, 2009 6:51 pm
by invisibled
Hey all,

I've been writing a pagination class for my projects and i'm trying to put this one variable inside the class so it doesn't have to be out in the main files... cause its not very neat.

to include the class in your project you do this

Code: Select all

 
//Include the files: 
require $root.'lib/inc/class.pagination.php';
$p = new pagination();
        
//Variables
$p->page_table  = database_table;
$p->per_page    = string;
        
if(isset($_GET['page'])):
    $p->res     = $p->per_page * $_GET['page'] - $p->per_page;
else:
    $p->res     = $p->per_page * 1 - $p->per_page;
endif;
        
//Initiate an instance:
    $p->output();
 
so that gross if statement i want to put inside the class, when i do it gives me errors like unexpected if statement and stuff. The $p->res variable is to be used in the mysql query to display the correct results depending on what page you are on:

Code: Select all

mysql_query("SELECT * FROM module_projects ORDER BY id DESC LIMIT $p->res, $p->per_page ");
So i want to define it inside of the class and call it from a page. ALl suggestions are welcomed, as i am fairly new to writing classes, here is my entire class:

Code: Select all

 
<?php
/*
 
Pagination
Written and Maintained by 
Shan Robertson (http://invisibled.com)
        
    Include the files: 
        require $root.'lib/inc/class.pagination.php';
        $p = new pagination();
        
    Variables
        $p->page_table  = database_table;
        $p->per_page    = string;
        
        if(isset($_GET['page'])):
            $p->res     = $p->per_page * $_GET['page'] - $p->per_page;
        else:
            $p->res     = $p->per_page * 1 - $p->per_page;
        endif;
        
    Initiate an instance:
        $p->output();
    
    
    TO DO
        -fix prev and next links when page var is not set
        -put the class init at the bottom of this file
        -put the res variable into the class
        
        
*///
class pagination{
    
    function output(){
        //Brings the vars into the function
        $page_table = $this->page_table;
        $per_page = $this->per_page;
        
        
        
        //The Query
        $page_query = mysql_query("SELECT id FROM $page_table");
 
        
 
        //Stores total number of results
        $num = mysql_num_rows($page_query);
 
 
 
        //Divides the number of results by the number per page
        $pages = ceil($num / $per_page);
 
        
 
        //Sets the increment value at 1
        $i = "1";
        
 
        
        //Next and Prev Links
        $next = $_GET['page']+1;
        $prev = $_GET['page']-1;
        
        
        //The Output
        print '<div class="pagination">';
            
            if($_GET['page'] != "1"):
                print '<a href="'.$php_self.'?page='.$prev.'"><</a>';
            endif;
            
            while($i <= $pages):
                print '<a href="'.$php_self.'?page='.$i.'" ';
                
                if($_GET['page'] == $i):
                    print 'class="current"';
                endif;
                
                print '>'.$i.'</a>';
                
                $i++;
            endwhile;
            
            if($_GET['page'] != $pages):
                print '<a href="'.$php_self.'?page='.$next.'">></a>';
            endif;
            
        print '</div>'; 
    }
}
?>
 

Re: class stucture help

Posted: Thu Jan 15, 2009 7:07 pm
by it2051229
So its your first time to create a class and your first class would be the pagination class. I wanted to create a generic class for pagination but at some point i failed because of some constraints(one of them is your problem) so i ended up that in every project that I made that includes pagination, it's not done with a class. But anyways I'm looking forward to have what you've done.

Re: class stucture help

Posted: Thu Jan 15, 2009 7:30 pm
by invisibled
well i'm building it for my projects, and i always build the same way, so yeah its restricted on some levels but for me its perfect. I do hope to be good enough to make it work in any situation, someday :)

Re: class stucture help

Posted: Fri Jan 16, 2009 1:24 am
by invisibled
problem fixed. :D

Re: class stucture help

Posted: Fri Jan 16, 2009 6:17 pm
by it2051229
great, now you post the source code and teach me how to use your generic class :mrgreen: