/    Sign up×
Community /Pin to ProfileBookmark

paging class (limit woes)

This is my paging class (can’t remember where i got it from but ive used it for years):

the mysql_data_seek is doing its job perfect but here is whats happening:
[url]http://www.knowj.com/photography/1/[/url]

as you can see no LIMIT is being passed into the query causing it to start from the right row but getting all the results rather than the limited amount.

How would i be able to build a LIMIT into this paging class without having to change all my SQL querys through out my site?

[code=php]
<?php
class paging extends database
{
var $pageSize;
var $page;
var $row;

function paging()
{

}

function paging_build($pageSize)
{
$resultpage = $_GET[‘resultpage’];
//run the mysql query
$this->execute();

$this->pageSize = $pageSize;
if ((int)$resultpage <= 0)
{
$resultpage = 1;
}
if ($resultpage > $this->getNumPages())
{
$resultpage = $this->getNumPages();
}
$this->setPageNum($resultpage);
}

function getNumPages()
{
if (!$this->results)
{
return FALSE;
}

return ceil(mysql_num_rows($this->results) /
(float)$this->pageSize);
}

function setPageNum($pageNum)
{
if ($pageNum > $this->getNumPages() or $pageNum <= 0)
{
return FALSE;
}
$this->page = $pageNum;
$this->row = 0;
//change the pointer to the new row and set $this->results to that value
mysql_data_seek($this->results,($pageNum-1) * $this->pageSize);
}

function getPageNum()
{
return $this->page;
}

function isLastPage()
{
return ($this->page >= $this->getNumPages());
}

function isFirstPage()
{
return ($this->page <= 1);
}

function fetchArray()
{
if (!$this->results)
{
return FALSE;
}
if ($this->row >= $this->pageSize)
{
return FALSE;
}
$this->row++;
return mysql_fetch_array($this->results);
}

function getPageNav($queryvars = ”)
{
$nav = ”;
if (!$this->isFirstPage())
{
$nav .= ‘<a href=”‘.preg_replace(“(/[0-9]+/)”, ”, $_SERVER[‘REQUEST_URI’]).’/’.($this->getPageNum()-1).’/”>Prev</a>’;
}
if ($this->getNumPages() > 1)
{
for ($i=1; $i<=$this->getNumPages(); $i++)
{
if ($i==$this->page)
{
$nav .= “$i “;
}
else
{
$nav .= ‘<a href=”‘.preg_replace(“(/[0-9]+/)”, ”, $_SERVER[‘REQUEST_URI’]).’/’.$i.’/”>’.$i.'</a>’;
}
}
if (!$this->isLastPage())
{
$nav .= ‘<a href=”‘.preg_replace(“(/[0-9]+/)”, ”, $_SERVER[‘REQUEST_URI’]).’/’.($this->getPageNum()+1).’/”>Next</a> ‘;
}
return $nav;
}
}
}
?>
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@knowjauthorJan 05.2008 — the obvious way would be to only loop out the amount i need with a for loop but this would be extremely inefficient as i would be loading a var with huge amounts of data than dropping most of it.

Anyone know an easy way to drop a limit in within the paging class?

[code=php]
$resultpage = $_GET['resultpage'];
$this->query .= " LIMIT $pageSize";
//run the mysql query
$this->execute();
[/code]


this just removed the paging as only the amount of rows specified were going into the paging class.
Copy linkTweet thisAlerts:
@knowjauthorJan 06.2008 — Resolved it was a very armature mistake of not using
[code=php]
function fetchArray()
{
if (!$this->results)
{
return FALSE;
}
if ($this->row >= $this->pageSize)
{
return FALSE;
}
$this->row++;
return mysql_fetch_array($this->results);
}
[/code]


to call results
×

Success!

Help @knowj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...