Page 1 of 1

[DONE]Divide article list into serveral pages

Posted: Thu 21. Oct 2004, 10:40
by thanhnq
Hello guys,
This is very important while your site is keeping growing so pls. tell me How to...? Or any idea???

ARTICLELIST:max_count

Posted: Sat 23. Oct 2004, 12:08
by thanhnq
Hi,
I write the following code to display articles list with pages. Pls. tell if there some thing wrong or will cause errors with the whole phpwcms!

Code: Select all

//In file  \include\inc_front\front.func.inc.php add the following
// usage: ARTICLELIST:max_count (display max_count summaries from CatAlias)
$content["all"] = preg_replace('/\{ARTICLELIST:(\d+)\}/ie', 'get_summary_list ($content["cat_id"], $template_default, $1, $db);', $content["all"]); 
=====================

Code: Select all

//Add the following into the file \include\inc_front\front.func.inc.php. 
//The level 1 link to each cat must be in this format: http://www.yourweb.com/index.php?cat&page=0&sum=0

function get_pager_data($total, $limit, $page) {
           $total  = (int) $total; 
           $limit    = max((int) $limit, 1); 
           $page     = (int) $page; 
           $numPages = ceil($total / $limit); 

           $page = max($page, 1); 
           $page = min($page, $numPages); 

           $offset = ($page - 1) * $limit; 

           $ret = array ("offset" => $offset, "limit" => $limit, "numPages" => $numPages, "page" => $page);            

           return (sizeof($ret)) ? $ret: array();			
}

function get_data_from_acat($offset, $limit, $dbcon) {
	//This code block is similar to the get_actcat_articles_data function, so you can modify the function instead
	$ao = get_order_sort($GLOBALS['content']['struct'][ $cat_id ]['acat_order']);
	$sql  = "SELECT *, UNIX_TIMESTAMP(article_tstamp) AS article_date FROM ".DB_PREPEND."phpwcms_article ";
	$sql .= "WHERE article_cid='$cat_id' AND ";
	$sql .= "article_public=1 AND article_aktiv=1 AND article_deleted=0 ";
	$sql .= "AND article_begin<NOW() AND article_end>NOW() ";	
	$sql .= "ORDER BY ".$ao[2]." LIMIT ".$offset.",".$limit.";";	

	if($result = mysql_query($sql, $dbcon)) {
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$data[$row["article_id"]] = array(
									"article_id"		=> $row["article_id"],
									"article_cid"		=> $row["article_cid"],
									"article_title"		=> $row["article_title"],
									"article_subtitle"	=> $row["article_subtitle"],
									"article_keyword"	=> $row["article_keyword"],
									"article_summary"	=> $row["article_summary"],
									"article_redirect"	=> $row["article_redirect_to"],
									"article_date"		=> $row["article_date"],
									"article_username"	=> $row["article_username"],
									"article_sort"		=> $row["article_sort"],
									"article_notitle"	=> $row["article_notitle"],
									"article_created"	=> $row["article_created"],
									"article_image"		=> unserialize($row["article_image"])
											  );
		}
		mysql_free_result($result);
	}
	return (count($data)) ? $data : array();	
}

// returns article summary for ARTICLELIST-replacement 
function get_summary_list ($cat_id, $template_default, $max_count, $dbcon) {       
	if ($_GET['id']) {		
		return $GLOBALS['content']["main"];
	}
	$page = $_GET['page']; 
	$total = $_GET['sum'];
	if (!$total) { 
		$sql = "SELECT count(*) FROM ".DB_PREPEND."phpwcms_article WHERE article_cid='$cat_id';";
		$result = mysql_query($sql, $dbcon); 
		$total = mysql_result($result, 0, 0); 
		mysql_free_result($result);		
	} 
	$limit = $max_count;
	// work out the pager values 
	$pager  = get_pager_data($total, $limit, $page); 
	$offset = $pager["offset"]; 
	$limit  = $pager["limit"]; 
	$page   = $pager["page"];

	$teaser_content = get_data_from_acat ($offset, $limit, $dbcon); 
	$teaser_content = list_articles_summary ($teaser_content, $template_default); 
	
	$alias = html_specialchars($GLOBALS['content']['struct'][$cat_id]['acat_alias']);

	// output paging system (could also do it before we output the page content) 
	if ($page == 1) // this is the first page - there is no previous page 
	        $teaser_content .= "<p> Previous"; 
	else            // not the first page, link to the previous page 
	        $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page - 1) . '&sum=' . $total . '>Previous</a>'; 

	for ($i = 1; $i <= $pager["numPages"]; $i++) { 
	        $teaser_content .= " | "; 		
	        if ($i == $page) 
	            $teaser_content .= $i; 
	        else 
	            $teaser_content .= '<a href=index.php?' . $alias . '&page=' .$i. '&sum=' . $total . '>'.$i.'</a>'; 
	} 
	 $teaser_content .= " | ";	
	if ($page == $pager["numPages"]) // this is the last page - there is no next page 
	        $teaser_content .= " Next </p>"; 
	else            // not the last page, link to the next page 
	        $teaser_content .= '<a href=index.php?' . $alias . '&page=' . ($page + 1) . '&sum=' . $total . '>Next </a>';

	return $teaser_content;         
}
=====================