0

Hi there Everybody! I am facing a little issue. I have a DIV where I need to show the count of rows from the database. The problem is that when the page refresh the count is not still updated because the database mySQL takes a while, so I have to refresh the page again. Do you know how can I show the count of the rows maybe with javascript? In a way that the count of the rows will be continuosly checked and updated without page reloading.. If I need jQuery for this, just to let you know I am on version 1.3.2 Let me know! Thanks so much!

0

2 Answers 2

1

yea sure, ajax it... first, it does sound odd that when u refresh the page the database isnt ready, but lets assume there is a sync issue where db gets updated out of sync with the current page... the solution is ajax

function doCountUpdate(){
$.get("url_to_return_count",function(data){
    assuming the returned data is a number
    $("#myDiv").text(data);
    setTimeout(doCountUpdate, 1000);
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

is recalling ajax every 1 second a good idea? maybe more like 10-30 seconds.even a minute..
just saying... but yeah ive done 1 second gaps before, the page is so small a png image call is probably heavier... anyways, a good performance test at the end is always a good idea
What the other file should do on the other end? just an "echo" function? or what? I am new to ajax. Thanks
@DiegoP just a echo will do but sending in json_encode format is better option
1

I think at page load, You can do that by simply a ajax call to a function and return the row and display it on a DIV.

$(document).ready(function() {
 // put all your jQuery goodness in here.
  $.ajax({
  url: "somepage.php",
  success: function(data){
    $('#div_id').text(data);
   }
 });

});

3 Comments

The problem is that the count it's not on an external page. It's a variable inside the same page, so I can't use an ajax call like that in this situation. Any other good advise? thanks so much!
yes, of course you can. you must also write "somepage.php" as @ime example shows. it only returns rows count. so the called page replaces inital value
@DiegoP, you can the self page also... by ajax, load, etc..... OR you can assign that php variable to js varaiable, and then display it on div..e.g: => var count=<?=$count?>

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.