PHinett Posted June 10, 2009 Share Posted June 10, 2009 Hi all... I am an experienced Asp.Net developer, but I need to convert a PHP script to ASP.net. I'm not looking for someone to do the whole conversion, i just need some help understanding what it's actually doing. $items .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; $_SESSION['chatHistory'][$chat['from']] .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { if (!isset($_SESSION['tsChatBoxes'][$chatbox])) { $now = time()-strtotime($time); $time = date('g:iA M dS', strtotime($time)); $message = "Sent at $time"; if ($now > 180) { $items .= <<<EOD { "s": "2", "f": "$chatbox", "m": "{$message}" }, EOD; if (!isset($_SESSION['chatHistory'][$chatbox])) { $_SESSION['chatHistory'][$chatbox] = ''; } $_SESSION['chatHistory'][$chatbox] .= <<<EOD { "s": "2", "f": "$chatbox", "m": "{$message}" }, EOD; $_SESSION['tsChatBoxes'][$chatbox] = 1; } } } } Thank you very much in advanced i really appreciate your help! Paul Hinett House-Mixes.com Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted June 10, 2009 Share Posted June 10, 2009 $items .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; This is assembling JSON and adding it to the content already stored in "items". The $chat['from'] and ['message'] are unique variables. $_SESSION['chatHistory'][$chat['from']] .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; This is storing the JSON created in the previous example into the global array of $_SESSION I'll come back alter and finish the rest...i have to be at work in 19 minutes Quote Link to comment Share on other sites More sharing options...
PHinett Posted June 10, 2009 Author Share Posted June 10, 2009 Thank you for that...i understand the string building for JSON now. However, i'm not sure on the last piece of code...the part i am struggling with is the foreach part: foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { } If you are interested the full script is here which i am trying to convert: <?php /* Copyright (c) 2009 Anant Garg (anantgarg.com | inscripts.com) This script may be used for non-commercial purposes only. For any commercial purposes, please contact the author at anant.garg@inscripts.com THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ define ('DBPATH','localhost'); define ('DBUSER','root'); define ('DBPASS','password'); define ('DBNAME','chat'); session_start(); global $dbh; $dbh = mysql_connect(DBPATH,DBUSER,DBPASS); mysql_selectdb(DBNAME,$dbh); if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } if ($_GET['action'] == "sendchat") { sendChat(); } if ($_GET['action'] == "closechat") { closeChat(); } if ($_GET['action'] == "startchatsession") { startChatSession(); } if (!isset($_SESSION['chatHistory'])) { $_SESSION['chatHistory'] = array(); } if (!isset($_SESSION['openChatBoxes'])) { $_SESSION['openChatBoxes'] = array(); } function chatHeartbeat() { $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC"; $query = mysql_query($sql); $items = ''; $chatBoxes = array(); while ($chat = mysql_fetch_array($query)) { if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) { $items = $_SESSION['chatHistory'][$chat['from']]; } $chat['message'] = sanitize($chat['message']); $items .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; if (!isset($_SESSION['chatHistory'][$chat['from']])) { $_SESSION['chatHistory'][$chat['from']] = ''; } $_SESSION['chatHistory'][$chat['from']] .= <<<EOD { "s": "0", "f": "{$chat['from']}", "m": "{$chat['message']}" }, EOD; unset($_SESSION['tsChatBoxes'][$chat['from']]); $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent']; } if (!empty($_SESSION['openChatBoxes'])) { foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { if (!isset($_SESSION['tsChatBoxes'][$chatbox])) { $now = time()-strtotime($time); $time = date('g:iA M dS', strtotime($time)); $message = "Sent at $time"; if ($now > 180) { $items .= <<<EOD { "s": "2", "f": "$chatbox", "m": "{$message}" }, EOD; if (!isset($_SESSION['chatHistory'][$chatbox])) { $_SESSION['chatHistory'][$chatbox] = ''; } $_SESSION['chatHistory'][$chatbox] .= <<<EOD { "s": "2", "f": "$chatbox", "m": "{$message}" }, EOD; $_SESSION['tsChatBoxes'][$chatbox] = 1; } } } } $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0"; $query = mysql_query($sql); if ($items != '') { $items = substr($items, 0, -1); } header('Content-type: application/json'); ?> { "items": [ <?php echo $items;?> ] } <?php exit(0); } function chatBoxSession($chatbox) { $items = ''; if (isset($_SESSION['chatHistory'][$chatbox])) { $items = $_SESSION['chatHistory'][$chatbox]; } return $items; } function startChatSession() { $items = ''; if (!empty($_SESSION['openChatBoxes'])) { foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) { $items .= chatBoxSession($chatbox); } } if ($items != '') { $items = substr($items, 0, -1); } header('Content-type: application/json'); ?> { "username": "<?php echo $_SESSION['username'];?>", "items": [ <?php echo $items;?> ] } <?php exit(0); } function sendChat() { $from = $_SESSION['username']; $to = $_POST['to']; $message = $_POST['message']; $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time()); $messagesan = sanitize($message); if (!isset($_SESSION['chatHistory'][$_POST['to']])) { $_SESSION['chatHistory'][$_POST['to']] = ''; } $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD { "s": "1", "f": "{$to}", "m": "{$messagesan}" }, EOD; unset($_SESSION['tsChatBoxes'][$_POST['to']]); $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())"; $query = mysql_query($sql); echo "1"; exit(0); } function closeChat() { unset($_SESSION['openChatBoxes'][$_POST['chatbox']]); echo "1"; exit(0); } function sanitize($text) { $text = htmlspecialchars($text, ENT_QUOTES); $text = str_replace("\n\r","\n",$text); $text = str_replace("\r\n","\n",$text); $text = str_replace("\n","<br>",$text); return $text; } Thank you once again!! Paul Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 10, 2009 Share Posted June 10, 2009 foreach foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { foreach iterates over the array $_SESSION['openChatBoxes'] and assigns the keys of the array to $chatbox and their values to $time Quote Link to comment 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.