[SOLVED] Pagniation help. Limit required?

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
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[SOLVED] Pagniation help. Limit required?

Post by facets »

I haven't been able to work out how to display
Prev , 1,2,3.......34,35,36.Next
Does anyone have any examples that doesn't require the SQL to have a limit set. Could I use an array to store the data?

I guess before we look into the code, a question..
Do I *have* to use Limit on my SQL query to make this work?

Code: Select all

if ( $startrow - $this->pagesize > 0 ) { $prev = $startrow - $this->pagesize; } else { $prev = 0; }

	$content .= " <a href = ".$script_name."?".$this->passargs."&fname=".$fname."&searchtext=".$searchtext."&startrow=".$prev.">Prev</a>\n";

    for ( $i = 0; $i <= $this->numrows / $this->pagesize; $i++) { // Start Forloop
    	$startmp = $this->pagesize * $i ;
		//if ( $i != 0 ) { $content .= "<b>,</b> "; }

		if ( $startmp == $startrow ) { 
			$fontclass = "medium";
			$maxpages = number_format($this->numrows / $this->pagesize, "0");

			if ( $i < $maxpages ) { 
				$next = $this->pagesize * ($i+1); 
			} else { 
				$next = $this->pagesize * $i; 
			}
			
		} else { 
			$fontclass=""; 
		}
			$content .= "<a href = ".$script_name."?".$this->
                                passargs."&fname=".$fname."&searchtext=".$searchtext."&startrow=" . $startmp." ><font class=".$fontclass.">".$i .", </font></a>";
	} // End forloop

	$content .= " <a href = ".$script_name."?".$this->passargs."&fname=".$fname."&searchtext=".$searchtext."&startrow=".$next.">Next</a>\n";
tis, Will./
Last edited by facets on Fri Apr 06, 2007 9:03 am, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you don't use the limit clause, you can retrieve all results into an array, serialize it, and store it into a file for later retrieval. You will lose the simplicity of SQL in retrieve certain results, and each instance of your script will be bloated with all of the data from that table (maybe megabytes if your table is large). It's conventional to get your database data by using LIMIT -- makes your job easier and the data retrieval mechanisms aren't limited to PHP's built-in array functions. Arrays were never intended to hold tabular data.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Thanks Aaron.
But.. :) The link to your site was very handy.
I almost have your Pagination function integrated into my page.

Will./
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Glad you could make some use of it
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think I may be missing something with your pagination code.
When you click on link 12 should it not redisplay with a tweak order? (like the bottom of your website?)

I can't seem to get the current page to set. My URL look like this..
statements.php?searchtext=&fname=All&fname=All&searchtext=&startrow=345
So I have edited the currentPage variable to suit

Code: Select all

$currentPage = (empty($currentPage)) ? '1' : $_GET['searchtext=&fname=All&fname=All&searchtext=&startrow'];
Any ideas why this would not be working?

tia, Will
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Because PHP will parse the query string. var_dump($_GET) to see the results.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The function expects a page number. Your startrow input ($_GET['startrow']) needs to be converted into a page number, something like

Code: Select all

$resultsPerPage = 20;
$pageNumber = (is_numeric($_GET['startrow']) && $_GET['startrow'] > 0) ? ceil($_GET['startrow'] / $resultsPerPage) : 1;
Then pass that page number to the function along with the total number of results and the results that are being displayed per page.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

Thank you! That did the trick.
Now, back to getting FreeBSD 6.2 running on my laptop.
Post Reply