PHP To Extract Latest Data From MySQL

PHP Script uses custom CSS to generate HTML styled table to contain last nine database rows from the vehicle speed system.

  • Converts MySQL DateTime entry
    • YYYY/MM/DD H:i:s:xxx to h:i:s
  • Converts Direction Integer to Text
    • 1 = Up, 2=Down

$servername = “xxxxxxx”;
$username = “xxx”;
$password = “xxxxx”;
$dbname = “xxxxx”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$sql = “SELECT * FROM traffic ORDER BY Time DESC LIMIT 9”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo “<table class=styled-table>”;
echo “<thead><tr><th>Time</th><th>Speed</th><th>Direction</th><th>Count</th><th>SD</th></tr></thead>”;
// output data of each row
echo “<tbody>”;
while($row = $result->fetch_assoc()) {

$secvar = “”;
{$secvar = strtotime((trim($row[“Time”])));}
{$secvar2 = date(“H:i:s”, $secvar);}
$firstvar = “”;
if (trim($row[‘Direction’]) ==1)
{$firstvar = “Up”;}
if (trim($row[‘Direction’]) ==2)
{$firstvar = “Down”;}
echo “<tr><td>”.$secvar2.”</td><td>”.$row[“Speed”].”</td><td>”.$firstvar.”</td><td>”.$row[“Count”].”</td><td>”.$row[“SD”].”</td></tr>”;
}
echo “</tbody>”;
echo “</table>”;
} else {
echo “0 results”;
}
$conn->close();

WP Theme Custom CSS

Custom CSS used in Theme for a styled HTML Table

.styled-table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}

.styled-table thead tr {
background-color: #686868;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}

.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}

.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}

.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #686868;
}

.styled-table tbody tr.active-row {
font-weight: bold;
color: #686868;
}