<?php

require_once 'dbconfig.php';
class paginate
{

 private $conn;
     public function __construct()
      {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
      }
      
     public function runQuery($sql)
     {
      $stmt = $this->conn->prepare($sql);
      return $stmt;
     }
 
     public function dataview($query)
     {
         $stmt = $this->conn->prepare($query);
         $stmt->execute();
 
         if($stmt->rowCount()>0)
         {
                while($row=$stmt->fetch(PDO::FETCH_ASSOC))
                {
                   ?>
                   <tr>
                    <?php if ($row['Dia'] == "Total") { ?>
                     <td style="background: #eee; border-top: 2px solid #333;"><strong><?php echo $row['Dia']; ?></strong></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['8a9']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['9a10']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['10a11']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['11a12']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['12a13']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['13a14']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['14a15']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['15a16']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['16a17']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['17a18']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['18a19']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><?php echo $row['19a20']; ?></td>
                     <td style="background: #eee; border-top: 2px solid #333;"><strong><?php echo $row['Total']; ?></strong></td>
                    <?php } else { ?>
                      <td><?php echo $row['Dia']; ?></td>
                     <td><?php echo $row['8a9']; ?></td>
                     <td><?php echo $row['9a10']; ?></td>
                     <td><?php echo $row['10a11']; ?></td>
                     <td><?php echo $row['11a12']; ?></td>
                     <td><?php echo $row['12a13']; ?></td>
                     <td><?php echo $row['13a14']; ?></td>
                     <td><?php echo $row['14a15']; ?></td>
                     <td><?php echo $row['15a16']; ?></td>
                     <td><?php echo $row['16a17']; ?></td>
                     <td><?php echo $row['17a18']; ?></td>
                     <td><?php echo $row['18a19']; ?></td>
                     <td><?php echo $row['19a20']; ?></td>
                     <td><strong><?php echo $row['Total']; ?></strong></td>
                    <?php } ?>                   

                   </tr>
                   <?php
                }
         }
         else
         {
                ?>
                <tr>
                <td>No hay datos para mostrar...</td>
                </tr>
                <?php
         }
  
 }
 
 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)
 {
  
        $self = $_SERVER['PHP_SELF'];
  
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
  
        $total_no_of_records = $stmt->rowCount();
  
        if($total_no_of_records > 0)
        {
            ?><tr><td colspan="30"><?php
            $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 class='btn btn-warning' href='".$self."?page_no=1'>Inicio</a>&nbsp;&nbsp;";
               echo "<a class='btn btn-warning' href='".$self."?page_no=".$previous."'>Anterior</a>&nbsp;&nbsp;";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
            if($i==$current_page)
            {
                echo "<strong><a class='btn btn-primary' href='".$self."?page_no=".$i."'>".$i."</a></strong>&nbsp;&nbsp;";
            }
            else
            {
                echo "<a class='btn btn-secondary' href='".$self."?page_no=".$i."'>".$i."</a>&nbsp;&nbsp;";
            }
   }
   if($current_page!=$total_no_of_pages)
   {
        $next=$current_page+1;
        echo "<a class='btn btn-warning' href='".$self."?page_no=".$next."'>Siguiente</a>&nbsp;&nbsp;";
        echo "<a class='btn btn-warning' href='".$self."?page_no=".$total_no_of_pages."'>Ultimo</a>&nbsp;&nbsp;";
   }
   ?></td></tr><?php
  }
 }
}