Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting only once IF statement
    text
    copied!<p>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? </p> <pre><code>&lt;body&gt; &lt;table&gt; &lt;?php if (!($data = file('items.txt'))) { echo 'ERROR: Failed to open file! &lt;/body&gt;&lt;/html&gt;'; exit; } else { echo "&lt;h1&gt;Transaction Completed!&lt;/h1&gt;"; } date_default_timezone_set('Europe/London'); $now = date(' d/m/y H:i:s '); $visitor = $_POST['visitor']; foreach ($_POST as $varname =&gt; $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 "&lt;p&gt;ERROR: Cannot write to file($myFile)\n&lt;/p&gt;"; exit; } else { echo "&lt;p&gt;Order Submitted!&lt;/p&gt;"; fclose($fh); } } } } ?&gt; &lt;/table&gt; &lt;input type="button" onClick="parent.location='home.php'" value="Return Home"&gt; &lt;input type="button" onClick="parent.location='items.php'" value="New Purchase"&gt; </code></pre> <p>If 2 items are selected the output is:</p> <p><strong>Transaction Completed Order Submitted! Order Submitted!</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload