how to refresh php div every few seconds

706 views Asked by At

So I'm basically trying to implement an online trading card game in php and (maybe minimal) javascript, and so far I have a GUI set up in php, a database with the required tables (so far) to hold the decks, fields, and other various data. I've got it to the point where i have a function that initializes the fields for both players and populates their fields and hands with cards from their deck. I also got it to the point where a player on another computer or device can take the room number of the game and join the game so the see the exact initialized field.

my next objective was to be able to set up the turn logic, and allow the screen to update for one player while the other is making plays during their turn. My problem is I'm not sure what the minimum viable solution will be for this, because I have long intricate functions that display the current field based off of values that come from a field table. So my ideal logic is set up like this:

//find game form

//host game form

//if host is set (post){
  //call insert field function (a long function that creates a table for the current game being played and initializes the field and hand with random cards from the deck table)  
  //link game to host for future field printing
  // populate host function (a function that prints a table of every card on the field with the most up to date variables to represent each spot) 
}

//if find is set (post){
  //call insert field function
  //link game to host for future field printing
  //a button form to start the game which begins by calling heads or tails
  // populate find function (same as host but for find player side)
}

//if start game is set (post){
  // do game logic such as coin flip which will be notified to opponent in a message field in the game table upon the refresh
}

I was wrapping the populate host function in a refreshable div like this:

print "<div><meta http-equiv='refresh' content='5' />"; //host populate just disapears
populatehost($roomnumber); //populates self and opponents in hosts perspsective
print "</div>";

so that the function would be called every 5 seconds or so, but this just causes the field to disappear after 5 seconds. So...

  1. Why is it disappearing?

  2. How can i get it to refresh the page and just display one instance of the field using the populate field function?

  3. Is this approach viable? if not, what's the easiest approach I could take and how?

  4. I also tried a solution where the function was called every 5 seconds and gotten rid of, but i'm not sure if I was doing it right, the PHP manual didn't explain it very clearly, and after lots of trial and error i think i crashed my server. It was something like this:

    //loop

    ob_start();
    populatehost($_SESSION['gamenumber']);
    sleep(5);
    ob_end_clean();
    

I kept playing around with that logic but it either printed infinitely, didn't print at all, or crashed my server. I'm afraid to play around even more for fear of crashing the server again. Maybe someone can explain the correct way to do it or why it is not viable if it isn't?

I'm sorry if these are dumb questions, I've spent days and days researching approaches to this problem, I wasn't sure if I could implement it using javascript or ajax along with my field printing function, I tried some and they weren't working, and I'm not sure if it's because my approach is viable or not.

1

There are 1 answers

6
R.K.Bhardwaj On

You can take javascript or Ajax help for div you can assign id to the div and call id in javascript:

jQuery(function () {
    var $els = $('div[id^=quote]'),
        i = 0,
        len = $els.length;

    $els.slice(1).hide();
    setInterval(function () {
        $els.eq(i).fadeOut(function () {
            i = (i + 1) % len
            $els.eq(i).fadeIn();
        })
    }, 2500)
})

check this fiddle:

http://jsfiddle.net/arunpjohny/asTBL/

Thanks