listarticles.class.php
<?php
/*########################################################################################
EXAMPLE USAGE OF THIS CLASS
include('listarticles.class.php');
if ( (isset($_GET['page_num']) && $_GET['page_num'] != "") && is_numeric($_GET['page_num']) ){
$pg = $_GET['page_num'];
} else {
$pg = 1;
}
// new ListArticles Object (PDO object,items_per_page,current_page_number)
$list = new ListArticles($PDO_object,5,$pg);
// Show pagination
$list->showPagination();
// Show result set
$list->showList()
#############################################################################################################
*/
?>
<?php
abstract class ListArticles{
public $items_per_page;
public $conn;
public $sql;
public $result;
public $page_count;
public $page_sql;
public $page_result;
public $curPageNum;
public $rowcnt;
public function __construct(PDO $connection,$items_per_page,$curPageNum){
$this->items_per_page = $items_per_page;
$this->conn = $connection;
$this->curPageNum = $curPageNum;
// Generate initial query (to get result count)
$this->sql = $this->generateQuery();
// Execute initial query
$this->result = $this->conn->query($this->sql);
// Generate page count
$this->page_count = $this->getPageCount($this->result);
// Generate sql for this page only
$this->page_sql = $this->generatePageQuery($this->sql);
// execute sql for this page only
$this->page_result = $this->conn->query($this->page_sql);
}
protected function clean($tainted_data)
{
if(get_magic_quotes_gpc()){
$tainted_data = stripslashes($tainted_data);
}
$cleaned_data = htmlentities(trim($tainted_data));
return $cleaned_data;
}
public function showPagination()
{
// Previous Selector
if ($this->curPageNum > 1){
$prev = $this->curPageNum -1;
} else {
$prev = $this->curPageNum;
}
echo "<a class=\"prev_and_next\" href='$_SERVER[PHP_SELF]?";
echo "page_num=$prev";
foreach ($_GET as $key=>$val){
if ($key == "page_num"){
continue;
}
echo "&$key=$val";
}
echo "'>";
echo "<</a> ";
// Numbers
for ($i = 1; $i <= $this->page_count; $i++){
if ($i != $this->curPageNum){
echo "<a href='$_SERVER[PHP_SELF]?";
echo "page_num=$i";
foreach ($_GET as $key=>$val){
if ($key == "page_num"){
continue;
}
echo "&$key=$val";
}
echo "'>";
echo "$i</a> ";
} else {
echo "<b>$i</b>";
}
}
// Next Selector
if ($this->curPageNum < $this->page_count){
$next = $this->curPageNum + 1;
} else {
$next = $this->curPageNum;
}
echo " <a class=\"prev_and_next\" href='$_SERVER[PHP_SELF]?";
echo "page_num=$next";
foreach ($_GET as $key=>$val){
if ($key == "page_num"){
continue;
}
echo "&$key=$val";
}
echo "'>";
echo "></a> ";
}
public function generatePageQuery() // Uses the generateQuery() function
{ // to generate a new similar query
$page = $this->curPageNum; // with limit clause that only generates
// records for the specific page 'curPageNum'
if ($page == 1){ // (as passed into the constructor)
$this->sql .= " LIMIT $this->items_per_page";
} else {
$start = ($page * $this->items_per_page) - ($this->items_per_page);
$this->sql .= " LIMIT $start,$this->items_per_page";
}
//echo $sql;
return $this->sql;
}
public function getPageCount($res) // Returns the number of pages for the
{ // result set.
$this->rowcnt = $res->rowCount();
return $num_pages = ceil($this->rowcnt/$this->items_per_page);
}
abstract public function generateQuery(); // Generate the initial db query to get rowcount etc
// dependant on GET/POST variables
abstract public function showList(); // Layout specific record display.
// foreach ($this->page_result as $row){
// echo $row['field_name'] etc
} // end class
?>
Paul's Code Library
Categories
HomeAJAX
classes
database_functions
date_functions
file_functions
googlevideo_and_youtube_api
htaccess
image_functions
string_functions