只打印IF语句一次

时间:2013-03-20 17:30:59

标签: php javascript html

下面是我的电子商务网站(原型)中使用的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 个答案:

答案 0 :(得分:1)

跟踪是否存在错误并将其他方式移出循环。

$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);

答案 1 :(得分:0)

在foreach循环之前

$printed=false;

然后代替

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

使用

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

答案 2 :(得分:0)

您应该从echo分支中移除else并将其放在第一个foreach结束括号

之后

答案 3 :(得分: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>";