Ok, here it is. It’s very messy though. :hr:
It needs a lot of clean up and improvements. I also need to add validation and exceptions. I will keep on working on it tomorrow.
If you have any suggestions to simplify the code or find a way to improve the class and methods please let me know.
After I come up with version 0.2 I will post it here.
class TableFromMySQL{
protected $_tablename;
protected $_select_columns;
protected $_where;
protected $_itemsPerPage;
protected $_pageNumber;
protected $_query;
protected $_table;
public function __construct($tablename, $select_columns, $where, $itemsPerPage, $pageNumber){
$this->_tablename = $tablename;
$this->_select_columns = $select_columns;
$this->_where = $where;
$this->_itemsPerPage = $itemsPerPage;
$this->_pageNumber = $pageNumber;
$this->_query = 'SELECT '.$this->_select_columns.' FROM '.$this->_tablename.' WHERE '.$this->_where;
$this->setLimit($this->_itemsPerPage);
}
/////////////////////////////////////
// //
// add the LIMIT part of the query //
// //
/////////////////////////////////////
private function setLimit($itemsPerPage){
$limit = $itemsPerPage;
if(isset($this->_pageNumber)){
$pageNumber = $this->_pageNumber;
$start = ($pageNumber-1)*$limit;
}
else{
$start = 0;
}
$this->_query = $this->_query.' LIMIT '.$start.', '.$itemsPerPage;
}
////////////////////////////////////////////////////////////
//
// Get total number of records
//
////////////////////////////////////////////////////////////
public function countRecords(){
$query_count = 'SELECT COUNT(*) FROM '.$this->_tablename.' WHERE '.$this->_where;
$result_count = mysql_query($query_count);
$fetch_row = mysql_fetch_row($result_count);
$num_rows = $fetch_row[0];
return $num_rows;
}
////////////////////////////////////////////////////////////
//
// pagination
//
////////////////////////////////////////////////////////////
public function pagination($limit, $add_to_url){
$add_to_url = $_SERVER['PHP_SELF'].'?';
$num_rows = $this->countRecords();
$adjacents = 3;
if(isset($this->_pageNumber)){
$pageNumber = $this->_pageNumber;
$start = ($pageNumber-1)*$limit;
}
else{
$start = 0;
}
if($pageNumber==0) $pageNumber=1;
$prev = $pageNumber-1;
$next = $pageNumber+1;
// $lastpage is = total pages / items per page, rounded up
$lastpage = ceil($num_rows/$limit);
// $last page minus 1
$lpm1 = $lastpage - 1;
$pagination='';
if($lastpage>1){
$pagination.='<div class="pagination">';
// previous
if($pageNumber>1) {$pagination.='<a href="'.$add_to_url.'pagenumber='.$prev.'"> previous </a>';}
else {$pagination .= '<span class="disabled"> previous </span>';}
// pages
if($lastpage<7+($adjacents*2)){
for($counter=1; $counter<=$lastpage; $counter++){
if($counter==$pageNumber) {$pagination .= '<span class"current">'.$counter.'</span>';}
else {$pagination .= '<a href="'.$add_to_url.'pagenumber='.$counter.'">'.$counter.'</a>';}
}
}
elseif($lastpage > 5 + ($adjacents * 2)){
if($pageNumber < 1 + ($adjacents * 2)){
for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if($counter == $pageNumber){
$pagination .= '<span class="pagination_current">'.$counter.'</span>';
}
else{
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$counter.'">'.$counter.'</a>';
}
}
$pagination .= "...";
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$lpm1.'">'.$lpm1.'</a>';
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$lastpage.'">'.$lastpage.'</a>';
}
elseif($lastpage - ($adjacents * 2) > $pageNumber && $pageNumber > ($adjacents * 2)){
$pagination .= '<a href="'.$add_to_url.'pagenumber=1'.'">1</a>';
$pagination .= '<a href="'.$add_to_url.'pagenumber=2'.'">2</a>';
$pagination .= '...';
for($counter = $pageNumber - $adjacents; $counter <= $pageNumber + $adjacents; $counter++){
if($counter == $pageNumber){
$pagination .= '<span class="pagination_current">'.$counter.'</span>';
}
else{
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$counter.'">'.$counter.'</a>';
}
}
$pagination .= '...';
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$lpm1.'">'.$lpm1.'</a>';
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$lastpage.'">'.$lastpage.'</a>';
}
else{
$pagination .= '<a href="'.$add_to_url.'pagenumber=1">1</a>';
$pagination .= '<a href="'.$add_to_url.'pagenumber=2">2</a>';
$pagination .= '...';
for($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++){
if($counter == $pageNumber){
$pagination .= '<span class="pagination_current">'.$counter.'</span>';
}
else{
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$counter.'">'.$counter.'</a>';
}
}
}
}
# next button
if($pageNumber < $counter - 1){
$pagination .= '<a href="'.$add_to_url.'pagenumber='.$next.'"> next </a>';
}
else{
$pagination .= '<span class="pagination_disabled"> next </span>';
}
$pagination .= '</div><br />';
}
$query = mysql_query($this->_query);
$result_count = mysql_query($this->_query);
$fetch_row = mysql_fetch_row($result_count);
if($fetch_row[0]==0) {
$pagination .= '<p>There is no items to display</p>';
}
while($row=mysql_fetch_array($query)){
// set columns to display
// echo $row['id'].'-'.$row['firstname'].'<br />';
}
return $pagination;
}
public function recommendedStylesForPagination(){
return '
<style>
div.pagination {
padding: 3px;
margin: 3px;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;
text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;
font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;
color: #DDD;
}</style>';
}
public function tableFromMySQL($display_columns){
$table = '<table>';
$result = mysql_query($this->_query);
$table .= '<tr>';
foreach($display_columns as $key => $value){
$table .= '<th>'.$key.'</th>';
}
$table .= '</tr>';
while($row = mysql_fetch_array($result)){
$table .= '<tr>';
foreach($display_columns as $value){
$table .= '<td>'.$row[$value].'</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
$table .= $this->pagination($this->_itemsPerPage, '');
return $table;
}
///////////////////
// //
// execute query //
// //
///////////////////
public function query(){
$result = mysql_query($this->_query);
// while($row = mysql_fetch_array($result)){
// echo '<pre>';
// print_r($row);
// echo '</pre>';
// }
}
}
if(isset($_GET['pagenumber']) && is_numeric($_GET['pagenumber'])){$pageNumber=$_GET['pagenumber'];}else{$pageNumber=1;}
$display_columns = array(
'clientid'=>'id',
'ID'=>'cardid',
'Name'=>'firstname',
'Last'=>'lastname',
'Email'=>'email',
'Mobile'=>'mobilephone',
'Home'=>'homephone',
'Work'=>'workphone'
);
$select_columns = ' clients.id, clients.cardid, clients.firstname, clients.lastname,
clientsContact.clientID, clientsContact.address, clientsContact.city, clientsContact.state, clientsContact.email,
clientsContact.homephone, clientsContact.workphone, clientsContact.mobilephone ';
$tablename = ' clients, clientsContact ';
$where = 'clients.id=clientsContact.clientID AND userid='.$_SESSION['id'];
$itemsPerPage = 3;
$table = new TableFromMySQL($tablename, $select_columns, $where, $itemsPerPage, $pageNumber);
$table->query();
echo $table->recommendedStylesForPagination();
echo $table->tableFromMySQL($display_columns);
?>