views:

259

answers:

1

Hey I'm trying to get 2 main sql queries to be more dynamic.

When the user clicks the marketing link, I want the 2 queries below to be updated with that link Id:

1. <?php generate_pagination('SELECT * FROM explore WHERE category="marketing"'); ?>

so when the marketing link is clicked, I want the query to change the category in the query to whatever the link id or class is. In this case 'marketing'.

Here is the other query that needs to be updated dynamically with the same link click:

2. $sql = "SELECT * FROM explore WHERE category='marketing' ORDER BY category LIMIT $start,$per_page";

There are a total of three pages, posted below:

pagination.php

<?php
function generate_pagination($sql) {
  include_once('config.php');
  $per_page = 3;

  //Calculating no of pages
  $result = mysql_query($sql);
  $count = mysql_num_rows($result);
  $pages = ceil($count/$per_page);

  //Pagination Numbers
  for($i=1; $i<=$pages; $i++)
  {
    echo '<li class="page_numbers" id="'.$i.'">'.$i.'</li>';
  }
}
?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="jquery_pagination.js"></script>

<div id="loading" ></div>
<div id="content" data-page="1"></div>
<ul id="pagination">

<?php generate_pagination('SELECT * FROM explore WHERE category="marketing"'); ?>

<a href="#" id="marketing">Marketing</a>

</ul>
<br />
<br />

pagination_data.php:

<?php
include('config.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}

$start = ($page-1)*$per_page;
//$sql = "select * from explore order by id limit $start,$per_page";
$sql = "SELECT * FROM explore WHERE category='marketing' ORDER BY category LIMIT $start,$per_page";
$result = mysql_query($sql);
?>
<table width="800px">
<?php
while($row = mysql_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['site_description'];
$site_price=$row['site_price'];

?>
<tr>
<td><?php echo $msg_id; ?></td>
<td><?php echo $message; ?></td>
<td><?php echo $site_price; ?></td>
</tr>
<?php
}
?>
</table>

jquery_pagination.php:

$(document).ready(function(){

    //Display Loading Image
    function Display_Load()
    {
        $("#loading").fadeIn(900,0);
        $("#loading").html("<img src='bigLoader.gif' />");
    }
    //Hide Loading Image
    function Hide_Load()
    {
        $("#loading").fadeOut('slow');
    };


   //Default Starting Page Results

    $("#pagination li:first").css({'color' : '#FF0084'}).css({'border' : 'none'});

    Display_Load();

    $("#content").load("pagination_data.php?page=1", Hide_Load());



    //Pagination Click
    $("#pagination li").click(function(){

        Display_Load();

        //CSS Styles
        $("#pagination li")
        .css({'border' : 'solid #dddddd 1px'})
        .css({'color' : '#0063DC'});

        $(this)
        .css({'color' : '#FF0084'})
        .css({'border' : 'none'});

        //Loading Data
        var pageNum = this.id;

    $("#content").load("pagination_data.php?page=" + pageNum, function(){
          Hide_Load();
          $(this).attr('data-page', pageNum);
    });
});

// Editing below.        
        // Sort content Marketing    
        $("#pagination a#marketing").click(function () {


        Display_Load();

        //CSS Styles
        $("#pagination li")
        .css({'border' : 'solid #dddddd 1px'})
        .css({'color' : '#0063DC'});

        $(this)
        .css({'color' : '#FF0084'})
        .css({'border' : 'none'});

                var pageNum = $('#content').attr('data-page');

                $("#content").load("pagination.data.php?page=" + pageNum, Hide_Load());
                });

});
+1  A: 

To simplify things, you should always query with a LIMIT and page size, even if the offset is 0.

.. LIMIT 0,3

Let another query or cached value count the records (for max page number) . Mysql's COUNT is better for this than num_row'ing SELECT *.

$start = intval($_GET['page']); // zero if null or false
$sql = "SELECT * FROM explore WHERE category='marketing' ORDER BY category LIMIT $start,$per_page";
bibby
this class doesn't play with limits... or so says the http://phpsense.com/php/php-pagination-script.html class page
CheeseConQueso