Edit: okay question 1 is dumb i just saw it asked earlier... sorry
2) Is this the correct place for a Class for Pagination and need help with, or does it have to be on another forum?
Thx in advance.
Moderator: General Moderators
Code: Select all
Class Pagination
{
var $result; //The result of mysql_query("select * from your_table").
var $perpage; //The number of results to display on page.
var $start; //The result at which to start.
var $row_num; //The total number of rows.
var $page_num; //The total number of pages, based on $perpage and $row_num
var $divider; //The symbol used to divide the page numbers
Function Paginate($result, $perpage, $start, $divider)
{
$this->query = $result;
$this->perpage = ($perpage < 1) ? $perpage : 1;
$this->start = isset($_GET['start']) ? $_GET['start'] : 0;
$this->page = $this->current_page();
$this->divider = isset($divider) ? $divider : " |.| ";
$this->row_num = Get_Row_Num();
$this->page_num = Calculate_Page_Num();
}
Function Get_Row_Num()
{
return mysql_num_rows($this->result);
}
Function Calculate_Page_Num()
{
$this->fixed = (intval($this->row_num) % intval($this->perpage)) <> 0 ? 1 : 0;
$this->calc_var = intval($this->row_num) / intval($this->perpage) + intval($this->fixed);
return $this->calc_var;
}
Function Current_Page()
{
$this->page = ($this->start = 0) ? 1 : $this->start / $this->perpage;
return $this->page;
}
Function Placeholder_First()
{
If($this->start <> 0){
$this->first = "<a href='?start=0'>First</a>";
}
Else
{
$this->first = "<u>First</u>";
}
return $this->first;
}
Function Placeholder_Previous()
{
If($this->start = 0){
$this->previous = "<u>Previous</u>";
}
Else
{
$this->prev_calc = (intval($this->page) - 1) * intval($this->perpage);
$this->previous = "<a href='?start=" . $this->prev_calc . "'>Previous</a>";
}
return $this->previous;
}
Function Placeholder_1()
{
If($this->page <= 3){
If($this->page = 1){
$this->p1 = "<u>1</u>";
}
Else
{
$this->p1 = "<a href='?start=" . $this->perpage . "'>1</a>";
}
}
Else
{
$this->p1_1 = intval($this->start) / intval($this->perpage) - 3;
$this->p1_2 = intval($this->p1_2) * intval($this->perpage);
$this->p1 = "<a href='?start=" . $this->p1_2 . "'>" . $this->p1_1 . "</a>";
}
return $this->p1;
}
Function Placeholder_2()
{
If($this->page <= 3){
If($this->page = 2){
$this->p2 = "<u>2</u>";
}
Else
{
$this->p2_1 = intval($this->perpage) * 2;
$this->p2 = "<a href='?start=" . $this->p2_1 . "'>2</a>";
}
}
Else
{
$this->p2_1 = intval($this->start) / intval($this->perpage) - 2;
$this->p2_2 = intval($this->p2_1) * intval($this->perpage);
$this->p2 = "<a href='?start=" . $this->p2_2 . "'>" . $this->p2_1 . "</a>";
}
return $this->p2;
}
Function Placeholder_3()
{
If($this->page <= 3){
If($this->page = 3){
$this->p3 = "<u>3</u>";
}
Else
{
$this->p3_1 = intval($this->perpage) * 3;
$this->p3 = "<a href='?start=" . $this->p3_1 . "'>3</a>";
}
}
Else
{
$this->p3_1 = intval($this->start) / intval($this->perpage) - 1;
$this->p3_2 = intval($this->p3_1) * intval($this->perpage);
$this->p3 = "<a href='?start=" . $this->p3_2 . "'>" . $this->p3_1 . "</a>";
}
return $this->p3;
}
Function Placeholder_4()
{
$this->p4_eval = $this->page_num - 3;
If($this->page <= 3 OR $this->page >= $this->p4_eval){
If($this->page <= 3){
$this->p4_1 = intval($this->perpage) * 4;
$this->p4 = "<a href='?start=" . $this->p4_1 . "'>4</a>";
}
If($this->page >= $this->p4_eval){
$this->p4_1 = (intval($this->page_num) - 4) * intval($this->perpage);
$this->p4_2 = intval($this->page_num) - 4;
$this->p4 = "<a href='?start=" . $this->p4_1 . "'>" . $this->p4_2 . "</a>";
}
}
Else
{
$this->p4 = "<u>" . $this->current_page() . "</u>";
}
return $this->p4;
}
Function Placeholder_5()
{
$this->p5_eval = $this->page_num - 2;
If($this->page >= $this->p5_eval){
If($this->page = $this->p5_eval){
$this->p5 = "<u>" . $this->p5_eval . "</u>";
}
Else
{
$this->p5_1 = intval($this->page_num) - 3;
$this->p5_2 = intval($this->p5_1) * intval($this->perpage);
$this->p5 = "<a href='?start=" . $this->p5_2 . "'>" . $this->p5_1 . "</a>";
}
}
Else
{
$this->p5_1 = (intval($this->page_num) - intval($this->page) + 1) * intval($this->perpage);
$this->p5_2 = intval($this->page_num) - intval($this->page) + 1;
$this->p5 = "<a href='?start=" . $this->p5_1 . "'>" . $this->p5_2 . "</a>";
}
return $this->p5;
}
Function Placeholder_6()
{
$this->p6_eval = $this->page_num - 2;
If($this->page >= $this->p6_eval){
$this->p6_eval2 = $this->page_num - 1;
If($this->page = $this->p6_eval2){
$this->p6 = "<u>" . $this->p6_eval2 . "</u>";
}
Else
{
$this->p6_1 = intval($this->page_num) - 2;
$this->p6_2 = intval($this->p6_1) * intval($this->perpage);
$this->p6 = "<a href='?start=" . $this->p6_2 . "'>" . $this->p6_1 . "</a>";
}
}
Else
{
$this->p6_1 = (intval($this->page_num) - intval($this->page) + 2) * intval($this->perpage);
$this->p6_2 = intval($this->page_num) - intval($this->page) + 2;
$this->p6 = "<a href='?start=" . $this->p6_1 . "'>" . $this->p6_2 . "</a>";
}
return $this->p6;
}
Function Placeholder_7()
{
$this->p7_eval = $this->page_num - 2;
If($this->page >= $this->p7_eval){
If($this->page = $this->page_num){
$this->p7 = "<u>" . $this->page_num . "</u>";
}
Else
{
$this->p7_1 = (intval($this->pagenum) - 1) * intval($this->perpage);
$this->p7 = "<a href='?start=" . $this->p7_1 . "'>" . $this->page_num . "</a>";
}
}
Else
{
$this->p7_1 = (intval($this->page_num) - intval($this->page) + 3) * intval($this->perpage);
$this->p7_2 = intval($this->page_num) - intval($this->page) + 3;
$this->p7 = "<a href='?start=" . $this->p7_1 . "'>" . $this->p7_2 . "</a>";
}
return $this->p7;
}
Function Placeholder_Next()
{
$this->start_1 = intval($this->page_num) * intval($this->perpage);
If($this->start = $this->start_1){
$this->next1 = "<u>Next</u>";
}
Else
{
$this->next_1 = intval($this->start) + intval($this->perpage);
$this->next1 = "<a href='?start=" . $this->next_1 . "'>Next</a>";
}
return $this->next1;
}
Function Placeholder_Last()
{
$this->last_1 = (intval($this->page_num) - 1) * intval($this->perpage);
If($this->last_1 = $this->start){
$this->last = "<u>Last</u>";
}
Else
{
$this->last = "<a href='?start=" . $this->last_1 . "'>Last</a>";
}
return $this->last;
}
Function Compile()
{
$this->output = $this->placeholder_first() . $this->divider;
$this->output .= $this->placeholder_previous() . $this->divider;
$this->output .= $this->placeholder_1() . $this->divider;
$this->output .= $this->placeholder_2() . $this->divider;
$this->output .= $this->placeholder_3() . $this->divider;
$this->output .= $this->placeholder_4() . $this->divider;
$this->output .= $this->placeholder_5() . $this->divider;
$this->output .= $this->placeholder_6() . $this->divider;
$this->output .= $this->placeholder_7() . $this->divider;
$this->output .= $this->placeholder_next() . $this->divider;
$this->output .= $this->placeholder_last();
return $this->output;
}
}Code: Select all
/**
* Display pagination
* @param page
* @param maxPages
* @param url
* @return string
*/
public function Pagination($page, $maxPages, $url = '')
{
$str = '<p>Pages: ';
if((int)$page > 1)
{
$str .= '<a href="' . $url . '?p=' . ((int)$page - 1) . '">Prev</a>' . "\n";
}
for($i = 0; $i < $maxPages; $i++)
{
if(($i + 1) != $page)
{
$str .= '<a href="' . $url . '?p=' . ($i + 1) . '">' . ($i + 1) . '</a> ' . "\n";
}
else
{
$str .= ($i + 1) . "\n";
}
}
if((int)$page < $maxPages)
{
$str .= '<a href="' . $url . '?p=' . ((int)$page + 1) . '">Next</a>' . "\n";
}
$str .= '</p>' . "\n";
return $str;
}
echo Pagination((int)$_GET['p'], mysql_num_rows($result) / $limit, basename(__FILE__));It may have given you an idea of what a class looks like, but the why is lost to you.UrButtFullOfArr0ws wrote:Ive actually written the whole class on my own ... It gave me a general idea of how my own class should be.
As for $this-> i've had a general idea about what it did from analyzing that same class.
How do i call on it... I knew how to write it but havent been able to call it..
Not to be offending, but right now i dont have time to find out everything there is to know about a class, and that's why i used another class related to mine and make it the way i wanted it, with it's flaws, and benefits...bdlang wrote:It may have given you an idea of what a class looks like, but the why is lost to you.UrButtFullOfArr0ws wrote:Ive actually written the whole class on my own ... It gave me a general idea of how my own class should be.
As for $this-> i've had a general idea about what it did from analyzing that same class.
How do i call on it... I knew how to write it but havent been able to call it..
Start here. Then read this and this.
Oh, and you might want to look at this or this.
Glad i could help you when im supposed to be asking for helpsuperdezign wrote:Oooh, I like that. The function I gave you was something I made for a little tutorial on "Basic Pagination." I think I may write a class to do that as well. You've really inspired me today.
I was wondering what to do beyond "basic..." It's hard to call something "basic," and then not have a follow-up.
Code: Select all
Class Pagination
{
var $result; //The result of mysql_query("select * from your_table").
var $perpage; //The number of results to display on page.
var $start; //The result at which to start.
var $row_num; //The total number of rows.
var $page_num; //The total number of pages, based on $perpage and $row_num
var $divider; //The symbol used to divide the page numbers
Function Paginate($result, $perpage, $start, $divider)
{
$this->query = $result;
$this->perpage = $perpage;
$this->start = $_GET['start'];
$this->page = $this->current_page();
$this->divider = isset($divider) ? $divider : " |.| ";
$this->row_num = mysql_num_rows($this->query);
return $this->compile();
}
Function Calculate_Page_Num()
{
$this->fixed = (intval($this->row_num) % intval($this->perpage)) <> 0 ? 1 : 0;
$this->page_num = intval($this->row_num) / intval($this->perpage) + intval($this->fixed);
return $this->page_num;
}
Function Current_Page()
{
$this->page = $_GET['start'] / $this->perpage;
return $this->page;
}
Function Placeholder_First()
{
If($_GET['start'] <> 0){
$this->first = "<a href='?start=0&page=" . $this->current_page() . "'>First</a>";
}
Else
{
$this->first = "<u>First</u>";
}
return $this->first;
}
Function Placeholder_Previous()
{
If($_GET['start'] = 0){
$this->previous = "<u>Previous</u>";
}
Else
{
$this->prev_calc = (intval($this->page) - 1) * intval($this->perpage);
$this->previous = "<a href='?start=" . $this->prev_calc . "&page=" . $this->current_page() . "'>Previous</a>";
}
return $this->previous;
}
Function Placeholder_1()
{
If($this->page <= 3){
If($this->page = 1){
$this->p1 = "<u>1</u>";
}
Else
{
$this->p1 = "<a href='?start=" . $this->perpage . "&page=" . $this->current_page() . "'>1</a>";
}
}
Else
{
$this->p1_1 = intval($_GET['start']) / intval($this->perpage) - 3;
$this->p1_2 = intval($this->p1_2) * intval($this->perpage);
$this->p1 = "<a href='?start=" . $this->p1_2 . "&page=" . $this->current_page() . "'>" . $this->p1_1 . "</a>";
}
return $this->p1;
}
Function Placeholder_2()
{
If($this->page <= 3){
If($this->page = 2){
$this->p2 = "<u>2</u>";
}
Else
{
$this->p2_1 = intval($this->perpage) * 2;
$this->p2 = "<a href='?start=" . $this->p2_1 . "&page=" . $this->current_page() . "'>2</a>";
}
}
Else
{
$this->p2_1 = intval($_GET['start']) / intval($this->perpage) - 2;
$this->p2_2 = intval($this->p2_1) * intval($this->perpage);
$this->p2 = "<a href='?start=" . $this->p2_2 . "&page=" . $this->current_page() . "'>" . $this->p2_1 . "</a>";
}
return $this->p2;
}
Function Placeholder_3()
{
If($this->page <= 3){
If($this->page = 3){
$this->p3 = "<u>3</u>";
}
Else
{
$this->p3_1 = intval($this->perpage) * 3;
$this->p3 = "<a href='?start=" . $this->p3_1 . "&page=" . $this->current_page() . "'>3</a>";
}
}
Else
{
$this->p3_1 = intval($_GET['start']) / intval($this->perpage) - 1;
$this->p3_2 = intval($this->p3_1) * intval($this->perpage);
$this->p3 = "<a href='?start=" . $this->p3_2 . "&page=" . $this->current_page() . "'>" . $this->p3_1 . "</a>";
}
return $this->p3;
}
Function Placeholder_4()
{
$this->p4_eval = $this->page_num - 3;
If($this->page <= 3 OR $this->page >= $this->p4_eval){
If($this->page <= 3){
$this->p4_1 = intval($this->perpage) * 4;
$this->p4 = "<a href='?start=" . $this->p4_1 . "&page=" . $this->current_page() . "'>4</a>";
}
If($this->page >= $this->p4_eval){
$this->p4_1 = (intval($this->page_num) - 4) * intval($this->perpage);
$this->p4_2 = intval($this->page_num) - 4;
$this->p4 = "<a href='?start=" . $this->p4_1 . "&page=" . $this->current_page() . "'>" . $this->p4_2 . "</a>";
}
}
Else
{
$this->p4 = "<u>" . $this->page . "</u>";
}
return $this->p4;
}
Function Placeholder_5()
{
$this->p5_eval = $this->page_num - 2;
If($this->page >= $this->p5_eval){
If($this->page = $this->p5_eval){
$this->p5 = "<u>" . $this->p5_eval . "</u>";
}
Else
{
$this->p5_1 = intval($this->page_num) - 3;
$this->p5_2 = intval($this->p5_1) * intval($this->perpage);
$this->p5 = "<a href='?start=" . $this->p5_2 . "&page=" . $this->current_page() . "'>" . $this->p5_1 . "</a>";
}
}
Else
{
$this->p5_1 = (intval($this->page_num) - intval($this->page) + 1) * intval($this->perpage);
$this->p5_2 = intval($this->page_num) - intval($this->page) + 1;
$this->p5 = "<a href='?start=" . $this->p5_1 . "&page=" . $this->current_page() . "'>" . $this->p5_2 . "</a>";
}
return $this->p5;
}
Function Placeholder_6()
{
$this->p6_eval = $this->page_num - 2;
If($this->page >= $this->p6_eval){
$this->p6_eval2 = $this->page_num - 1;
If($this->page = $this->p6_eval2){
$this->p6 = "<u>" . $this->p6_eval2 . "</u>";
}
Else
{
$this->p6_1 = intval($this->page_num) - 2;
$this->p6_2 = intval($this->p6_1) * intval($this->perpage);
$this->p6 = "<a href='?start=" . $this->p6_2 . "&page=" . $this->current_page() . "'>" . $this->p6_1 . "</a>";
}
}
Else
{
$this->p6_1 = (intval($this->page_num) - intval($this->page) + 2) * intval($this->perpage);
$this->p6_2 = intval($this->page_num) - intval($this->page) + 2;
$this->p6 = "<a href='?start=" . $this->p6_1 . "&page=" . $this->current_page() . "'>" . $this->p6_2 . "</a>";
}
return $this->p6;
}
Function Placeholder_7()
{
$this->p7_eval = $this->page_num - 2;
If($this->page >= $this->p7_eval){
If($this->page = $this->page_num){
$this->p7 = "<u>" . $this->page_num . "</u>";
}
Else
{
$this->p7_1 = (intval($this->page_num) - 1) * intval($this->perpage);
$this->p7 = "<a href='?start=" . $this->p7_1 . "&page=" . $this->current_page() . "'>" . $this->page_num . "</a>";
}
}
Else
{
$this->p7_1 = (intval($this->page_num) - intval($this->page) + 3) * intval($this->perpage);
$this->p7_2 = intval($this->page_num) - intval($this->page) + 3;
$this->p7 = "<a href='?start=" . $this->p7_1 . "&page=" . $this->current_page() . "'>" . $this->p7_2 . "</a>";
}
return $this->p7;
}
Function Placeholder_Next()
{
$this->start_1 = intval($this->page_num) * intval($this->perpage);
If($_GET['start'] = $this->start_1){
$this->next1 = "<u>Next</u>";
}
Else
{
$this->next_1 = intval($_GET['start']) + intval($this->perpage);
$this->next1 = "<a href='?start=" . $this->next_1 . "&page=" . $this->current_page() . "'>Next</a>";
}
return $this->next1;
}
Function Placeholder_Last()
{
$this->last_1 = (intval($this->page_num) - 1) * intval($this->perpage);
If($this->last_1 = $_GET['start']){
$this->last = "<u>Last</u>";
}
Else
{
$this->last = "<a href='?start=" . $this->last_1 . "&page=" . $this->current_page() . "'>Last</a>";
}
return $this->last;
}
Function Compile()
{
$this->output = $this->placeholder_first() . $this->divider;
$this->output .= $this->placeholder_previous() . $this->divider;
$this->output .= $this->placeholder_1() . $this->divider;
$this->output .= $this->placeholder_2() . $this->divider;
$this->output .= $this->placeholder_3() . $this->divider;
$this->output .= $this->placeholder_4() . $this->divider;
$this->output .= $this->placeholder_5() . $this->divider;
$this->output .= $this->placeholder_6() . $this->divider;
$this->output .= $this->placeholder_7() . $this->divider;
$this->output .= $this->placeholder_next() . $this->divider;
$this->output .= $this->placeholder_last();
return $this->output;
}
}Code: Select all
$pagination = new pagination();
echo $pagination->paginate($query, 5, isset($_GET['start']) ? $_GET['start'] : 0, " |.| ");Code: Select all
First |.| Previous |.| 1 |.| 2 |.| 3 |.| -4 |.| -2 |.| -1 |.| |.| Next |.| Lastsuperdezign wrote: constructor
Code: Select all
function print_page_1()
{
echo 'page 1' ;
}
function print_page_2()
{
echo 'page 2' ;
}
// should be
function print_page($page)
{
echo 'page '.$page ;
}Code: Select all
function append_sid($url, $non_html_amp = false) //not sure why this is here, or if it could be replaced...
{
global $SID;
if ( !empty($SID) && !preg_match('#sid=#', $url) )
{
$url .= ( ( strpos($url, '?') !== false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID;
}
return $url;
}
function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{
$total_pages = ceil($num_items/$per_page);
if ( $total_pages == 1 )
{
return '';
}
$on_page = floor($start_item / $per_page) + 1;
$page_string = '';
if ( $total_pages > 10 )
{
$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
for($i = 1; $i < $init_page_max + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "?start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
if ( $i < $init_page_max )
{
$page_string .= ", ";
}
}
if ( $total_pages > 3 )
{
if ( $on_page > 1 && $on_page < $total_pages )
{
$page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
{
$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "?start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
if ( $i < $init_page_max + 1 )
{
$page_string .= ', ';
}
}
$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
}
else
{
$page_string .= ' ... ';
}
for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "?start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
if( $i < $total_pages )
{
$page_string .= ", ";
}
}
}
}
else
{
for($i = 1; $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . "?start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
if ( $i < $total_pages )
{
$page_string .= ', ';
}
}
}
if ( $add_prevnext_text )
{
if ( $on_page > 1 )
{
$page_string = ' <a href="' . append_sid($base_url . "?start=" . ( ( $on_page - 2 ) * $per_page ) ) . '">Previous</a> ' . $page_string;
}
if ( $on_page < $total_pages )
{
$page_string .= ' <a href="' . append_sid($base_url . "?start=" . ( $on_page * $per_page ) ) . '">Next</a>';
}
}
$page_string = 'Goto_page ' . $page_string;
return $page_string;
}Code: Select all
Goto_page Previous 1, 2, 3 ... 28, 29, 30 ... 36, 37, 38 Next