PHP - Chat Box Confusion
Hi,
I'm trying to make an area where people can post stuff, status updates etc... I have a small layout done, but something has confused me. I'm trying to get it so when somebody is logged in, and posts something in the chat area, it displays: [Name] [Comment] I know how to read from the database, but for some reason, when I press the 'submit' button, the text in the 'comment' box isn't inserting into my 'comment' column in my database... But I want it so it inserts in the users column. Is this possible? Code: [Select] <?php if($submit) { $comment = $_POST['comment']; $submit = $_POST['submit']; $connect = mysql_connect("****","****","****") or die("Could not connect to database."); mysql_select_db("****") or die ("Could not find database!"); $query("INSERT INTO login ('comment') VALUES('$comment')"); $result = mysql_query($query) or die(mysql_error()); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form method="POST"> Comment: <input type="text" name="comment" /><br /> <input type="submit" name="submit" value="Submit" /><br /> </form> </body> </html> Similar Tutorialsi am new with web socket
and in same time
I try to make server and client communication for soccer manager application, and for this purpose, i need to create closed "chat rooms" or "games" where two users can watch (read game event) and menage (create) game event (make substitution, formation ... or if you want, to send message to server, that they make changes in formation)
this game (room) mast bi secret, and only this two player can use this game for "read" and "write" events in game
for example: Game no: 200001 between user A and user B in 14.05.
when User A, and User B click on game no. 200001 they can read and write game event.
PS: for now, i create basic chat aplication with PHP, jQuery and webSocket and in this app, all user see wath all other user do (public chat) and i want to make secret chat for specific user (already automate subscribed from server) All work with no problem (i work on this example http://www.sanwebe.c...ket-php-socket)
How i can create chat room(s) and subscribe user to specific chat room?
Thanks
Edited by laponac84, 30 November 2014 - 09:09 AM. $sql = "INSERT INTO entries(title, entry) VALUES (?, ?)"; $stmt = $db->prepare($sql); $stmt-> execute(array($title, $entry)); $stmt->closeCursor(); I'm confused but i think i gotta just stop the self doubt -> i'm confused about what these code does ok i know the insert statement $stmt = $db->prepare($sql) = i'm guessing this prepares the $sql variable and prepares it for execution and stores the (?, ?) as an array with ($title, $entry) keys. am i right ? ok so this code here is working find in regards to fetching the users name and ID number however it's just putting them in any order I want the first user to be user 1 and then 2 then so forth? would a simple for loop do this if so before or after my while loop? or would I even need a while loop? I think i have muddled myself up here somewhere along the line I am trying to check to see if various things already exists in a database. So for example i have a collumn with names, second collumn is email address, third collumn phone number etc. So i make the usual connection to the database: $sql=("SELECT * FROM table") $result = mysql_query($sql); $row = mysql_fetch_array($result); Now to assign the various collumns is where i have come unstuck. if (in_array("?", $row)) {do something} or is it if (in_array("?", $row['name'])) {do something} ? I have it working by hard coding to a seperate php file, but its bulky and would prefer to use the database. Alright, hopefully my last question for this current project. I've build an array containing other arrays which each hold 5 values. I'm trying to loop through all the arrays in the main array but only pull out one of the values. What I have right now is: echo "<ol>"; for ($rw = 0; $rw < sizeof($serenwilde); $rw++) { echo "<li><b>The row number $rw</b>"; echo "<ul>"; for ($col = 0; $col < 5; $col++) { if (isset($serenwilde[$rw][$col])) { echo "<li>".$serenwilde[$rw][$col]."</li>"; } } echo "</ul>"; echo "</li>"; } echo "</ol>"; Which produces: Quote The row number 0 value0 value1 value2 value3 value4 How can I modify my code so I still loop through all the arrays, but only pull out say value0 from each of them? Thanks again. Ok I have this form with 6 fields that the user enters a geocaching code. In the database I have a table called cacheinfo. There are 6 enteries in the database in this table. Now Im trying to confirm that each field entered matches the right geo code. I have it working but I feel that its a very bad way of doing it my way. can someone please show me a better way as I have other situations I do something simliar and its just getting silly. Here is the code; if(isset($_POST['nextbtn'])){ $cache1 = trim($_POST['cache1']); $cache2 = trim($_POST['cache2']); $cache3 = trim($_POST['cache3']); $cache4 = trim($_POST['cache4']); $cache5 = trim($_POST['cache5']); $cache6 = trim($_POST['cache6']); $cache1 = mysql_real_escape_string($cache1); $cache2 = mysql_real_escape_string($cache2); $cache3 = mysql_real_escape_string($cache3); $cache4 = mysql_real_escape_string($cache4); $cache5 = mysql_real_escape_string($cache5); $cache6 = mysql_real_escape_string($cache6); $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='1'") or die('can not find cache information'); $cache1 = mysql_fetch_array($FetchCache); if($cache1 !==$cache1['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='2'") or die('can not find cache information'); $cache2 = mysql_fetch_array($FetchCache); if($cache2 !==$cache2['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='3'") or die('can not find cache information'); $cache3 = mysql_fetch_array($FetchCache); if($cache3 !==$cache3['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='4'") or die('can not find cache information'); $cache4 = mysql_fetch_array($FetchCache); if($cache4 !==$cache4['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='5'") or die('can not find cache information'); $cache5 = mysql_fetch_array($FetchCache); if($cache5 !==$cache5['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } $FetchCache = mysql_query("SELECT * FROM cacheInfo WHERE cacheInfoID='6'") or die('can not find cache information'); $cache6 = mysql_fetch_array($FetchCache); if($cache6 !==$cache6['cacheCode']){ $url = "/register-cache.php?error=true"; header("Location: $url"); } // If all match now run the rest of my code...... } I am very confused at an example in a book I am reading called Beggining PHP regarding XML-RPC requests. I will write the code below: function send($remoteServer, $remotePort, $fullXMLRPCRequest) { $headers = ''; $data = ''; $socket = fsockopen($remoteServer, $remotePort); fwrite($socket, $fullXMLRPCRequest); while ($str = trim(fgets($socket))) { // why doesn't this exhaust the returned call?? $headers .= $str . "\n"; } while (!feof($socket)) { $data .= fgets($socket); } fclose($socket); return $data; } The question I have is notice how there are two while statements and they both access the same resource ($socket). So how can there be two while statements?? Wouldn't the first while statement exhaust the resource and there would be nothing on the second while statement? I hope this makes sense. It's sort of like saying while(x < 10) { //execute... ++x; } then another while statement while(x < 10) { //execute... ++x; } Well then wouldn't the second while statement not execute? I hope I made sense. Thanks for all the help in advance. Obviously knowing XML-RPC would help in answering this question, hopefully most of you are familiar with it. why does Code: [Select] while($random_user_TOTAL_COUNT <= 15 && $random_user_INFO = $random_user_USERS->fetch_object()){ work, but Code: [Select] while($random_user_INFO = $random_user_USERS->fetch_object() && $random_user_TOTAL_COUNT <= 15){ doesn't. why??? I'm sure this is something simple that I'm just overlooking right now, but it's got me very frustrated and I just want a quick fix... I have 2 arrays, $data_old and $totals_old Let's say each looks like this (with the 123456 being different for both): Array ( [2014] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) )When I merge the two, I get something like this: Array ( [2014] => Array ( [0] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) [1] => Array ( [60] => Array ( [EMPS] => 1470 ) ) ) )How can I avoid the [0] and [1] and just have those be [123456] and [60], without referencing the 123456 and 60? Hi, I am writing a script for invoicing various contractor jobs. I have fields for date assigned and date completed. Both are timestamp with timezone columns in a postgresql table My goal is to have a cron job which selects the records from the table on the dates the invoices are due and includes the proper jobs-- i.e. the ones that are completed prior to the due date and time(5PM on the first and 15th of each month) That is where I am running into confusion as to how to store the time/tz and then select it in the future so everything is correct This is what I have right now: Code: [Select] $assignmenttype, "miles" => $miles, "notes" => $notes, "dateassigned" => "{$assignmenty1}-{$assignmentm1}-{$assignmentd1} $time1:00 America/Chicago", "datecompleted" => "{$completedassignmenty1}-{$completedassignmentm1}-{$completedd1} $time2:00 America/Chicago"); $res = pg_insert($conn, 'assignments', $invoicearray); hey, I started creating this page that is suppose to upload pdf files, it opens the file upload box but when i click to submit the file nothing happens. I have checked the folder and no file goes in it. I don't know what i am doing wrong. Can anyone help please? Code: [Select] <?php session_start(); if($_SESSION['name']=="hollywincote") { echo "Welcome ".$_SESSION['name']; echo " "; echo "<A href = \"login.php?do=logout\">logout</A>"; } else { header("Location:login.php"); } $page_title= 'Holly Wincote | Login'; include('includes/header.html'); ?> <div class="leftcontent"> <h2>Administrator Panel</h2> <?php //check if the form has been submitted: if(isset($_POST['submitted'])) { //check for an uploaded file: if(isset($_FILES['upload'])) { //validate the type. Should be pdf, doc or rtf. $allowed = array('application/pdf'); if(in_array($_FILES['upload']['type'], $allowed)) { //move the file over. if(move_uploaded_file($_FILES['upload']['name'], "../hollywincote/uploads/{$_FILES['upload']['name']}")) { echo '<p><em>The file has been uploaded</em></p>'; } //end of move... IF } else { echo '<p>Please upload a PDF.</p>'; } } //end of isset($_FILES['upload']) IF. //check for an error: if($_FILES['upload']['error'] >0) { echo '<p>The file could not be uploaded because: <strong>'; //print a message based upon the error. switch ($_FILES['upload']['error']) { case 1: print 'the file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'the file exceeds the MAX_FILE_SIZE setting in the html form.'; break; case 3: print 'the file was only partially uploaded.'; break; case 4: print 'no file was uploaded.'; break; case 6: print 'no temporary folder was available.'; break; case 7: print 'unable to write to the disc.'; break; case 8: print 'file upload stopped.'; break; default: print 'a system error occured.'; break; } //end of switch echo '</strong></p>'; } //end of error IF. //delete the file if it still exists if(file_exists($_FILES['upload']['tmp_name'])) { unlink ($_FILES['upload']['tmp_name']); } }//end of submitted conditional ?> <form enctype="multipart/form-data" action="admin_panel.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="5242488"> <fieldset> <legend>Select a PDF to be uploaded:</legend> <p><b>File:</b> <input type="file" name="upload"/></p> </fieldset> <div align="center"> <input type="submit" name="submitted" value="submit"/> </div> <input type="hidden" name="submitted" value="TRUE"/> </form> </div><div class="clear"></div> </div> <?php include ('includes/footer.html'); ?> thanks Janette After reading the PHP guide that actually seems way more useful then online tutors. Im starting to see why PHP is confusing me now. My way of thinking is way off on how the script reads stuff. when I read this code I think the output should be Bob cause echo reads $foo which equals bob and thats the end of it. but the output is actually My name is bob can someone please help me understand the error of my thinking in how this works. would be most appreciated. Code: [Select] <?php $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &$foo; // Reference $foo via $bar. $bar = "My name is $bar"; // Alter $bar... echo $foo; // $foo is altered too. ?> Hi How can i see which category is being used in rss feed, this is my code to parse one rss feed. $feed_url = "http://www.foo.com/feed/"; $xml = simplexml_load_file($feed_url); foreach ($xml->channel->item as $item) { $ns_content = $item->children('http://purl.org/rss/1.0/modules/content/'); $title = $item->title; // This is title $desc = $ns_content->encoded; // This is description $category =""; // Here i am stuck, how can i get category associated with this feed } How can i see its category, i see in source code it looks like this and even categories have sub categories which looks same, i wants categories names to display wihich is associated with feed: <category><![CDATA[Health]]></category> <category><![CDATA[Men]]></category> thanks for any help. Hi All... I am facing some problem while coding chat system.....my problem is that whenever i refresh the chat page the last message stored in the url by the current user displays again....its showing messages from the other users also but its keep on repeating the messages of the current user logged in,in the browser.....i am using post for all....do i need to try something else or any new way etc....if anybody have suggestions and solutions for the above mentioned problem then please do help me out...thank you... Hi all im trying to build a chat like chathotel.http://chathotel.nl can someone help me out what the best way is to start ? I know php just trying to get it like this doesn’t get me on a start thanks would be great Edited May 29, 2019 by requinixunlinking Hi Guys Somehow I can't make this chat work... maybe you've got an idea what the problem is? main.php <?php session_start(); ?> <html> <head> <title>Chat</title> <script language="JavaScript"> msg = new Array(); function showchat() { for (i = 0; i < msg.length; i++) { show.document.write(msg[i]; show.document.write("<br />"); } show.document.write("<a name='end'></a>"); } </script> </head> <frameset rows="0,*,45" border="0"> <frame name="main" src="main.php"> <frame name="show" src="show.php"> <frame name="send" src="send.php" scrolling="no"> </frameset> </html> <?php session_start(); $time = $_SESSION['time']; if (!isset($time)) $time = 0; $hdlDB = mysql_connect('localhost', 'root', ''); $strSQL = "SELECT * FROM chat WHERE time > '$curTime'"; $dhlRS = mysql_db_query('chat', $strSQL, $hdlDB); while ($arrRS = mysql_fetch_assoc($hdlRS)) { $arrMsg[] = '<b>' . $arrRS['sender'] . '</b>: ' . $arrRS['data']; $time = max($arrRS['time'], $time); } session_register('time'); ?> <html> <head> <title>Chat</title> <script language="JavaScript"> <?php if (is_array($arrMsg)) { $i = 0; foreach($arrMsg as $strLine) { echo "top.msg[$i] = '$strLine';\n"; $I++; } } ?> top.show.location.reload(); </script> </head> <body onload="window.setInterval('location.reload()', 4000)"> </body> </html> send.php <?php session_start(); if (!isset($time)) $time = 0; if (strlen($_POST['newmsg'] > 2) { $hdlDB = mysql_connect('localhost', 'root'; ''); $curTime = time(); $strSQL = "INSERT INTO chat (time, data, sender) VALUES ('$curTime', $newmsg', '$sender')"; mysql_db_query('chat', $strSQL, $hdlDB); session_register('time'); } /* end if */ ?> <html> <head> <title>Chat</title> <script language="JavaScript"> <?php if (is_array($arrMsg)) { foreach($arrMsg as $strLine) echo "top.msg[top.msg.length]=\"$strLine\""; } ?> top.show.location.reload(); </script> </head> <body bgcolor="white"> <form action="<?php echo $PHP_SELF; ?>" method="post"> <select name="sender" size="1"> <option value="Admin" <?php $sender == 'Admin' ? print 'selected' : print ''; ?>>Admin <option value="Client" <?php $sender == 'Client' ? print 'selected' : print ''; ?>>Client </select> 's Beitrag: <input type="Text" name="newmsg" size="40"> <input type="Submit" value="Send"> </form> </body> </html> show.php <html> <head> <title>Chat</title> </head> <body> <script language="JavaScript"> top.showchat(); </script> </body> </html> Database (chat) CREATE TABLE chat ( id bigint(20) NOT NULL auto_increment, time varchar(10) NOT NULL, data text, sender varchar(32) DEFAULT '1' NOT NULL, PRIMARY KEY (id), KEY id (id) ); cheers, ozzo Hi, I'm a PHP beginner using Julie Meloni Sams Teach yourself to help me. My confusion is with this script <?php $file_dir ="/path/to/upload/directory" ; foreach($_FILES as $file_name => $file_array) { echo "path: ".$file_array["tmp_name"]."<br/>\n" ; echo "name: ".$file_array["name"]."<br/>\n" ; echo "type: ".$file_array["type"]."<br/>\n" ; echo "File name is" . " ". $file_name ; } ?> What I don't understand is the foreach statement foreach($_FILES as $file_name => $file_array) $_FILES is an associative array, right? So $filename should represent the key id and $file_array the value. but she is using the $file_array as if it is an array and accessing the values with associative index which confuses. I mean, let's say I declared a normal array. $array = array("Sarah" => "Jane", "John Smith") ; if i wanted to use a foreach loop i would do it like so foreach($array as $fname => $surname) { echo $fname ; echo $surname ; } $fname will print the first name and surname surname. I didn't use an indices to access the values. foreach($_FILES as $file_name => $file_array) can someone explain this to me? Thanks This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=343994.0 I'm fairly new to this and I have looked around but I can't find much information on it. Basically I know what an array and a function is but I'm currently working through an oop login and register sytem and I am seeing this quite a lot:
function myFunction($myVariable = array()){
$Firstvariable = $myVariable[0];
$Secondvariable = $myVariable[1];
//etc
}
This is really confusing me and I can't see any examples in any tutorial sites or books of an array and a function being used like this. Can someone just explain how something like this would work in simple terms? I have also checked the manual but I can't see an example an array being used like this. Any help would be much appreciated. Thanks.
I am not sure how I should go about doing this. I have a server that has a chat server on it, people register an account and then have access to this chat server from their application. To initially connect to it, they need a game id and game stamp (kind of like a username/password for the game). I don't want chats from one application to display in another users application. When I created this chat server it was for another purpose, and could only support about 64,510 different chats since each chat ran on its own port. For my current purpose it is a little different some applications can have 1 - 10 chats going at once per user using the application. Say their is 100 people using the application and each is using 5 - 10 different chat windows. With 100 users on at the same time all chatting, that can get up to 1,000 different ports being used. I don't want that, because say there are 100 applications also using chat, that is about 100,000 different ports, and I am pretty sure you cant have 100,000 ports. So! I need a way to make a chat server, any ideas on how I could do this with my php? need more information? please ask! |