1

I am trying to create a "Mark as Dispatched" tool for a small Shopping Cart I am creating as part of an educational project.

The first page I have allows you to tick two boxes, one to get the Order ID and the second to retrieve the Customer's Email. The second page has the following code: (Please see the attached image for all of the code).

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

echo $orders;
echo $emails;

If I have two orders in my Database it will currently echo something out like this...

1,2,[email protected],[email protected]

How can I assign Order ID 1 to [email protected] and Order ID 2 to [email protected]?

The reason behind me doing this is so I can send an email with a list of emails in the $to = "" field and also attach the Order ID which is assigned to the email in order to show the customer which order has been dispatched.

If this is confusing - (sorry), I have attached an image to help explain what I am trying doing.

Example of code

Full code:

mark-orders-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>

update-to-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;           
?>
6
  • Well you could split the array in half and then do a merge between the values of each array. Commented Nov 3, 2015 at 10:55
  • You'd need to create an' array or something similar in order to keep them connected. Assuming that they always follow each other in order (First order ID in the array is the first email in the email array and so on), you could then use a foreach to do that Commented Nov 3, 2015 at 10:55
  • Post your code here instead of image Commented Nov 3, 2015 at 10:56
  • @VijayaSankarN I have done that for you. Commented Nov 3, 2015 at 11:31
  • @D.Ashbridge is the count of elements in chkBoxEmails and chkBoxID same? Commented Nov 3, 2015 at 11:34

3 Answers 3

1

Try this:

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

So you will get orderID and appropriate orderEmail

It is has slight change to @Justas

Sign up to request clarification or add additional context in comments.

Comments

0

Use array_combine to combine $orders and $emails into one array:

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

Then use foreach() on $info array and execute the needed code for emailing, updating the entry in database, etc.

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...
}

1 Comment

This isn't working. I may be doing something wrong so I have posted my code above.
0

A posible solution instead of

$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) {
   ...     
}

will be something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.