Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- /************************
- - SIMPLE SAMPLE PAGING -
- note : not yet tested,
- not yet finished
- oh no! ^_^V
- created by : CAHYA DSN
- created date: 2012-10-19
- *************************/
- //Database Configuration
- //-------------------
- $dbhost='localhost';
- $dbuser='test';
- $dbpass='test';
- $dbname='test';
- //Database connection
- //-------------------
- $db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
- //-- get 1st content id for current page
- //--------------------------------------
- $page=isset($_GET['page'])?$_GET['page']:0;
- $id=0;
- $next_id='';
- if(isset($_SESSION['pages']) && !empty($_SESSION['pages']))
- {
- $id=$_SESSION['pages'][$page];
- if($_SESSION['pages'][$page]<$_SESSION['pagenum'])
- {
- $next_id=' AND id<'.$_SESSION['pages'][$page+1];
- }
- }
- //show content per page
- //-------------------
- $sqldetail = 'SELECT id,title,content FROM table WHERE id>='.$id.$next_id.' ORDER BY id DESC';
- if ($details = $db->query($sqldetail))
- {
- while($row=$details->fetch_object())
- {
- echo "<div class=\"content_box\">\n"
- ."<div class=\"content_title\"><a href=\"?detail=".$row->id."\">".$row->title."</a></div>\n";
- ."<div class=\"content\">".$row->content."</div>\n"
- ."</div>";
- }
- $details->close();
- }
- //build paging navigation
- // paging model : 1|2|3|4|5
- //-------------------
- $perpage=5; //number of contents to be displayed per page
- if(!isset($_SESSION['pages']) || empty($_SESSION['pages']))
- {
- $sqlpaging = 'SELECT id FROM table ORDER BY id DESC';
- if ($result = $db->query($sqlpaging))
- {
- $num=$result->num_rows;
- if(num>0)
- {
- echo "<div class=\"navigation\">\n";
- $pages=array();
- $pagenum=ceil($num/$perpage);
- for($i=0;$i<$pagenum-1;$i++)
- {
- $result->data_seek($perpage*$i);
- $row = $result->fetch_object();
- $pages[$i]=$row->id;
- echo ($i==0?'':'|').($page==$i?' '.$i.' ':' <a href="?page='.$i.'">'.($i+1).'</a> ');
- }
- echo "</div>\n";
- $_SESSION['pages']=$pages;
- $_SESSION['pagenum']=$pagenum;
- }
- $result->close();
- }
- }else{
- if($_SESSION['pagenum']>0){
- echo "<div class=\"navigation\">\n";
- for($i=0;$i<$_SESSION['pagenum'];$i++){
- echo ($i==0?'':'|').($page==$i?' '.$i.' ':' <a href="?page='.$i.'">'.($i+1).'</a> ');
- }
- echo "</div>\n";
- }
- }
- // paging model: prev | next
- // paging model: first|prev|next|last
- // paging model: prev|... 4|5|[6]|7|8 ..|next
- $db->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement