Automatic pagination for displaying schedule information in

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hydra
Forum Newbie
Posts: 5
Joined: Mon Oct 06, 2014 6:05 am

Automatic pagination for displaying schedule information in

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Automatic pagination for displaying schedule information

Post 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.
hydra
Forum Newbie
Posts: 5
Joined: Mon Oct 06, 2014 6:05 am

Re: Automatic pagination for displaying schedule information

Post by hydra »

Can you please guide me on how to do it? Is there any other way simpler? Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Automatic pagination for displaying schedule information

Post by Celauran »

What have you tried? Where are you getting stuck?
borre
Forum Newbie
Posts: 10
Joined: Thu Oct 02, 2014 7:21 am

Re: Automatic pagination for displaying schedule information

Post 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.
hydra
Forum Newbie
Posts: 5
Joined: Mon Oct 06, 2014 6:05 am

Re: Automatic pagination for displaying schedule information

Post 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.
borre
Forum Newbie
Posts: 10
Joined: Thu Oct 02, 2014 7:21 am

Re: Automatic pagination for displaying schedule information

Post 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. :-)
hydra
Forum Newbie
Posts: 5
Joined: Mon Oct 06, 2014 6:05 am

Re: Automatic pagination for displaying schedule information

Post 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?
hydra
Forum Newbie
Posts: 5
Joined: Mon Oct 06, 2014 6:05 am

Re: Automatic pagination for displaying schedule information

Post 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.
borre
Forum Newbie
Posts: 10
Joined: Thu Oct 02, 2014 7:21 am

Re: Automatic pagination for displaying schedule information

Post by borre »

Oh wow, I think I forgot the functionality of header :D Nice!
Post Reply