refresh part of page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

refresh part of page

Post by Brad7928 »

Hi, i have this page http://202.45.110.174/social i have it so messages are stored in the database.

i want to have php check the number of rows when the page is loaded, then display the rows in descending order, wait 10 seconds, check to see if the number of rows has changed (if there are new messages) then have it either display a pop-up with javascript or simply refresh the page.

is this even possible?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: refresh part of page

Post by mikemike »

Not with just PHP no. You could just JavaScript to do the counter then have it call a PHP page which checks your database - have the JavaScript check that PHP page's output and then refresh or do the pop-up.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: refresh part of page

Post by Brad7928 »

can't i use

Code: Select all

 
$query=mysql_query("SELCET * FROM messages");
$number_messages=mysql_num_row($query);
 
won't this check the number of rows when the page is loaded? then i'd have it checked again in 10 seconds and store it in $number the see if $number>$number_messages then javascript pop-up else end?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: refresh part of page

Post by mikemike »

Yeah, that's qhat I was saying - but you couldn't do everything in JUST PHP, it'd have to be a mixute of PHP and JavaScript.

PHP for:
- Grabbing db values
- Checking values

JavaScript for:
- 10 second countdown
- Calling PHP pages (AJAX)
- Refreshing the page
- Openning pop-up

You may find using a meta refresh useful: link.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: refresh part of page

Post by Brad7928 »

Ok, so now that were both on the same wave length... to start coding the php section of this? i was hunting around and found this script:

Code: Select all

<script type="text/javascript">
time = 15;
function nullcounter() { 
if (time == 0) { window.location='index.php'; }
else { document.getElementById('****su').innerHTML='<font face="Hellvetica" size="16px">You will be redirected in ' + time + ' seconds. Later.</font>'; 
setTimeout('nullcounter()',1000);
}
time--;
}
</script>
i'm looking into modding it for this?

so when the timer gets down to 0 we would need this php:

Code: Select all

$query=mysql_query("SELCET * FROM messages");
$number_messages=mysql_num_row($query);
(i'm only a newbie)
an if statement saying if $number_messages>$number_start {
refresh using java script?}
else
start countdown again (set time ==10)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: refresh part of page

Post by mikemike »

I'm gettng the impression this is for some sort of inbox system that automatically updates, but you only want it to update if there are messages?

I suggest you do your timer and Google for a simple php ajax tutorial. You can then create a PHP page that displays all messages after a given ID. Using AJAX (you'll learn howto in the tutorial) you can then post a request to the PHP page and give it the latest message ID. If the PHP page returns something then you can print it on the page using JavaScript. This way the user will never have to have their page refreshed.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: refresh part of page

Post by Brad7928 »

but then i'd need to loop the ajax request... so i'm still in the same boat... but i like the idea of not having to refresh the page!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: refresh part of page

Post by mikemike »

Yep you're right - but it's quite easy to do, especially once you know how to make a simple AJAX call to a PHP page, and you have the timer too which is one of the harder parts.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: refresh part of page

Post by Brad7928 »

can the timer be modded to use the ajax though? i will google ajax now...
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: refresh part of page

Post by mikemike »

Yes, very easily. AJAX is Asynchronous Javascript and XML. Its basically the method of using JS to call PHP scripts and gather their output. Perfect for you in this situation.
Brad7928
Forum Commoner
Posts: 39
Joined: Thu Jan 29, 2009 4:54 pm

Re: refresh part of page

Post by Brad7928 »

Ok... i've read through the tutorial on w3schools, i see how it works, but i don't get how to adapt it for this situation, are you able to help me code it?
Post Reply