动态更改查询-PHP,jQuery [英] Change queries dynamically - php, jquery

查看:62
本文介绍了动态更改查询-PHP,jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想让2个主要的SQL查询更加动态.

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

当用户单击营销链接时,我希望使用该链接ID更新以下2个查询:

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"'); ?>

因此,当单击营销链接时,我希望查询将查询中的类别更改为任何链接ID或类.在这种情况下是营销".

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

共有三页,发布如下:

pagination.php

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:

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:

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());
                });

});

推荐答案

为简化起见,即使偏移量为0,也应始终使用LIMIT和页面大小进行查询.

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

.. LIMIT 0,3

让另一个查询或缓存的值对记录进行计数(以获取最大页数).相比于num_row'ing SELECT *,MySQL的COUNT更好.

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

这篇关于动态更改查询-PHP,jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆