change page 10 query in 10 seconds

Asked

Viewed 325 times

2

I have a page that lists all my events from the event table (tbl_eventos). I want to list all the events of the first week of the year, last 10 seconds, list all the events in the second week of the year, and so on.

In my table events I have the following fields: id_evento; nome; data

For the consultation I’m using php and mysql. I’m thinking of using ajax, but I don’t know how to update the content every 10 seconds and how to send the date I will look for.

  • Would events be displayed more or less like a slider? Enter the source code you’ve already made.

  • 2

    So, I don’t know how interesting it is making requests to the bank and server every 10 seconds, in my view the most efficient would be you bring the query with everything that will be displayed at once, ai with Avascript you keep changing the content of what will be displayed

  • @rray, yes, let’s imagine that it would be a slider but will actually update the content of the page

1 answer

3


Assuming you use jQuery.

Javascript:

jQuery(function(){

    var semana = 0;

    var itv = window.setInterval(function(){
        jQuery.get("consulta.php?semana="+semana, function(data){
            jQuery("#container").html(data);
        });

        semana++;

    },10000);
});

PHP:

/*Supondo que você já possua a conexão com o banco de dados*/

$semana = $_GET["semana"];

/*Supondo que a variável $semana seja 0, a consulta irár retornar todos os registros da primeira semana do ano, independente do ano. Para restringir, utilize por exemplo, 'AND YEAR(data) = 2014'*/
$query = mysql_query("SELECT * FROM tbl_eventos WHERE WEEK(data) = {$semana}");

while ( $row = mysql_fetch_object($query) ){
    echo $row->id_evento . " - " .  $row->nome . " - " . $row->data . "<br>";
}

I didn’t test it, but it should work.

  • the variable "$offset" takes the date? the days? so how can I limit the query by days, increasing one week at a time?

  • I forgot that detail, I’ll correct the answer

  • Corrected response @pc_oc

  • the code only runs at once, that is, the counter is not always repeating itself

  • Exchange the setTimeout for setInterval

  • This is one way to do this, but it is not the best... the correct believe would be to make a single query and via javascript, go alternating the results, as @Marcelo Bonifazio commented.

  • changed window.setInterval(Function() by setInterval(Function() and already gave. thanks!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.