Yet another MYSQL Pagination Class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Burrito wrote: what advantage do you gain by having them in different objects rather than properties?
With different objects you can override *all* the methods and get a completely different presentation of the paginated item... (And example of why that can be beneficial can be found here.)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I hit the link...and the first thing that caught my eye was the example names you selected... 8O

8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ha yeah that genious :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the fact that those names sound familiar to you says enough ;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

timvw wrote:the fact that those names sound familiar to you says enough ;)
ummm...yeah, I heard them from my uhh umm friend.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The code that TmVW uses is very similar to mine -- because the ideas went into and came out of the same discussion (code here). :)

The thing about pagers is that they decompose pretty quickly into a bunch of classes if you start to refactor them. The ususal things: request processor, calculations, datasource, HTML (or other) output. The current Skeleton code has pager code extended from that discussion that does things like recalc and resume when returning from sub-pages of the list, and database independence, etc. I also end up having the Pager just do the basics and have the View build the links exactly how they are specificed by the design.
(#10850)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Burrito wrote:I hit the link...and the first thing that caught my eye was the example names you selected... 8O

8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O
Yeah, but shouldn't it be:

Code: Select all

$data = array(
        array(‘surname’ => ‘Jameson’, ’forename’ => ‘Jenna’),
        array(‘surname’ => ‘Banks’, ’forename’ => ‘Briana’),
        array(‘surname’ => ‘Giovanni’, ’forename’ => ‘Aria’),
        array(‘surname’ => ‘Rush’, ’forename’ => ‘Daniella’),
        array(‘surname’ => ‘Flowers’, ’forename’ => ‘April’)
);
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

jayshields wrote:
Burrito wrote:I hit the link...and the first thing that caught my eye was the example names you selected... 8O

8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O 8O
Yeah, but shouldn't it be:

Code: Select all

$data = array(
        array(‘surname’ => ‘Jameson’, ’forename’ => ‘Jenna’),
        array(‘surname’ => ‘Banks’, ’forename’ => ‘Briana’),
        array(‘surname’ => ‘Giovanni’, ’forename’ => ‘Aria’),
        array(‘surname’ => ‘Rush’, ’forename’ => ‘Daniella’),
        array(‘surname’ => ‘Flowers’, ’forename’ => ‘April’)
);
hmm...I wouldn't know :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

OK I'm off to... ermm... do some research around these names before I go to bed...
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

JayBird wrote:Hmmm...weird, I'm sure someone posted a reply when i checked this morning.

Anyway...he was saying that it may be better to just COUNT() the rows in the DB instead of returning the entire resultset. Well, this is something i was talking to JCart about last night.

To do this, we were think of doing something along the lines of (as suggested by Aborint in a thread a while ago)
It was me... i thought this was the code snipets forum and i made that comment. When i saw it was the code critique i thoght my comment was not complete enough to keep and deleted it... i guess i should have kept it.
Anyway... cpliting the query using the front as a separator to explode() should do it... i come in 5 minutes ;)
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post by pedrotuga »

hey there.. here i am again...

i made some chages but i didnt test them yet.

I replaced this:

Code: Select all

function totalResults()
	{
			$this->result = mysql_query($this->query);
			$this->totalResults = mysql_num_rows($this->result);
		   
			if($this->totalResults != 0)
			{
					$this->maxPage = ceil(($this->totalResults / $this->perPage));
			}
	}
By this:

Code: Select all

function totalResults()
	{		
			$this->query = str_replace ( "from", "FROM", $this->query );
			$this->query_pieces=explode("FROM");
			$this->query_test = "select count(*) from ".$this->query_pieces[1];
			$this->result = mysql_query($this->query_test);
			$this->totalResults = mysql_result($this->result,0);
		   
			if($this->totalResults != 0)
			{
					$this->maxPage = ceil(($this->totalResults / $this->perPage));
			}
	}
this wont work with subqueries as they have more than one ocorrence of "from".
Also, a dummy doubt i never got solved... when should i use the %delimiters% on str_replace??? and why?
Post Reply