简体   繁体   English

如何动态更新SQL查询-jQuery

[英]How to update sql query dynamically - jquery

I am trying to figure out how I can update my sql query dynamically. 我试图弄清楚如何动态更新我的SQL查询。

On the main page, I have a pagination script that counts how many pages there will be: 在主页上,我有一个分页脚本,该脚本计算将要有多少页:

<?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>';
  }
}
?>

I then have in the body of the same page this line of code to populate the page numbers: 然后,我在同一页的主体中包含以下代码行以填充页码:

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

So, that line is displaying the necessary amount of page numbers to be displayed for just the 'marketing' category. 因此,该行显示的是仅针对“营销”类别显示的必要页码数量。

The problem that I am having is with that single line of code. 我遇到的问题是那一行代码。 I want to make the category dynamic, so instead of it being hardcoded to 'marketing' I would like jquery to get the id of an element and place it in. 我想使类别动态化,因此与其将其硬编码为“市场营销”,不如希望jquery获取元素的ID并将其放置在其中。

The element would be this link that I have on the same page: 元素将是我在同一页面上拥有的此链接:

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

So, when the user clicks that link, I am trying to place the id of the link in the category section of the query using jquery. 因此,当用户单击该链接时,我正在尝试使用jquery将链接的ID放在查询的类别部分中。

I hope that made sense, and if anyone can assist me on this that would be great. 我希望这是有道理的,如果有人可以在这方面帮助我,那将是很棒的。

First, the PHP side: 首先,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>';
  }
}
?>

<?php generate_pagination("SELECT * FROM explore WHERE category='" . mysql_real_escape_string ( $_POST ['category'] ) . "'"); ?>

Then in your jquery post: 然后在您的jquery帖子中:

$("a.category").click(function() {
  $.post("test.php", { category: $(this).attr("id") },
    function(data){
      //Load your results into the page
    });
});

On click we take the ID, pass it to the server as a post variable category , then on the server, grab it, escape it properly for security, and use that query. 单击时,我们获取ID,将其作为post变量category传递给服务器,然后在服务器上,抓住它,为安全起见对其进行正确的转义,然后使用该查询。 Load your results the same way you are now, that part doesn't change. 以与现在相同的方式加载结果,该部分不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM