Printing Only Once IF Statement

santtos

New Member
Below is a php file used within my e-commerce website (prototype) to write the item(s) selected by a customer and storing their choices in a flat file database. The logic works fine although the \[code\]echo "Order Submitted!;\[/code\] is printed for every item selected e.g. if 4 items are selected this line is printed 4 times, I only require it to be printed once. Any idea how this could be accomplished? \[code\]<body> <table> <?php if (!($data = http://stackoverflow.com/questions/15530677/file('items.txt'))) { echo 'ERROR: Failed to open file! </body></html>'; exit; } else { echo "<h1>Transaction Completed!</h1>"; } date_default_timezone_set('Europe/London'); $now = date(' d/m/y H:i:s '); $visitor = $_POST['visitor']; foreach ($_POST as $varname => $varvalue) { foreach ($data as $thedata) { list($partno, $name, $description, $price, $image) = explode('|', $thedata); if ($partno == $varname) { $myFile = "purchases.txt"; $fh = fopen($myFile, 'a') or die("ERROR: Failed to open purchases file!\n"); $content = $now . "|" . $visitor . "|" . $partno . "|" . $name . "|" . $price . "\n"; if (!(fwrite($fh, $content))) { echo "<p>ERROR: Cannot write to file($myFile)\n</p>"; exit; } else { echo "<p>Order Submitted!</p>"; fclose($fh); } } } } ?> </table> <input type="button" onClick="parent.location='home.php'" value="http://stackoverflow.com/questions/15530677/Return Home"> <input type="button" onClick="parent.location='items.php'" value="http://stackoverflow.com/questions/15530677/New Purchase">\[/code\]If 2 items are selected the output is:Transaction CompletedOrder Submitted!Order Submitted!
 
Back
Top