note 52190 added to control-structures.foreach

From: Date: Sat, 23 Apr 2005 20:10:40 +0000
Subject: note 52190 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-88455@lists.php.net to get a copy of this message
I wrote this code to add each post from a user to every users' txt file. But it only adds the message to the last user in users.txt. The reason of that is...(questmark) <?php session_start(); header("Cach-control:private"); $name=$_SESSION['name']; $nameframe=$name.".txt"; $message=$_POST['message']; $wierdchars = array("\'", "\"", "\\", ":-)", ":-D", ":-p", ":-(", "=p", ">:0", ":-[", ":-/", ":-\\", ":-X", ":-?", "B-)"); $newchars = array (stripslashes("\'"), stripslashes("\""), "\\", "<img src='smile.gif'>", "<img src='opensmile.gif'>", "<img src='tounge.gif'>", "<img src='sad.gif'>", "<img src='tounge.gif'>", "<img src='yelling.gif'>", "<img src='embarrased.gif'>", "<img src='sosoleft.gif'>", "<img src='sosoright.gif'>", "<img src='quiet.gif'>", "<img src='confused.gif'>", "<img src='cool.gif'>"); $newmessage=str_replace($wierdchars, $newchars, $message); $fontcolor=$_POST['fontcolor']; $fontsize=$_POST['fontsize']; $fontface=$_POST['fontface']; $users=file("users.txt"); foreach($users as $user) { $nameframed=$user.".txt"; $thefile=file_get_contents($nameframed); $file=fopen($nameframed, "w+"); $body=$name."|".$newmessage."\n"; $body2=$body.$thefile; $write=fwrite($file,$body2); fclose($file); } echo "<html><head><title>Adding info...</title>"; echo "<script>window.location='frame.php';</script>"; echo "</head>"; echo "<!--Removes ads form page</head><body>"; ?> ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52190 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52190&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52190&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52190&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52190&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52190&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52190&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88455) next »