4

I am using JavaScript to refresh div content and load a PHP file containing a query to get data from MySQL.

This is my jQuery code:

<script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#random_notes').load('include/blocks/random_stories.php').fadeIn("slow");
    }, 2000); // refresh every 10000 milliseconds
</script>
<div id="random_notes" class='random'>s</div>

This is my PHP file:

<?php
    include "../../cp/config.php";
    $query = mysql_query("SELECT title FROM stories ORDER BY RAND()");
    $random_story = mysql_fetch_object($query);
    echo $random_story->title;
?>

The page loads and shows one row. The refresh does not get another row from the database. What am I missing?

Is there another good piece of code to do the same thing? This code needs cleaning cache whenever I do anything.

Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
Zeroic
  • 241
  • 1
  • 4
  • 16
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Mar 27 '13 at 08:35
  • Thanks for the link it will help me much coz iam still learning – Zeroic Mar 27 '13 at 08:37
  • try running the php script on its own, check what is displayed, you will also have to be looping through the result set – Lappies Mar 27 '13 at 09:41
  • @Lappies Yeah its working and wvery refresh change the rows but i need the jquery to refresh the div automatically its refresh successfully but the php file always get the same result coz its not refreshed all i need is refreshing the php file – Zeroic Mar 27 '13 at 10:12

3 Answers3

4

I just tested your logic and it works fine for me, this is what i have

<div id="random_notes">
sdas
</div>

<script>
$(document).ready(function(){
    setInterval(function() {
        $('#random_notes').load('include/blocks/random_stories.php').fadeIn("slow");
    }, 5000)
});
</script>

this should work for you

Lappies
  • 903
  • 2
  • 11
  • 29
2

You need to loop through results in PHP

while( $random_story = mysql_fetch_object($query) ) {
    echo $random_story->title;
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • thanks you but its same thing only one row then get the same row always why the random dont work ? – Zeroic Mar 27 '13 at 08:44
  • its 30 rows and i it show all then i used LIMIT 1 its now show only one and dont refresh – Zeroic Mar 27 '13 at 08:51
0

Ok Done i got the fix

<script>
 $(document).ready(function() {
     $("#random_notes").load("include/blocks/random_stories.php");
   var refreshId = setInterval(function() {
     $("#random_notes").load("include/blocks/random_stories.php");
   }, 2000);
   $.ajaxSetup({ cache: false });
});
</script>

<div id="random_notes" class='random'>s</div>

it was cach problems i just turn cach off everything going fine now

Zeroic
  • 241
  • 1
  • 4
  • 16