Reputation: 11
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 echo "Order Submitted!;
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?
<body>
<table>
<?php
if (!($data = 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="Return Home">
<input type="button" onClick="parent.location='items.php'" value="New Purchase">
If 2 items are selected the output is:
Transaction Completed Order Submitted! Order Submitted!
Upvotes: 0
Views: 166
Reputation: 782
You can use a counter and see if more the one was submitted
like:
$count=0;
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 {
$count++;
fclose($fh);
}
}
}
}
if ($count>0)
echo "<p>Order Submitted! $count Times</p>";
Upvotes: 0
Reputation: 2412
You should remove the echo
from the else
branch and put it right after the first foreach
closing bracket
Upvotes: 0
Reputation: 4461
before foreach loop:
$printed=false;
then instead of
else {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
use
else {
if(!$printed)
{
echo "<p>Order Submitted!</p>";
$printed=true;
}
fclose($fh);
}
Upvotes: 0
Reputation: 5428
Keep track of if there's an error and move the else outside the loop.
$error=false;
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>";
$error=true;
exit;
}
}
}
}
if(!$error) {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
Although, the way you have it written, you don't even need a conditional surrounding "Order submitted" because it will never execute if there's an error. (Due to the exit.)
Also you can move $myfile out of the loop if it doesn't change.
Second version:
$myFile = "purchases.txt";
foreach ($_POST as $varname => $varvalue) {
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
if ($partno == $varname) {
$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;
}
}
}
}
echo "<p>Order Submitted!</p>";
fclose($fh);
Upvotes: 1