October 28, 2012

Suggestions with pagination

Question by dorin dorin

I’ve a problem with pagination. The php code of pagination query is:

$ <? if (isset($_GET["page"])) { 

    $Page = preg_replace("/[^0-9]/","", $_GET["page"]);
} else {
    $Page = 0;
}
$limit       = 10;
$StartFrom  = $limit * $Page;
$TotalFiles = mysql_num_rows(mysql_query("SELECT * FROM linkuri WHERE `categorie` = 'Afaceri' AND status = 1"));
$TotalPages = floor($TotalFiles / $limit); ?>

And code to display it:

$i = 0; while ($TotalPages >= $i) { echo '<a class="active imgf" style="opacity: 1;margin-bottom:3px; margin-top:3px;" href="afaceri.php?page='.$i.'">'.($i+1).'</a>';$i++;}

The problem is I am trying to make the display as: PAGES: "BACK" 1 2 3 4 5 "NEXT"

Answer by Starx

First I would like to suggest an improvement on your query. To count the total rows use:

SELECT count(*) as `totalpost` FROM linkuri WHERE `categorie` = 'Afaceri' AND status = 1

Then, you are create that pagination by doing something like this:

$totalPost = 50; //Dummy total post
$limit = 10;

$pages = $totalPost / $limit; //Giving 5

//Now we know there are five pages
for($i=1; $i<=$pages; $i++) {
  echo $i; // Better echo something like <a href="link">$i</a>
}

P.S: This is a very basic example

After you get the hang of how to create the pagination effect, check this tutorial

How to paginate with PHP?

June 14, 2010

How to use jquery ui slider to create a pagination effect and load the content in a <DIV>?

Question by user366106

I want to create a pagination script using jquery UI’s slider widget. So far I have got the slider working but I dont know how to send request to the server to fetch new content.

So far this is my HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>jQuery UI Slider - Range slider</title>
    <link type="text/css" href="themes/base/jquery.ui.all.css" rel="stylesheet" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.ui.core.js"></script>
    <script type="text/javascript" src="jquery.ui.widget.js"></script>
    <script type="text/javascript" src="jquery.ui.mouse.js"></script>
    <script type="text/javascript" src="jquery.ui.slider.js"></script>      
    <style type="text/css">
        body { padding:5em; }
    </style>
    <script type="text/javascript">
    $(function() {
        $("#slider-range").slider({
            min: 1,
            max: 14,
            values: [1],
            slide: function(event, ui) {
                $(".values").html(ui.values[0];);
            }
        });
    });
    </script>
</head>
<body>
<div class="values" style="padding:2em;"></div>
<div id="slider-range"></div>

<div class="info" style="margin-top:2em; background:#CCC;padding:2em;">
Here is where the content will be displayed.
</div>

</body>

Thanks in Advance

Answer by Starx

Well, You can send a request at the slide event of your slider to send a request to the server at put the fetched data inside a div

  $("#slider-range").slider({
   min: 1,
   max: 14,
   values: [1],
   slide: function(event, ui) {
    var newPage = ui.values[0];
    $(".info").load("content.php", { page: newPage });
                                //load the content into a division
   }
  });

UPDATED
a sample of content.php

<?
     $recordsperpage = 15;
     if(!isset($_POST['page'] or empty($_POST['page']) { $page =1 ; }
     else { $page = $_POST['page']; }

     $limit = $page * $recordsperpage.",".$recordsperpage;
     $query = "SELECT * FROM yourtable LIMIT ".$limit;
     $result = mysql_query($query) or die(mysql_error());
     while($row = mysql_fetch_array($result)) {
          //Display the records in your pattern
     }
?>
?>
...

Please fill the form - I will response as fast as I can!