<?php
class paginate
{
    private $db;
        function __construct($Conn)
     {
        $this->db = $Conn;
     }

    public function dataview($query)
     {
        $stmt = $this->db->prepare($query);
        $stmt->execute();
        $count = $stmt->rowCount();
        if($stmt->rowCount()>0)
         {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
             {
   			    $name = $row["name"];
 	    	    $comment = $row["comment"];
                $datetime = $row["datetime"];
		echo '<TABLE BORDER="0" WIDTH="99%" align="center" bgcolor="#B7C1C3" style="font-size: medium; color: #351DCC" colspan="1" rowspan="1">
            <tr>
            <td width="117">Name</td>
            <td width="14">:</td>
            <td width="357">' . $name . '</td>
            </tr>
            <tr>
            <td valign="top">Comment</td>
            <td valign="top">:</td>
            <td>' . $comment . '</td>
            </tr>
            <tr>
            <td valign="top">Date/Time </td>
            <td valign="top">:</td>
            <td>' . $datetime . '</td></td>
            </table>
            <hr style="width: 100%; height: 1px; background-color: #000000; border: none">  ';
                  }
	     }
         else
         {
         echo '  <p style="text-align: center">No entries in guestbook. Please try again later.</p></td>';
         }
     }
 
    public function paging($query,$records_per_page)
     {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
            $query2=$query." limit $starting_position,$records_per_page";
            return $query2;
     }
 
    public function paginglink($query,$records_per_page,$count)
     {
  	    $self = $_SERVER['PHP_SELF'];
        $stmt = $this->db->prepare($query);
        $stmt->execute();
        $search = "";
        $total_no_of_records = $stmt->rowCount();
        if($total_no_of_records > 0)
        {
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
               $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
               $previous =$current_page-1;
               echo "<a href='".$self."?count=$count&search=$search&page_no=1'>First</a>&nbsp;&nbsp;";
               echo "<a href='".$self."?count=$count&search=$search&page_no=".$previous."'>Previous</a>&nbsp;&nbsp;";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
            if($i==$current_page)
            {
                echo "<strong><a href='".$self."?count=$count&search=$search&page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a></strong>&nbsp;&nbsp;";
            }
            else
            {
                echo "<a href='".$self."?count=$count&search=$search&page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
            }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<a href='".$self."?count=$count&search=$search&page_no=".$next."'>Next</a>&nbsp;&nbsp;";
                echo "<a href='".$self."?count=$count&search=$search&page_noo=".$total_no_of_pages."'>Last</a>&nbsp;&nbsp;";
            }
        }
}
}
?>