<?php
if ( $_GET['order'] ){
  $order = $_GET['order'];
}
if ( $_GET['name'] ){
  $name = $_GET['name'];
}
if ($_GET['phone']){
  $phone = trim($_GET['phone'], "-/ ");
}

require_once("dt_sql_config.php"); // database connection config.

$list = array();
$x = 0;

$sql = "SELECT order_number, CONCAT_WS(' ', first_name, last_name) AS name, CONCAT_WS(' ', address1, city, state) AS address, CONCAT_WS(' or ', phone1, phone2) as phones, request_date, status, amount, pieces
        FROM dispatch.orders";
if($_GET['order']){
  $sql .= " WHERE `order_number` LIKE '%".$order."%'";
}
if($_GET['name']){
  $sql .= " WHERE `last_name` LIKE '%".$name."%'";
}
if($_GET['phone']){
  $sql .= " WHERE (phone1 LIKE '%".$phone."%' or phone2 like '%".$phone."%')";
}


$result = mysqli_query($db1, $sql);
while($row = mysqli_fetch_assoc($result)) {
  $ord = $row["order_number"];
  $name = $row["name"];
  $date = $row["request_date"];

  $list[$x][0] = $ord;
  $list[$x][1] = $name;
  $list[$x][2] = $date;
  $x++;
}

$y = 0;
echo "
<table id='ordertbl' class='table table-condensed table-hover'>
  <th>Order ID</th><th>Customer Name</th><th>Date</th>
  ";
foreach ($list as $key) {
  echo "<tr>
          <td>".$key[0]."</td>
          <td>".$key[1]."</td>
          <td>".$key[2]."</td>
        </tr>";
  $y++;
}
echo "
    </table>
    ";
?>
