Page 1 of 1

[SOLVED] Pagniation help. Limit required?

Posted: Thu Apr 05, 2007 11:50 pm
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./

Posted: Fri Apr 06, 2007 12:13 am
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.

Posted: Fri Apr 06, 2007 3:49 am
by facets
Thanks Aaron.
But.. :) The link to your site was very handy.
I almost have your Pagination function integrated into my page.

Will./

Posted: Fri Apr 06, 2007 5:01 am
by aaronhall
Glad you could make some use of it

Posted: Fri Apr 06, 2007 8:45 am
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

Posted: Fri Apr 06, 2007 8:49 am
by feyd
Because PHP will parse the query string. var_dump($_GET) to see the results.

Posted: Fri Apr 06, 2007 8:55 am
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.

Posted: Fri Apr 06, 2007 9:02 am
by facets
Thank you! That did the trick.
Now, back to getting FreeBSD 6.2 running on my laptop.