hrpeanut Posted July 26, 2008 Share Posted July 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/ Share on other sites More sharing options...
MasterACE14 Posted July 27, 2008 Share Posted July 27, 2008 you could just use PHP and use the sleep() function. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600582 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 MasterACE, not really. You'd use AJAX for this. It would be set to a timer and then call the AJAX function, which queries a PHP page for the new shoutbox data. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600586 Share on other sites More sharing options...
aim25 Posted July 27, 2008 Share Posted July 27, 2008 I agree with DarkWater, AJAX is the most efficient for this. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600589 Share on other sites More sharing options...
MasterACE14 Posted July 27, 2008 Share Posted July 27, 2008 yeah, I did not think of AJAX :-\ Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600597 Share on other sites More sharing options...
hrpeanut Posted July 27, 2008 Author Share Posted July 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600637 Share on other sites More sharing options...
JasonLewis Posted July 27, 2008 Share Posted July 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600667 Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 Orrrrrrr, you could use AJAX so no refresh is necessary...And plus iframes aren't as cool. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600682 Share on other sites More sharing options...
hrpeanut Posted July 27, 2008 Author Share Posted July 27, 2008 Alrighty I'll do some ajax research, i'll post up some results in a bit. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600768 Share on other sites More sharing options...
hrpeanut Posted July 27, 2008 Author Share Posted July 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-600830 Share on other sites More sharing options...
hrpeanut Posted July 28, 2008 Author Share Posted July 28, 2008 (little bump) Still stumped. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601250 Share on other sites More sharing options...
Pyro Posted July 28, 2008 Share Posted July 28, 2008 i would o fthought a simple javascript timer, and for the php, so when the timer ends, it activates or what ever yuou want it to. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601251 Share on other sites More sharing options...
jonsjava Posted July 28, 2008 Share Posted July 28, 2008 a simple php version: <?php $time = date("U"); $finish_time = $time + 10; while ($time < $finish_time){ //do your thing. } Now, with this version, the screen stays blank until it is complete. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601282 Share on other sites More sharing options...
hrpeanut Posted July 28, 2008 Author Share Posted July 28, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601631 Share on other sites More sharing options...
hrpeanut Posted July 28, 2008 Author Share Posted July 28, 2008 oh, ert. I see in my code I have "count" in the conditional, not "$count". Not sure if thats what caused it, but I'll find out after work today. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601666 Share on other sites More sharing options...
ionik Posted July 28, 2008 Share Posted July 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601702 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601735 Share on other sites More sharing options...
hrpeanut Posted July 28, 2008 Author Share Posted July 28, 2008 Oh, i was unaware of the "limit" in SQL. That should clean up the code a bit, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601746 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 let us know if it also solved your main problem along with cleaning of code Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-601749 Share on other sites More sharing options...
hrpeanut Posted July 28, 2008 Author Share Posted July 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-602056 Share on other sites More sharing options...
hrpeanut Posted July 29, 2008 Author Share Posted July 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-602198 Share on other sites More sharing options...
hrpeanut Posted July 29, 2008 Author Share Posted July 29, 2008 Me and a friend got it ALL figured out. Thanks for all the help guys. Its great, a box that automatically refreshes without refreshing the page. Sweet stuff. Quote Link to comment https://forums.phpfreaks.com/topic/116770-running-a-php-script-on-an-loop-of-10-seconds/#findComment-602250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.