class stucture help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

class stucture help

Post 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>'; 
    }
}
?>
 
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: class stucture help

Post 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.
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: class stucture help

Post 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 :)
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: class stucture help

Post by invisibled »

problem fixed. :D
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: class stucture help

Post by it2051229 »

great, now you post the source code and teach me how to use your generic class :mrgreen:
Post Reply