0

下面是我的电子商务网站(原型)中使用的一个 php 文件,用于编写客户选择的项目并将他们的选择存储在平面文件数据库中。尽管为每个选定的项目都打印了逻辑,但逻辑工作正常,echo "Order Submitted!;例如如果选择了 4 个项目,则此行打印 4 次,我只需要打印一次。知道如何实现吗?

<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 个项目,则输出为:

交易完成 订单提交!提交订单!

4

4 回答 4

1

跟踪是否有错误并将 else 移到循环之外。

$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);
于 2013-03-20T17:36:25.680 回答
0

在 foreach 循环之前:

$printed=false;

然后代替

else {
    echo "<p>Order Submitted!</p>";
    fclose($fh);
}

利用

else {
    if(!$printed)
    {
        echo "<p>Order Submitted!</p>";
        $printed=true;
    }
    fclose($fh);
}
于 2013-03-20T17:36:43.590 回答
0

您应该echoelse分支中删除并将其放在第一个右foreach括号之后

于 2013-03-20T17:36:49.710 回答
0

您可以使用计数器并查看是否提交了更多

喜欢:

$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>";
于 2013-03-20T17:38:13.200 回答