dongni3854 2010-02-22 00:46
浏览 63

动态更改查询 - php,jquery

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"></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条回答

  • douzhouhan4618 2010-02-22 01:20
    关注

    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";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿