Forum Moderators: coopster
<?php
/**
* This function is just to aid with debugging. It will display the contents
* of an array in an easily readable fashion. You might want to keep it in
* another global file somewhere for reusability. It is used in the script
* below to show you what is happening with the arrays.
*/
function debug($var) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
//*********************************************
// Check login
session_start();
$username = isset($_SESSION['user'] ? $_SESSION['user'] : 'Log in';
// CONNECT TO DATABASE HERE
// mysql_connect(...)
//*********************************************
// Process the form
if (isset($_POST['update'])) {
// (DEBUG) Show the information that was posted by the form
echo '<p>The order IDs posted from the form are:</p>';
debug($_POST['orderid']);
echo '<p>The statuses posted from the form are:</p>';
debug($_POST['status']);
// Combine posted order IDs and statuses into a nice array to work with
$statuses = array_combine($_POST['orderid'], $_POST['status']);
// (DEBUG) See what the combined information looks like
echo '<p>The combined array data is:</p>';
debug($statuses);
// Loop through our array
foreach ($statuses as $orderID => $status) {
// If the status has changed from 'processed', update the order
// otherwise we don't need to bother.
if ($status != 'processed') {
$query = "UPDATE orders SET status = '$status' WHERE orderid = '$orderID'";
$result = mysql_query ($query) or die (mysql_error());
// (DEBUG) See which rows updated
echo "<p>Order '$orderid' status changed from <i>processed</i> to <i>$status</i>.</p>";
}
}
}
//*********************************************
// Get all orders with a status of 'processed'
$query = "SELECT * FROM `orders` WHERE `username` = '$username' AND `status` = 'processed' ORDER BY orderid ASC LIMIT 100";
$orders = mysql_query($query) or die(mysql_error());
?>
<head></head>
<body>
<form method="post" action="expectorders.php">
<?php while ($row = mysql_fetch_assoc($orders)) : ?>
<h3><?php echo $row['orderid']; ?></h3>
<select name="status[]">
<option value="expected">Expected</option>
<option value="late">Late</option>
<option value="processed" selected="selected">Processed</option>
<option value="quarantined">Quarantined</option>
</select>
<input type="hidden" name="orderid[]" value="<?php echo $row['orderid'] ?>" />
<hr />
<?php endwhile; ?>
<input type="submit" name="update" value="Update" />
</form>
</body>
</html>