Zeroic
Zeroic

Reputation: 251

how can I refresh the query inside the div

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.

Upvotes: 4

Views: 3989

Answers (3)

Lappies
Lappies

Reputation: 923

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

Upvotes: 4

Zeroic
Zeroic

Reputation: 251

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

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

You need to loop through results in PHP

while( $random_story = mysql_fetch_object($query) ) {
    echo $random_story->title;
}

Upvotes: 2

Related Questions