2010-02-22 60 views
0

嗨,我試圖讓2個主要的sql查詢更加動態。動態地更改查詢-php,jquery,

當用戶點擊了銷售環節,我想下面的2個查詢到與該鏈接ID更新:

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

所以點擊營銷鏈接時,我希望查詢更改類別查詢任何鏈接ID或類是。在這種情況下,'營銷'。

這是一個需要用相同的鏈接點擊動態更新的其他查詢:

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

共有三頁,張貼下面是:

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"></script> 
<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

爲了簡化問題,你應該總是用LIMIT和頁面大小查詢,即使偏移量爲0。

.. LIMIT 0,3 

讓另一個查詢或緩存值計記錄(用於最大頁碼)。 Mysql的COUNT比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"; 
+0

這個班級沒有限制......或者說http://phpsense.com/php/php-pagination-script.html班級頁面 – CheeseConQueso 2010-03-23 19:37:10