PHP - Real Time Chat Databse Output Update?
I'm working on a chat using basic textarea input, post, but the problem that I'm facing is, if a person doesn't send something, then the output is not updated eg. other messages from other users updating in real time.
How is that achieved when you see new database entries pop up live, milliseconds after they are entered?
Do I set a constant refresh rate of some sort?
How do you update a section of a webpage without "refreshing" the entire page like the POST method?
Similar TutorialsI'm not looking for code, mostly just some direction, but I'm sure I'll be back as I try to shape the code. Also, I know very little about coding jQuery, Ajax, etc. Here is what I'm looking for: Basketball coach enters a Page that only he (or admin) can see. (I have that part figured out.) Once there, he has a form to fill out with a player's name and other information. I'd rather there not be 12 rows of empty cells. (Coaches will view that as work) I'd rather not have the Page reload on each "Add", but that would be the lesser of two evils. Is there a way to keep a persistent connection so when a coach hits Add, the player gets added to the database, a new empty form shows up, and the player's information shows up below on a roster? I am building a new web app and want to make sure I do everything right from the beginning. What is the best method of storing the date and time into a database using PHP along with the table field settings? Hey there! So, I am working trying to get a page to edit and update field to insert back into the DB. But for some odd reason, the page just shows up vlank. I've added it to show errors, but still nothing is coming back, so I have no idea what the issue is. Not too sure if there is a config issue with PHP on my server or what. Here's my code: Code: [Select] <?php ini_set('display_errors', 1); //open DB connection include '../dbconn.php'; if ($_POST) { $msg=""; //build an update query $update = "update testspaces set status='$_POST[status]',spacenumber='$_POST[spacenumber]' where id='$_POST[id]'"; //execute query and check to see if it went through if (!mysql_query($update)) { $msg = "Error Updating User"; print $update; } else { $msg = "Record Updated Successfully"; echo "<br>"; echo "<br>"; echo "<br>"; echo "<div class='header2'><a href='adminindex.php'>Go Back To Edit</a></div>"; //print $update; //write a table row confirming the data $table_row = <<<EOR <TR> <td>$status</td> <td>$spacenumber</td> </TR> EOR; } //if not posted, check that an ID has been //passes VIA the URL } else { if (!IsSet($_GET['id'])) { $msg = "No User Selected!"; } else { $id = $_GET['id']; //build and execute da query $select = "select * from testspaces where id=$id"; $result = mysql_query($select); //check to see if the record is there if (mysql_num_rows($result) < 1) { $msg = "No User with that ID is found"; } else { //set the variables for the form code $form_start = "<FORM METHOD=\"post\"ACTION= \"" . $_SERVER['PHP_SELF'] . "\">"; $form_end = ' <TR> <TD COLSPAN="2"><input type="submit" value="Submit Changes" /> <input type="reset" value="Cancel Changes" /></td> </tr> </form> '; //assign the results to an array while ($row = mysql_fetch_array($result)){ $status = $row['status']; $space = $row['spacenumber']; //write out into table row with form fields $table_row = ' <table border="1" align="center" cellpadding="5" cellspacing="0"> <th>Space Number</th> <th>Status</th> </tr> <tr> <td><input type="text" name ="spacenumber" value ="' . $space . '" size="20"/></td> <td><input type="text" name ="status" value ="' . $status . '" size="20"/></td> </tr> <br /> <a href="editemployees.php">Back to Employees</a> '; } // end of while } // end of else num rows } // end of else isset get id } // end of else mysql update //print message echo (IsSet($msg)) ? "<div class=\"error\">$msg</div>" : ""; ?> <? echo (IsSet($form_start)) ? $form_start : ""; ?> <input type ="hidden" name="id" value="<? echo $id ?>" /> <? echo (IsSet($table_row)) ? $table_row : ""; ?> <? echo (IsSet($form_end)) ? $form_end : ""; ?> Not too sure what could be causing to become blank. Ok so I have a question. is it possible to make a form submit to a DB in real time without the need for a submit button ? if so could you drop a hint to what it would be caled or a tut / reference to it ? Hi guys, got some questions about the Instagram real-time api. first let me tell you what I want to do. I'd like to run a daemon process with a socket connection or something of the like to the Instagram API to get a constant feed of photos with a certain tag. We estimate it to be a large amount of data at a particular time (thus why we want to go Real-time). This process will parse the feed and store it into a mongodb database. Secondly, for the front end, I'd like to display all new, live photos in real-time, possible with ajax or some form of checking on a set interval. Problem being, I can't find anyone doing this with php. All of the resources I have seen use Node.js and Tornado. Has anyone done this with PHP or know of a good Real-time API demonstration/tutorial to get me started? Here's the documentation... http://instagr.am/developer/realtime/ any help would be greatly appreciated! thanks! My database name is cute and in cute database contain chatmessages and ipbans tables in which chatmessages contains id(auto increment), name, IP,postime and message and here is the form2.php coding form 2.php Code: [Select] <?php mysql_connect("localhost","root") or die(mysql_error()); mysql_select_db("cute") or die(mysql_error()); $message=$_POST['message']; $n=$_POST['username']; $name=$_POST['name']; if(isset($_POST['submit'])) //if submit button push has been detected { if(strlen($message)>1) { $message=strip_tags($message); $IP=$_SERVER["REMOTE_ADDR"]; //grabs poster's IP $checkforbanned="SELECT IP from ipbans where IP='$IP'"; $checkforbanned2=mysql_query($checkforbanned) or die("Could not check for banned IPS"); if(mysql_num_rows($checkforbanned2)>0) //IP is in the banned list { print "You IP is banned from posting."; } else { $thedate = date("U"); //grab date and time of the post $insertmessage="INSERT into chatmessages (name,IP,postime,message) values('$name','$IP','$thedate','$message')"; mysql_query($insertmessage) or die("Could not insert message"); } } } ?> <html> <head> <script type="text/javascript"> function addsmiley(code) { var pretext = document.smile.message.value; this.code = code; document.smile.message.value = pretext + code; } function a() { var x = document.smile.message.value; if(x=="") { alert("Please insert an message!"); return false; } } </script> <style type="text/css"> body{ background-color: #d8da3d } </style> </head> <body> <form name="smile" method="post" action="form2.php" onSubmit="return a()" > Your message:<br><textarea name='message' cols='40' rows='2'></textarea><br> <img src="smile.gif" alt=":)" onClick="addsmiley(':)')" style="cursor:pointer;border:0" /> <img src="blush.gif" alt=":)" onClick="addsmiley('*blush*')" style="cursor:pointer;border:0" /> <input type="hidden" name="name" value="<?php echo $n;?>"> <input type='submit' name='submit' value='Set Name' class='biasa' ></form> <br> <br> </body> </html> my problem is if the first chat message that i click submit button it would be appear name:message but when the second time submit it only appear the message but the name is blank(nothing and empty) This is my login.php Code: [Select] <?php mysql_connect("localhost","root") or die(mysql_error()); mysql_select_db("Regis") or die(mysql_error()); if (isset($_POST["sub"])) { $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); $_POST['pass'] = addslashes($_POST['pass']); } $usercheck = $_POST["username"]; $check = mysql_query("SELECT username FROM registration WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { echo("<SCRIPT LANGUAGE='JavaScript'>window.alert('Sorry, the username" ." ".$usercheck." ". "is already in use.')</SCRIPT>"); echo ("<SCRIPT LANGUAGE='JavaScript'>setTimeOut(window.location = 'registration.php',1)</script>"); } else if($_POST['username'] && $_POST['pass'] && $_POST['pass2'] ) { $insert = "INSERT INTO registration (username, password) VALUES ('".$_POST['username']."', '".$_POST['pass']."')"; $add_member = mysql_query($insert); echo("<SCRIPT LANGUAGE='JavaScript'>window.alert('Registration had been succesfully added :)')</SCRIPT>"); } } ?> <html> <head> <script type="text/javascript"> function a() { var x = document.login.username.value; var y = document.login.pass.value; if(x==""&& y=="") { alert("Please insert all message!"); return false; } if(x=="") { alert("Please insert an username!"); return false; } if(y=="") { alert("Please insert an password!"); return false; } } </script> </head> <body> <table border="0"> <form name="login" method="post" action="form2.php" onsubmit="return a()"> <tr><td colspan=2><h1>Login</h1></td></tr> <tr><td>Username:</td> <td><input type="text" name="username" maxlength="40"></td></tr> <tr><td>Password:</td> <td><input type="password" name="pass" maxlength="50"></td></tr> <tr><td><input type="submit" name="submit" value="Register"></a></td> <td><input type="submit" name="submit" value="Login"></td></tr> </form> </body></html> So how to make when the second and next submit click button, it would appear name with message ?[/b] Right I want to make a request to my db to check the no. of rows it has. If it has changed, refresh the chat page. else wait. I have tried to check for last db update, that never worked as well. Well here is my code. Code: [Select] //this is my refresh script below $result = mysql_query("SELECT * FROM chatmessages"); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; while(0){ $num_rows_temp = mysql_num_rows($result); if ($num_rows != $num_rows_temp){ $$num_rows=$num_rows_temp; echo "<script>"; echo "window.location.replace('chatlog.php')"; echo "</script>"; }else{ sleep(2); } } I assume $num_rows != $num_rows_temp is wrong as it does not seem to do anything. Any help would be welcome. thanks. Hi, i am thinking of creating a text based game mmorpg using php/mysql etc... I know that a lot of these types of games use a tick based system, but i want it to be realtime, the only game i cna find that uses this is Torn, does anyone know how they make it realtime? I know that ticks can be controlled by cronjobs, but how about realtime? The problem is that the game might involve the player building a house, in a tick based system it could be easy to manage by saying the house will take 3 ticks to complete, but i want the house to be like in 10 minutes of 32 minutes, how can i do this. Someone said to check whenever the player next logs in to check if the house has finished and then say it has been built, but seeing as it is an mmorpg it could affect everyones gameplay, not just the person building it, so this will not work. Thanks and sorry if this is a bit confusing. I wrote a short script that takes a list of keywords and does a Google search to find how often they are used in the title of a page. However, I cannot get it to echo the results in real time. On one server I use it works fine. On the other it runs the entire list and then outputs everything in the end. The problem is sometimes this takes several minutes and therefore it times out after not receiving a response. Any suggestions? Thanks in advance. <?php $file = file('words.txt'); $results = ''; foreach ($file as $line) { $data = file_get_contents("http://www.google.com/search?hl=en&q=allintitle%3A%22" . urlencode($line) . "%22&btnG=Google+Search"); preg_match('/of about <b>([0-9,]*)?<\/b>/si', $data, $number); $results .= $line . ";" . $number[1]; echo $results . "</br>"; $results = ""; sleep(2); } ?> I am trying to simulate an ad expiration and carry out an action if the ad is expired. And I cannot get the if/else to work properly... I've tried many variations and I cannot see what I am doing wrong here. Any tips please 3 hours and counting of no solution! $ad_start = time()-14 . "<br />"; // 14 days from today in the past (negative) echo $ad_start; $current_time = time() . "<br />"; // current epoch time echo $current_time; $days_past = $ad_start - $current_time; // days past echo "<br />$days_past days have past since the ad started!<br />"; if($days_past <= 14) { echo "<br />Ad is less than 14 days. Not expired."; } else { echo "<br />Ad is over 14 days. Expired."; } Hi. How to implement twitter like real-time sharing system? So that is like, if my friend(following person) write a message, it also appear on my wall(page). 1. Is it hard? 2. What language used for it? PHP? 3. Is there any source about it? Thanks. This topic has been moved to Editor Help (Dreamweaver, Zend, etc). http://www.phpfreaks.com/forums/index.php?topic=322054.0 Hi all I am describing my problem below can any body please look into this I have two databases DB1:datain1 DB2:datain2 having the below structure database, table, fields datain1 , student , id,name datain2 , student , id,name the two databases having same structure My query is i want copy all data from datain1 to datain2 on daily process through php script(script will be executed manually on daily) i don't want any duplicate or repeated data in datain2 give me some script to do this kindly help me to solve this its so urgent Hello, I am building an online game(users make a character and move on a map and so on...)
All user data is stored in a mySQL database and I want the users to interact in real-time, but there can be a 1-3 second delay between the communication, but not exceed 3 seconds even if 500 players are playing at the same time.
But for the purpose of the question let's say the users can only chat between one another, if I'll have a solution for that then I can use the same method for more parts of the game.
I can't use websockets because my webhost doesn't support it( I don't want to use pusher.com).
I know I can make real-time apps with ajax long polling, but I think that with 500 players playing at the same time it's not the best solution.
So, finally:
How can I make user interaction as close as possible to a real-time game?
(Without too much load on the hosting server)
(I am sorry if some of my terms are not correct - I am just getting back to coding after a long time...)
Edited by Mythion, 17 August 2014 - 02:34 AM. I have two (2) forms on the same pages, each with 3 buttons. When a button is clicked, it will include the desired page. Since I have 2 of these forms, clicking one of them will refresh the page thus "delete" the info former retrieved from the other form. Is there a way to make the page "remember" the last form action? I am using multiple levels of JSON data coming into php from a C# application, as in:
return new RootObject() { ID_Project = 4, Name_Project = "Test", Receiver_ID = 4, Receiver_Name = "ABCDE", UserID = 20, UserRole = new User_Role() { ID_User = 20, Role_User = "level 3", User_Role_Description = "U", UserGroup = new List<user_group>() { new User_Group() { ID_UserGroup = 30, Name_UserGroup = "usergroup8", UserID = 20 }, new User_Group() { ID_UserGroup = 31, Name_UserGroup = "usergroup9", UserID = 21 }, new User_Group() { ID_UserGroup = 32, Name_UserGroup = "usergroup10", UserID = 22 } } } }; i am having troubles accessing the second level: UserRole and 3rd level: User_Group in my php script. im currently trying like this: foreach ($phpArray as $key => $value) { echo "<h2>$key</h2>"; foreach ($value as $k => $v) { echo "$k | $v "; foreach ($v as $key1) { echo "$key1 "; } } } does anyone have an idea? Thanks, Revathy Hi i need to count the number items in an album now on the album i have create a field called count which counts the number items in that album but now i dnt knw how to update those as soon insert a item in the table item so i thought woul be possible to create a script that when insert data into item tables also update album table row count by adding one like update count with A now A equal row count ++1 any help on start of it if possible???/ hi my db table has 2 field: ID - Place 1 France 2 Germany My form has 2 text field (printed with while cycle). mysql_query("UPDATE mytable SET Place = '" . $_POST['place'] . "' WHERE ID=". $_POST['ID']." "); This query doesn't work, because updates only the last record. Where is wrong? many thanks Ohhh, these thoughts just run through my head as I sleep. But will it work? Can I set a function so that my UPDATE form will NOT execute after a certain DATE (or even a time of day?) I'm thinking something along the lines of: $time=timestamp if $time>19:00 echo" You need to make changes before 7:PM"; else (UPDATE... yadda yadda yadda) Yes, i know this is worse than a rough draft, but will it work? Is it acceptable methodology? I have a form right now that Updates a table on my database. I was wondering if I could possibly still have that working but also Insert the same information into another table on the database when the submit button is pressed? |