Jump to content

Running a PhP Script on an Loop of 10 Seconds?


hrpeanut

Recommended Posts

Hello!

I'm trying to make a "Shoutbox" for a website.  I'm a bit new to php, so humor me here. 

I figure I'll make a javascript timer and call a php method from that?

 

What I need to work is that anyone can update the MYSQL database with a new record in the shoutbox, and each client will automatically recieve updates on this shoutbox every 10 seconds.

 

Can someone point me in the right direction? A tutorial perhaps?  I'm even having problems using php in javascript.

 

Thanks,

~HrPeanut

Link to comment
Share on other sites

Thanks for the replys everyone.  Could you point me in the direction of starting to get everything set up?  What I know well is java and .net.  I dont know much about this stuff.  :(

I'll start googleing, but not till' monday probably.

thanks again.

Link to comment
Share on other sites

Why do you need to use AJAX? I'm assuming it's being called in an iframe?

If so then you don't need to use AJAX at all.

 

But I would find that frustrating, what if the user is typing a message and then it just refreshed on them?

 

I suppose you could overcome it.

 

All you need to do is use JavaScript and refresh the shoutboxes location every 10 seconds. Should work.

Link to comment
Share on other sites

So heres what I came up with:

 

segment in the html file:


var http = createRequestObject();

	function handleResponse() 
	{

		if(http.readyState == 4)
		{
			var response = http.responseText;
			var update = new Array();

			if(response.indexOf('|' != -1)) 
			{
				update = response.split('|');
				var str = "<p>";
				for (i = 0; i < update.length; i++)
				{
					str = str + update[i] + " - " + update[i+1] + ": " + update[i+2];
					str = str + "<br />";
					i = i + 3;
				}
				str = str + "</p>";
				document.getElementById("shoutdata").innerHTML = str;	
			}
		}
	}

	function createRequestObject() 
	{
		var ro;
		ro = new XMLHttpRequest();
		return ro;
	}

	function sndReq() 
	{
		http.open('get', 'updateshout.php');
		http.onreadystatechange = handleResponse;
		http.send(null);
	}

	function startuptimer()
	{
		//Update the ShoutBox.
		setTimeout(tick(), 5000);
	}
	function tick()
	{
		sndReq();
		setTimeout("tick()", 5000);
	}

	function sendShout()
	{

	}

 

(the body is set onload="startuptimer()")

 

 

the PhP for update: password removed ;)

 

 

<?php

$user = "root";
$pass = "";
$db = "ban";

$select = "SELECT * FROM shoutbox order by date desc"; 

$link = mysql_connect( "localhost:3306", $user, $pass );

if ( ! $link )
	die( "Couldn't connect to the Database" );

mysql_select_db( $db );

$result = mysql_query($select) or die("Unable to select data because: ".mysql_error);

$count = 0;
while ($row = mysql_fetch_row($result) & count < 5)
{
	$name    = $row[0];
	$message = $row[1];
	$date    = $row[2];

	echo "$date|$name|$message|";

	$count = $count + 1;
}

mysql_close( $link );

?>

 

The problem is, that When I send the request out to get the current shoutbox data, It seems to only return empty strings, as if row[0,1,2] were all null.  I checked with the database and it does have some filled sample data.

 

Any ideas?

 

Link to comment
Share on other sites

Oh, I think i need to explain what current stage i'm at. 

 

I got the whole timer thing sorted out, but it's the PhP part thats a little sketchy now.  Currently, it's activating (like it's supposed to) every 5 seconds.  However It's not returning anything to the web page accept " - : ", which is the characters i'm using to delimit the post data.... which can be found in the solution I posted above.  The Mysql database DOES contain 2 rows of sample data, and the php code picks up 2 rows, but it does not pick up the data in the rows.... strange.  Anyone know why?

Link to comment
Share on other sites

what are the fields in the database try using mysql_fetch_assoc and then dumping them out to see what you get

 

and as far as count goes it should throw u an error for a undefined constant...try just going to that page directly and see what you get

Link to comment
Share on other sites

not sure if single & will work

 

while ($row = mysql_fetch_row($result) && count < 5)

 

or instead of using $count to show only 5 results...use limit for query, no need to check the count then...if results are less than 5 still it will work.

 

$select = "SELECT * FROM shoutbox order by date desc limit 5"; 

Link to comment
Share on other sites

thereeeeee we go, yea It's working now. 

Only thing left to get the "shout" button to work - to input into the database, and a little formatting.

 

I'm going to try and get the insert into to work, i'll post up here if i'm getting any troubles.

 

Thanks much guys.

Link to comment
Share on other sites

I have it working pretty well now, however when someone is going to send a new "shout" into the shoutbox, the button makes the browser load up sendShout.php, which is on the server.  This is ok, however, it makes it load up the blank document... is there a way to NOT load up sendShout on the page, and just run it?

 

If that makes any sense...

 

Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.