Page 1 of 1

Automatic pagination for displaying schedule information in

Posted: Mon Oct 06, 2014 6:12 am
by hydra
I want to display some scheduling information on LCD TV. For this i manage to create a php page that display the data. I set 10 data per page for display, for this have the pagination code. its working well but i want to run that pagination automatically that means automatically after few seconds the page turns to second page then third page etc..and starts back the cycle once it reached the last page. but i don't know how to implement this. please can anyone help me on this. below are the code for reference:

pagination.php;-

Code: Select all

<?php
$con = mysql_connect("localhost","root",""); 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("test", $con); //Provide database name which has our data for pagination.

  $query = mysql_query("select CTime, Venue, Lecturer, Subject, Course from schedule where TDate = CURRENT_DATE"); 
  $total_rows = mysql_num_rows($query);

  $base_url = 'https://localhost/page/';    
  $per_page = 10;                           
  $num_links = 8;                           
  $total_rows = $total_rows; 
  $cur_page = 1;                           


    if(isset($_GET['page']))
    {
      $cur_page = $_GET['page'];
      $cur_page = ($cur_page < 1)? 1 : $cur_page;            
    }
[syntax=php]
$offset = ($cur_page-1)*$per_page;
$pages = ceil($total_rows/$per_page);

$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;

$res = mysql_query("select CTime, Venue, Lecturer, Subject, Course from schedule where TDate = CURRENT_DATE LIMIT ".$per_page." OFFSET ".$offset);

mysql_close($con);
?>
[/syntax]

index.php:-

Code: Select all

<!DOCTYPE html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="eng">
    <head>
    <?php

    ?>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Pagination</title>
        <link href="column-options.css" rel="stylesheet" type="text/css" />
        </head>
    <body>
        <div id="kotak">    
    <?php

    include("pagination.php"); 


        if(isset($res))
        {


            echo '<table class="stat">';           
            echo'<th>Class Duration</th><th>Venue</th><th>Lecturer</th><th>Subject</th><th>Course</th></tr>'; 
            while($result = mysql_fetch_assoc($res))
            {
              echo '<tr>';
              echo '<td>'.$result['CTime'].'</td>'.'<td>'.$result['Venue'].'</td>'.'<td>'.$result['Lecturer'].'</td>'.'<td>'.$result['Subject'].'</td>'.'<td>'.$result['Course'].'</td>' ;
              echo '</tr>';
            }
            echo '</table>';

        }

    ?>
    </div>

    <div id="pagination">
        <div id="pagiCount">
            <?php

                if(isset($pages))

                {  
                    if($pages > 1)        
                    {    if($cur_page > $num_links)    
                        {   $dir = "first";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.(1).'">'.$dir.'</a> </span>';
                        }
                       if($cur_page > 1) 
                        {
                            $dir = "prev";
                            echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page-1).'">'.$dir.'</a> </span>';
                        }                 

                        for($x=$start ; $x<=$end ;$x++)
                        {

                            echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?page='.$x.'">'.$x.'</a> ';
                        }
                        if($cur_page < $pages )
                        {   $dir = "next";
                            echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?page='.($cur_page+1).'">'.$dir.'</a> </span>';
                        }
                        if($cur_page < ($pages-$num_links) )
                        {   $dir = "last";

                            echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pages.'">'.$dir.'</a> '; 
                        }   
                    }
                }
            ?>
        </div>
    </div>  
</body>
</html>
I hope someone can help me to get this working. Thank you.

Re: Automatic pagination for displaying schedule information

Posted: Mon Oct 06, 2014 7:34 am
by Celauran
Since you're looking to implement behaviour client-side, you'll need to use JavaScript. There are a few different ways this can be accomplished. Perhaps the easiest, depending on the total number of rows, is to fetch everything, set up all your divs, hide them, and use JS to cycle through them in very much the same way image carousels work.

Re: Automatic pagination for displaying schedule information

Posted: Mon Oct 06, 2014 9:08 am
by hydra
Can you please guide me on how to do it? Is there any other way simpler? Thanks.

Re: Automatic pagination for displaying schedule information

Posted: Mon Oct 06, 2014 12:34 pm
by Celauran
What have you tried? Where are you getting stuck?

Re: Automatic pagination for displaying schedule information

Posted: Mon Oct 06, 2014 12:37 pm
by borre
You can create a hidden div with the amount of pages you have. So count all pages within PHP and put it after your loop in a hidden div.

Than you might want to make use of jQuery to perform your task.

Code: Select all

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(function() {
 var totalPages = $('#hiddenDivID').text();
 var page = getParameterByName("page");
 
 if (page >= totalPages) {
    top.location.href = '/?page=1';
 } else {
    top.location.href = '/?page='+ parseInt(page+1);
 }
});
I haven't tested it, but I think you might try something like this.

Re: Automatic pagination for displaying schedule information

Posted: Tue Oct 07, 2014 7:04 pm
by hydra
Thanks borre for the reply. I am really new in php and not really sure how to implement your idea. It will be great if you can help me on this code.

Re: Automatic pagination for displaying schedule information

Posted: Thu Oct 09, 2014 5:39 am
by borre
Hey,

You can do this below your pagination, inside the isset($pages).

Code: Select all

echo "<div id='pageAmount' style='display: none;'>".$pages."</div>";
And than append the JavaScript code inside the head (be sure to include the jQuery library found on jQuery.com) and try it out.

Be sure to change "#hiddenDivID" to "#pageAmount".

I hope you learn something from this. :-)

Re: Automatic pagination for displaying schedule information

Posted: Thu Oct 09, 2014 10:02 pm
by hydra
I tried to implement the code as what you instructed. but it redirect me straight to this url : http://localhost:81/?page=1
which part is wrong?

Re: Automatic pagination for displaying schedule information

Posted: Thu Oct 09, 2014 11:08 pm
by hydra
Hi,

I manage to get it working just by using the following code :-

header('Refresh: 10; URL='.$_SERVER['PHP_SELF'].'?page='.$nextpage);

It works perfectly! Thank you for your help. God bless.

Re: Automatic pagination for displaying schedule information

Posted: Fri Oct 10, 2014 12:34 am
by borre
Oh wow, I think I forgot the functionality of header :D Nice!