I am making an ecommerce website. I have the following issue which I have came across while I was testing:
- Updating the order status (In Process, Shipping, Completed and Cancelled) from the admin panel is not working only for 1 product.
- Explode is deleting the last character when values are retrieved from the database.
- If there is more than 1 product in the order, order status works completely fine, though the point 2 remains the same.
So for example: Lets say that in my order (ORD-00001)
,
I have only 1 product (ABC-000001
). So while exploding, I get the value as (ABC-00000
).
In my next order (ORD-00002)
I have 2 product (ABC-000001
, PQR-00005
). So while exploding, I get the value as (ABC-000001
, PQR-0000
).
In the second order, the order status works completely fine. But in the first order, it doesn't work at all.
Here's the php button click event:
<?php
if (isset($_POST["btnUpdateOrderStatus"])) {
$qu = "SELECT * FROM orders WHERE OrderCode = '".$_GET["ord"]."'";
$result = $validate->Query($qu);
if (!$result) {
echo $validate->Error();
exit();
}
while ($rowp = $validate->FetchAllDatas()) {
$p = explode(', ', $rowp["ProdCode"]);
foreach ($_POST["updateOrderPerProductStatus"] as $key => $value) {
//echo $key;
for ($i=0; $i < count($p); $i++) {
if( $p[$i] == $key) {
$querToUpdateOrderStatus = "UPDATE orders SET OrderStatus = '".implode(', ',$_POST["updateOrderPerProductStatus"])."' WHERE OrderCode = '".$_GET["ord"]."' ";
if ($validate->Query($querToUpdateOrderStatus)) {
header("Location: http://www.example.com/nbs/Administrators/Orders/Update.php?ord=".$_SESSION["ord"]."&id=".$_SESSION["id"]);
exit();
} else {
echo $validate->Error();
exit();
}
}
}
}
}
}
?>
Here's the code that I have used:
<?php
$query = "SELECT o.*, com.* FROM orders o, commission com WHERE o.OrderCode = '".$ord."'";
$validate->Query($query);
if ($validate->NumRows() >= 1) {
while ($row = $validate->FetchAllDatas()) {
$products = explode(',', $row['ProdCode']); // <-- error line
$qnty = explode(', ', $row['Quantity']);
$orderStats = explode(', ', $row["OrderStatus"]);
$productsWithQunty = array_combine($products, $qnty);
echo '
<tr>
<td style="width: 50%">Order Code: </td>
<td style="width: 50%">'.$row["OrderCode"].'</td>
</tr>
<tr>
<td style="width: 50%">Order Date: </td>
<td style="width: 50%">'.$row["OrderDate"].'</td>
</tr>
<tr>
<td style="width: 50%">Order Status: </td>
<td style="width: 50%">'.$row["OrderStatus"].'</td>
</tr>
<tr>
<td style="width: 50%">Order Modified: </td>
<td style="width: 50%">'.$row["OrderModified"].'</td>
</tr>
<tr>
<td style="width: 50%">Order Payable: </td>
<td style="width: 50%">'.number_format($row["PayableAmount"], 2).'</td>
</tr>
<tr>
<td style="width: 50%">Products: </td>
<td style="width: 50%">';
foreach ($productsWithQunty as $key => $value) {
echo $key . ":" . $value;
}
echo '</td>
</tr>';
$arrProductStatus = array_combine($products, $orderStats);
?>
<form action="Update.php?ord=<?php echo $ord; ?>" method="POST">
<?php
foreach ($arrProductStatus as $key => $value) {
echo '
<tr>
<td style="width: 50%">'.$key.'</td>
<td style="width: 50%">
<select name="updateOrderPerProductStatus['.$key.']">
<option value="'.$value.'">'.$value.' -- selected</option>
<option value="In Process">In Process</option>
<option value="Cancelled">Cancelled</option>
<option value="Completed">Completed</option>
<option value="Shipping">Shipping</option>
</select>
</td>
</tr>';
}
?>
<tr>
<td colspan="2">
<input type="submit" name="btnUpdateOrderStatus" class="btn btn-primary btn-block" value="Submit">
</td>
</tr>
</form>
<?php
}
}
?>
EDIT 1:
The product array:
For order (ORD-00001)
Array
(
[0] => ABC-000001
)
For order (ORD-00002)
Array
(
[0] => ABC-000001
[1] => PQR-00005
)
EDIT 2:
After RickJames Comment, here's what I get from the actual database.
Code:
echo $row["ProdCode"];
$products = explode(', ', $row['ProdCode']);
print_r($products);
Result for the above code:
DC-0001-0002, PK-0002-000 // The actual product code coming from DB
Array ( [0] => DC-0001-0002 [1] => PK-0002-000 )
And in the database I have:
OrderCode CustomerId ProductCode
ORD-000002 1 DC-0001-0002, PK-0002-0004
Please help me out. Thank you in advance.