0

Is there anyway to control the limit of entries from a database using jquery.


I have the PHP Data.php:

mysqli_query($con,"(select * from social order by id desc limit 30) order by id asc");

But I want to be able to edit the limit 30 and use $.get() to request the php again on calling a click function in jquery. Is this possible?


The $.get():

 $.get("Data.php", function (data3) {
     $(".data").html(data3);
 });

My code now looks like this:

Data.php:

$limit_amt = mysqli_real_escape_string($_GET['limit']);

$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");

while($row = mysqli_fetch_array($result))
  {
      echo "<tr class='border_bottom'>";
      echo "<th scope='col'>";
      echo "<div align='left'><span class='name'>".$row['name']."</span></div><br />";
      echo "<span class='text'>".$row['comunicate']."</span><br />";
      echo "<div align='right'><span class='time'>".$row['date']."</span></div>";
      echo "</th>";
      echo "</tr>";

  }

Index.php:

$.get("Data.php",{ limit: 40}, function (data3) { .
         $(".data").html(data3);

    });
2
  • Unclear, need more code. Do you mean you want a dynamic value in place of 30? Also what's your current $.get() code? Commented Feb 17, 2014 at 5:20
  • Would you like anything else? Commented Feb 17, 2014 at 5:24

2 Answers 2

1

In your $.get():

//#btn would represent an HTML element like <input type="button" id="btn">
$("#btn").click(function(e){
    // I have given a static value here but you can change it to a variable or 
    // however you want to pass
    $.get("Data.php",{ limit: 40}, function (data3) { .
         $(".data").html(data3);
    });
}

In your Data.php:

//Fetching the value and saving into a variable
$limit_amt = mysqli_real_escape_string($_GET['limit']);
//Using the fetched value to change the limit amount
mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");
Sign up to request clarification or add additional context in comments.

12 Comments

There is also a Write-to.php page which sends the data (The data that Data.php receives) is that a problem?
Do you mean Write-to.php is also having a $.get() function call to Data.php? If so, that's not a problem, you can have multiple $.get() requests to the same file, just remember to use different parameter names.
Can you try $result = mysqli_query($con,"(select * from social order by id desc limit {$limit_amt}) order by id asc"); ? The second error is probably related to the first failed query. Also, this line: $limit_amt = mysqli_real_escape_string($con,$_GET['limit']);
Your query seems to be failing. Can you try echo mysqli_error($con); after mysqli_query? Alternatively you could try the actual query on your phpMyAdmin if you have access.
|
0

I think there is some mistake in the query that you have used

please try

$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.")");

instead of

$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.