Hello

I´m trying to make a function that give-me data from a data base mysql at time there is no problem but i would like to now how i can make that funcion to be called from x hover x seconds, the ideia is just to se if there is a new data on the database without having to press f5!!!!:eek:

this is for a roller panell with news on it and when a new noticie is put on datebase the page can load on it without refresh.....

sorry for my english but i´m not good at it....:mrgreen:

Recommended Answers

All 9 Replies

I can't think of any way to do it with PHP.. PHP does not do things at runtime, it is all processed by the server than converted into something your browser understands. you might have better luck withsomething like ASP or Java or Javascript.

Javascript is definatly the way forwards. You can either use javascript to refresh or you can use an AJAX query to get fresh data and update you page.

That's correct, you must use javascript to do that, either with ajax or japascript, i have a complete example that uses a countdown timer to control the time a user have to answer a question, you can see it at: Trivia Game

In this example i use a combination of php, javascript and a HTML Template to output the related choices of any questions that are stored in a MySql database and other stuff.

Apart from the registration process It works following this sequence:

  1. Check User: if the user that tries to play is a registered user and if this user is logged in, if not redirects to the login page.
  2. Check plays per day: Verify how many times a user plays the Trivia per day, no one can play more than 5 times per day, this value is configurable.
  3. Check questions per test: Verify the number of questions played per test, also this value is configurable and if the user reach the max number of questions finish the play and compute and store the results.
  4. Load Template: Verify if the template file can be opened, if not the script finish otherwise continue and stores the file in a temporary variable.
  5. Calculate number of questions.
  6. Pick and populate a random question.
  7. Select and populate the question's related choices.
  8. Build form's output for the choices using radio inputs.
  9. Get and set the initial time for the timer.
  10. Build some hidden fields especially the one for the count down timer.
  11. Replace the result value of the template that will be showed if a question have already answered.
  12. Replace the values of the template with all the data obtained before.
  13. Finally show the output to the user.

Something very important is that the template file -a HTML file- includes the necesary javascript code that controls the timer, without it, it dont work fine, also this file includes the design layout, the form's tag, a submit button and a simple input text box that dynamically shows how many seconds are left, when this time reach 0 or when the user press the submit button simply process the question and computes the results, then goes or to the next question or finishes the played test.

This is the code for the timer located in the template file between the form tag:

Time Left: <input type="text" name="seconds" size="3">
<script type="text/javascript">
var myTime = 20;
function countDown() {
  document.form.seconds.value = myTime;
  if (myTime == 0) {
    idq=document.form.idquestion.value;
    for (i=0;i<document.form.answer.length;i++) {
      if (document.form.answer[i].checked==true) {
        ida=document.form.answer[i].value;
      }
    }
    location.href="trivia1.php?idquestion="+idq+"&answer="+ida;
  }
  else if (myTime > 0) { 
    myTime--;
    setTimeout("countDown()",1000);
  }
}
countDown();
</script>

do it with AJAX :)

I wood like to do it with ajax but i have never look to ajax and i need hury with this....

Isn't there a meta tag that force your page to refresh every 'n' seconds? Cant remember the syntax right now (google it)
Then when the page load, read the database....?
But then, these mates know more than I do, and would have suggested it if it could work, seeing it's so simple

Yes you are quite right

you just stick something like

<meta http-equiv="refresh" content="600">
in the head section of your html document. The thing is that it is not good to take over the refresh control of a users browser because it can really get on your nerves, especially if you trying to read something.

Also you have to think about how people use the web. Most people don't stay on one page and just keep reloading it, often people browse lots of pages and just keep coming back to the homepage, to look for new news or you can put the new box on every page,that way when ever the user changes pages the script will automatically check for new news items.

Speaking from experiance I think that you are getting into a situation of over development where while you are testing the script is really tedius, so you are trying to create what of automating it but in real life you just end up taking the automation that you have spend hours out because it is just not practical or useful.

(My 2 cents worth)

do it with AJAX :)

easiest way, and you don't need to refresh any part of the page either...

using javascript, you can use a div or iframe & set a timer to refresh that part of the screen at x amount of mins/hours.

easiest way, and you don't need to refresh any part of the page either...

using javascript, you can use a div or iframe & set a timer to refresh that part of the screen at x amount of mins/hours.

I think that if you are in a hurry this recommendation will help you a lot, simply include an iframe with the refresh meta tag in your main document, that way only that part of your page will be reloaded x seconds.
Of course, that iframe must include all the code to connect, retrieve the data and disconect to your database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.