Page 1 of 2

2 Questions on :"$this->" and a class

Posted: Wed Jun 20, 2007 6:49 am
by UrButtFullOfArr0ws
1) How exactly does "$this-> " work ? I've searched through php.net but haven't found anything, although user comments on some pages seem to use it.

Edit: okay question 1 is dumb i just saw it asked earlier... sorry :oops:

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. :)

Posted: Wed Jun 20, 2007 7:03 am
by Gente
Find out what is "$this->" and TNEN feel free to ask help with your class :)

Posted: Wed Jun 20, 2007 7:17 am
by UrButtFullOfArr0ws
Ive actually written the whole class on my own with some help from an older pagination class taked from this forum's section, code snippets. 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.
For the class i need 2 things:
1) Help with refining...ANYTHING that can make it better is welcome. Better in any way, meaning to remove unnecessary parts and adding some that Are neccessary.
and (kind of stupid asking it)
2) How do i call on it :? ... I knew how to write it but havent been able to call it... :lol:
Here it is:

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;
	}
}
Ehm that should be it.

Posted: Wed Jun 20, 2007 7:41 am
by superdezign
:lol: That seems like overkill.

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__));

Posted: Wed Jun 20, 2007 8:00 am
by UrButtFullOfArr0ws
:lol: :lol: :lol: :lol:

Both look like overkill.... :D

Yours shows ALL of the pages in range....
What mine intended to do is:

Im on page 10 out of 40.

First |.| Previous |.| 7 |.| 8 |.| 9 |.| 10 |.| 11 |.| 12 |.| 13 |.| Next |.| Last


Im on page 2 out of 40.

First |.| Previous |.| 1 |.| 2 |.| 3 |.| 4 |.| 5 |.| 6 |.| 7 |.| Next |.| Last

Get the idea?...


And 2nd... this is not just to show some numbers but to use the "start" and "perpage" to Limit the query.

Posted: Wed Jun 20, 2007 8:30 am
by bdlang
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..
It may have given you an idea of what a class looks like, but the why is lost to you.

Start here. Then read this and this.

Oh, and you might want to look at this or this.

Posted: Wed Jun 20, 2007 8:30 am
by superdezign
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. :D

I was wondering what to do beyond "basic..." It's hard to call something "basic," and then not have a follow-up. :wink:

Posted: Wed Jun 20, 2007 8:48 am
by UrButtFullOfArr0ws
bdlang wrote:
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..
It may have given you an idea of what a class looks like, but the why is lost to you.

Start here. Then read this and this.

Oh, and you might want to look at this or this.
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...
What i had asked is some help with this class, not on classes, and however stupid i may sound, i don't want to find it out at this moment... maybe after i finish this project, i'll take a look at those links. So thx, but no thx.
superdezign 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.
Glad i could help you when im supposed to be asking for help :D.

Posted: Wed Jun 20, 2007 9:01 am
by superdezign
UrButtFullOfArr0ws wrote:2) How do i call on it :? ... I knew how to write it but havent been able to call it... :lol:
You can't. There's no constructor. Any class that utilizes "$this->" needs a constructor. Maybe the Paginate() function was meant to be "Pagination()"?

Posted: Wed Jun 20, 2007 9:01 am
by onion2k
Writing a paging class is like writing your own framework ... it's an object oriented coding rite of passage. Everyone writes one eventually.

Posted: Wed Jun 20, 2007 9:25 am
by UrButtFullOfArr0ws
Here's an update....

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;
	}
}
I'm working at it atm so it's just a copy-paste.
And i was actually able to call to it like this:

Code: Select all

$pagination = new pagination();
echo $pagination->paginate($query, 5, isset($_GET['start']) ? $_GET['start'] : 0, " |.| ");
And it gave me this:

Code: Select all

First |.| Previous |.| 1 |.| 2 |.| 3 |.| -4 |.| -2 |.| -1 |.| |.| Next |.| Last
You can see it's obviously got problems...
superdezign wrote: constructor
:? :?

Posted: Wed Jun 20, 2007 10:51 am
by Begby
Anytime you have duplicate code repeated in several functions, thats a good hint that the functionality should be combined into one method or function like this simple example

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 ;
}
You should be able to remove most of your methods and then combine them into one.

Secondly, you should write out the logic on paper before you write your pagination script. Determine the math you will need to calculate the number of pages to show, what page you are one, etc. etc. Understanding what your class is going to do in explicit detail is going to be a lot better than playing the guessing game which is why its printing negative numbers.

Here is another pagination class you can use as an example that I wrote awhile back viewtopic.php?t=60746

Also, you should really use those links that bdlang took the time to post for you. You apparently have enough time to mess around with this class so I assume you could spare some to do some reading. I think that making an effort to learn more about classes on your own is going to pay off a lot more then taking the easy way and asking others to critique your class, which IMO you should just start over from scratch.

Posted: Wed Jun 20, 2007 11:09 am
by superdezign
Begby wrote:IMO you should just start over from scratch.
Yeah, looking at it, I have to agree. The reason I called it overkill was because I saw so many functions, but I didn't read them. That's very strangely set up.

And you still don't have a constructor.

Posted: Wed Jun 20, 2007 11:25 am
by UrButtFullOfArr0ws
Thx for your reply.
I've been away for about 90 minutes and havent been working on it.
1) The paper you said... I never needed any partly becouse i'm very good at maths and i can make a lot of operations straight in my mind and also becouse i'm good at logic... It only took me like 6 hrs to make that pagination and besides the small errors that i made (like using "int()" instead of intval() ) i was expecting it to work head on... :)

2) I will try and remove some of them but below the first placeholder (that's placeholder_first() ) i don't expect it to be simplified.

3) I'm still working on it THAT's why it's showing me negative numbers :wink:

Edit: Well... 1st, i'd start over if it weren't as i already said... I dont have much time available, and more to that, this isnt a key feature to what i'm trying to do, so there's even less time to this than i got overall.
2nd, i really wasnt looking for criticising, all i needed is if i could do anything to tweak that particular code, and even if i sound stubborn, i didn't want to change to another code after already trying 5 different snippets. The original question was help on this one. :?
3rd, the reason why i didnt do as much as i can and when i found a point after which i couldn't go further is again ... time.... i gotta finish this in a few days hopefully and i still got a long way to go with the rest of the project. So i thought i might be able to correct mistakes on the code while seeing it here where hopefully i'd see stuff i DIDN'T see while correcting it at the same time.
Sorry about that :?

Posted: Thu Jun 21, 2007 3:43 pm
by UrButtFullOfArr0ws
OK... How's this? :D

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>&nbsp;&nbsp;' . $page_string;
		}

		if ( $on_page < $total_pages )
		{
			$page_string .= '&nbsp;&nbsp;<a href="' . append_sid($base_url . "?start=" . ( $on_page * $per_page ) ) . '">Next</a>';
		}

	}

	$page_string = 'Goto_page ' . $page_string;

	return $page_string;
}
Does the output look familiar? :lol: :lol: :lol: :lol:

Code: Select all

Goto_page Previous  1, 2, 3 ... 28, 29, 30 ... 36, 37, 38  Next