只打印一次IF语句
问题描述:
下面是一个在我的电子商务网站(原型)中用于写入客户选择的项目并将其选择存储在平面文件数据库中的php文件。虽然echo "Order Submitted!;
是针对每个选定的项目打印的,但逻辑工作正常。如果选择4个项目,则此行会打印4次,我只需要打印一次。任何想法如何实现?只打印一次IF语句
<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">
如果2项选择的输出是:
事务已完成 单被提交! 已提交订单!
答
记录是否有错误并移动循环外的其他部分。
$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);
}
虽然,你有它编写的方式,你甚至不需要有条件周围“订单提交”,因为如果有一个错误,将不会执行。 (由于退出)。
如果它没有更改,也可以将$ myfile移出循环。
第二个版本:
$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);
答
前foreach循环:
$printed=false;
然后代替
else {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
使用
else {
if(!$printed)
{
echo "<p>Order Submitted!</p>";
$printed=true;
}
fclose($fh);
}
答
您应该删除从else
分支echo
,并把它放在第一foreach
右括号之后
答
您可以使用一个计数器,看看更多的一个提交
,如:
$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>";
* “平面文件数据库”* ...有使用平面文件数据库的电子商务网站? – Kermit 2013-03-20 17:32:34
这是一个原型 – user2170008 2013-03-20 17:33:10
亲类型?...... – brbcoding 2013-03-20 17:33:33