Limiting Records

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
dmgt
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:40 pm

Limiting Records

Post by dmgt »

This code limits records to 12 records per page. Can anyone tell me how to control the number of records per page?

I don't see anywhere where the limit is set to 12... Not sure what I am missing.

Thanks

Code: Select all

 
 
<?
class newsSql{
 
function getNews($perPage='0',$onPage='0',$where='')
{
 
global $DB,$AUTH;
 
$sqlstart = ($perPage * $onPage) - ($perPage);
$sqlstop= $perPage;
 
$limit = ($sqlstart == 0 && $sqlstop == 0 ) ? '' : " LIMIT ".$sqlstart.",".$sqlstop;
 
$sql="SELECT SQL_CALC_FOUND_ROWS  * from GTM_NEWS  ";
 
$sql .= ($where != "") ? $where : '';
$sql .=" ORDER by date_entered DESC " ;
 
$sql .=  $limit;
 
$res = $DB->run_query($sql);
 
//print_r($DB);
if($res && $res->num_rows > 0 )
{
//print_r($res);
$pgs = $DB->getTotalPages($perPage);
$res->total_num_items = $pgs['totalItems'];
$res->total_pages = $pgs['totalPages'];
return $res;
}
return false;
}
 
 
function getArticle($id)
{
 
    global $DB;
 
    if( ! is_numeric($id) ) return false;
 
   $res = $DB->run_query("SELECT * FROM GTM_NEWS WHERE news_id='".$id."'");
 
        if($res && $res->num_rows == 1)
        {
//print_r($res);
                return $res->row;
 
        }
        else
        {
                return false;
        }
 
 
 
}
 
}// end class
 
?>
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Limiting Records

Post by susrisha »

Code: Select all

 
$sql .=  $limit;
 
echo $sql; //add this statement and you will know... WHERE to limit it 
$res = $DB->run_query($sql);
 
dmgt
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:40 pm

Re: Limiting Records

Post by dmgt »

Thanks- below is what i received from the echo... It doesn't explain to me where the value of 12 is set- I must be missing something pretty obvious...


SELECT SQL_CALC_FOUND_ROWS * from GTM_NEWS ORDER by date_entered DESC LIMIT 0,12
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Limiting Records

Post by Benjamin »

It's set in the code calling the class...

Code: Select all

 
function getNews($perPage='0',$onPage='0',$where='')
 
dmgt
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:40 pm

Re: Limiting Records

Post by dmgt »

That makes sense. But I can't figure out where it is getting the 12 per page?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Limiting Records

Post by Benjamin »

Where are you calling the function?
dmgt
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:40 pm

Re: Limiting Records

Post by dmgt »

Code: Select all

 
<?
require_once('news_sql.php');
require_once(PATH_MODS.'maps/maps_data.php');
 
class getnews extends news_sql {
 
    var $NEWSSQL = '';
    var $perPage = '3';
 
function getnews()
{
// nothing
}
 
 
 
function getNewsList($where='')
{
 
    global $REQ,$OUT,$FNS;
    
    $onPage = (isset($REQ->urlArray['page']) && is_numeric($REQ->urlArray['page'])) ? $REQ->urlArray['page'] : 1;
    $queryRes = $this->NEWSSQL->getNews($this->perPage,$onPage,$where);
    $output = array();
    if($queryRes && $queryRes->num_rows > 0 )
    {
        foreach($queryRes->result as $row)
        {
            $row['mids'] = explode(',',$row['news_mapids']);
            $output[] = $row;
        }   
                $OUT->templateVars['newsList'] = $output;
                $OUT->templateVars['queryTotal']  = $queryRes->total_num_items;
                $OUT->templateVars['Paging'] =$FNS->getPaging($queryRes->total_pages,$onPage,$this->perPage);
    }
    else
        {
                $OUT->templateVars['no_query_results'] = true;
        }
 
}
 
function getArticle($id)
{
$data = $this->NEWSSQL->getArticle($id);
return $data;
 
}
 
}// end class
?>
 
Post Reply