从implode将数据分配为变量

时间:2015-11-03 10:50:57

标签: php

我正在尝试为我正在创建的小型购物车创建一个“Mark as Dispatched”工具作为教育项目的一部分。

我的第一页允许您勾选两个框,一个用于获取订单ID,第二个用于检索客户的电子邮件。第二页包含以下代码:(请参阅所附代码的附件图片)。

$orders = implode(",", $_POST['chkBoxID']);
$emails = implode(",", $_POST['chkBoxEmails']);

echo $orders;
echo $emails;

如果我的数据库中有两个订单,它目前会回复这样的内容......

1,2,email1@test.com,email2@test.com

如何将订单ID 1分配给email1@test.com,将订单ID 2分配给email2@test.com?

我这样做的原因是我可以在$ to =“”字段中发送包含电子邮件列表的电子邮件,并附上分配给电子邮件的订单ID,以便向客户显示哪个订单有被派遣了。

如果这令人困惑 - (对不起),我附上了一张图片,以帮助解释我在做什么。

Example of code

完整代码:

标记订单dispatched.php

<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
    <?php
        foreach ($orders as $row) {
            echo "<tr class='even'>";
            echo "<td>";
            echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
            echo "</td>";           
            echo "<td>";
            echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
            echo "</td>";
            echo "<td>";
            echo "<strong>Username:</strong> <input type='text' name='Username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
            echo "</td>";
            echo "<td>";
            echo '<input type="checkbox" name="chkBoxID[]" id="chkBox" value="'. $row['OrderID'] .'"><input type="checkbox" name="chkBoxEmails[]" id="chkBox" value="'. $row['Username'] .'">';
            echo "</td>";
            echo "</tr>";
        }
    ?>

    <span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',true)">CHECK ALL</a></span>
    <span class="tag"><a href="javascript:checkall('markAsDispatched','chkBox',false)">UNCHECK ALL</a></span>
    <input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>

更新到dispatched.php

<?php
    include_once("../../config.php");

    try {
        $status = "Dispatched";
        $orders = implode(",", $_POST['chkBoxID']);
        $emails = implode(",", $_POST['chkBoxEmails']);
        $info = array_combine($orders, $emails);

        $stmt = "UPDATE orders SET Status = :status WHERE OrderID IN (:orders)";
        $stmt = $conn->prepare($stmt);
        $stmt->bindParam(':status', $status);
        $stmt->bindParam(':orders', $orders);
        $stmt->execute();

        foreach($info as $orderId => $email) {
            $to = "". $email ."";
            $subject = "Your order has been dispatched";
            $message = "Thank you for your purchase!\n\nYour Order (Order ID: ". $orderId .") has been dispatched.\n\nIf you have any queries regarding your order, please reply to this email or use the Live Support system available from our website.";
            $headers .= "From: REMOVED";
            mail($to, $subject, $message, $headers);        
        }

        header('Location: REMOVED');
    }

    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }

    $conn = null;           
?>

3 个答案:

答案 0 :(得分:1)

试试这个:

$info = array_combine( $_POST['chkBoxID'],$_POST['chkBoxEmails']);
foreach($info as $orderID => $orderEmail){
    //Your orderID and orderEmail
}

因此,您将获得orderID和适当的orderEmail

@Justas

略有变化

答案 1 :(得分:0)

使用array_combine$orders$emails合并为一个数组:

$info = array_combine($orders, $emails);

然后在foreach()数组上使用$info并执行所需的代码以便通过电子邮件发送,更新数据库中的条目等。

foreach($info as $orderId => $email) {
    // In each loop $orderId is the id of the order and $email is the email corresponding to the order.
    // Do stuff...
}

答案 2 :(得分:0)

一个可行的解决方案,而不是

$stmt = "UPDATE orders SET Status = :status WHERE OrderID IN (:orders)";
$stmt = $conn->prepare($stmt);
$stmt->bindParam(':status', $status);
$stmt->bindParam(':orders', $orders);
$stmt->execute();

foreach($info as $orderId => $email) {
   ...     
}

将是这样的:

$status = "Dispatched";
$orders = implode(",", $_POST['chkBoxID']);

 // update lines
 // ...
 // end update lines

$stmt = "select OrderID, Email from [Order] WHERE OrderID IN (:orders)";
$stmt = $conn->prepare($stmt);
$stmt->bindParam(':orders', $orders);
$result = $stmt->...; // get as array of objects

foreach($result as $order) {
    $to = "". $order->email ."";
    $subject = "Your order has been dispatched";
    $message = "Thank you for your purchase!\n\nYour Order (Order ID: ". $order->OrderId .") has been dispatched.\n\nIf you have any queries regarding your order, please reply to this email or use the Live Support system available from our website.";
    $headers .= "From: REMOVED";
    mail($to, $subject, $message, $headers);        
}