Can I make any further improvements to simplify / speed up this code? My reason for creating new arrays for the variables $name
, $business
, $email
, $orderstatus
is because when displaying results there will be identical order ID numbers stored in $orderid.
My script filters out the duplicates then with the remaining results matches up the keys from $orderid
with the correct key in $name
, $business
, $email
, $orderstatus
<?php
session_start();
if (!isset($_SESSION['username'])){
header("location:login.php");
}
// DB Credentials
$dbusername = 'xxxx';
$dbpassword = 'xxxx';
// Connect to order database
$conn = new PDO('mysql:host=localhost;dbname=xxxx_orders', $dbusername, $dbpassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Select Order IDs from database
try {
$stmt = $conn->prepare('SELECT o.*, c.fname, c.lname, c.business FROM xxxx_orders.orders o, xxxx_customers.customers c WHERE c.email = o.email');
$stmt->execute();
}
catch(PDOException $e) {
echo "I'm sorry, xxxx. I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
// For each result create or add to the arrays $orderid,$name,$business,$email,$orderstatus
foreach ($stmt as $result) {
$orderid[]=$result['orderid'];
$name[]=$result['fname'] . " " . $result['lname'];
$business[]=$result['business'];
$email[]=$result['email'];
$orderstatus[]=$result['orderstatus'];
}
// Filter out duplicate order IDs from $orderid array
$orderid = array_unique($orderid);
// Display results section
foreach ($orderid as $key => $value) {
echo "<tr>";
// Display First/Last name from array $name with $key being equal to the corresponding key from array $orderid.
// The key is necessary to properly display the correct result
echo "<td align=center>" . $name[$key] . "</td>";
// Display business name from array $name with $key being equal to the corresponding key from array $orderid.
// The key is necessary to properly display the correct result
echo "<td align=center>" . $business[$key] . "</td>";
// Display email from array $name with $key being equal to the corresponding key from array $orderid.
// The key is necessary to properly display the correct result
echo "<td align=center>" . $email[$key] . "</td>";
// Display order status from array $name with $key being equal to the corresponding key from array $orderid.
// The key is necessary to properly display the correct result
echo "<td align=center>" . $orderstatus[$key] . "</td>";
// Display First/Last name from array $name with $key being equal to the corresponding key from array $orderid.
// The key is necessary to properly display the correct result
echo "<td align=center><a href=editorder.php?q=" . $orderid[$key] . ">" . $orderid[$key] . "</a></td>";
echo "</tr>";
}
?>
exit
. So you send the data to the unauthorized user and hope the browser redirects quick enough before he sees it? I hope I am missing something not having done php in years. \$\endgroup\$