There's nothing wrong with doing it as Weedpacket suggests. However, I prefer to solve this type of problem with a more generic type of paging solution to give me more versatility in how to build my interfaces. Using a framework such as CodeIgniter or Kohana for example, in a function such as:
$campaigns = Portfolio::getCampaign($get['client']);
I'd generally include parameters such as $page and $limit. Such as:
$data= Portfolio::getCampaigns($get['client'],$get['page'],$get['limit']);
Then, in the Portfolio class' getCampaigns method I'd do something like:
public function getCampaigns($client_id=null,$page=1,$limit=1){
//we'll want to return the data and a little useful info for paging
$return= array(
'campaigns'=>array(),
'total_records' => 0,
'total_pages' => 0
);
if($client_id !== null){
//find out how many records/pages there are given the current page size
$query = sprintf("SELECT COUNT(client_id) as count FROM campaigns WHERE client_id=%s",
$this->db->escape($query)
);
$return['total_records']=$this->db->query($query)->current()->count;
//don't bother querying the rows if there aren't any
if(!$return['total_records'])return $return;
//calculate total pages
$return['total_pages'] = ceil($return['total_records']/$limit);
//now before calculating our offset for the paged query, we need to account for dummies
//that try to page past beginning or end of data
if($page > $return['total_pages']){
$page = $return['total_pages'];
}
if($page < 1)$page=1;
//calculate our offset for the current result set based on the page and the limit
$offset = ($page-1) * $limit;
//finally, get our page of records
$query = sprintf("SELECT * FROM campaigns WHERE client_id=%s LIMIT %s OFFSET %s",
$this->db->escape($client_id)),
$this->db->escape($page),
$this->db->escape($limit)
);
$result = $this->db->query($query);
//assign the records.
if($result->count() > 0){
$return['campaigns']=$result->result_array();
}
}
return $return;
}
Then in your frontend code, it's easy to construct next/prev links and display info about the current page, how many pages there are, etc. You'd just pass a $page and $limit var in every next/prev link.
echo "Page $page of {$data['total_pages']}";
echo "(showing ".($page*$limit)-$limit+1." through ".($page*$limit)." of {$data['total_records'})<br/>";
if($page > 1){
echo "<a href='{$_SERVER['PHP_SELF']}?page=".($page-1)."&limit=1' title='previous'>previous</a>";
}
if($page < $data['total_pages']){
echo "<a href='{$_SERVER['PHP_SELF']}?page=".($page+1)."&limit=1' title='next'>next</a>";
}
I just wrote all that off the top of my head, so there may be some typos or other errors, but you get the idea. It maybe overkill for your purposes, but if you set the limit to 1, you get the effect of paging through the records 1 at a time like you're trying to do and if you ever want to add interface elements to let people choose their page size arbitrarily you then have that option.