create a data table example of menu items and prices from a restaurant in php

To create a data table of menu items and prices from a restaurant in PHP, we need to first create a database to store the information. We can use MySQL as our database management system.

Here's an example of how to create the database and table:

CREATE DATABASE restaurant;

USE restaurant;

CREATE TABLE menu_items (
   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   item_name VARCHAR(50) NOT NULL,
   description VARCHAR(200),
   price DECIMAL(6,2) NOT NULL
);
220 chars
11 lines

After creating the table, we can then insert the menu items and their prices into the table using PHP code. Here's an example:

main.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "restaurant";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO menu_items (item_name, description, price)
VALUES ('Hamburger', 'Juicy beef patty served with fries and a drink', 10.99),
       ('Cheese Pizza', 'Thin crust pizza with mozzarella cheese and tomato sauce', 12.99),
       ('Grilled Chicken Sandwich', 'Savory chicken on a toasted bun served with lettuce, tomato, and mayo', 8.99),
       ('Caesar Salad', 'Fresh romaine lettuce with parmesan cheese, croutons, and Caesar dressing', 6.99)";

if ($conn->query($sql) === TRUE) {
  echo "Menu items inserted successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
914 chars
29 lines

Next, we can display the menu items in a data table using PHP and the DataTables plugin, which allows us to sort and search the data easily. Here's an example:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>Restaurant Menu</title>
   <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.2/css/jquery.dataTables.min.css">
   <script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
   <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.2/js/jquery.dataTables.min.js"></script>
</head>
<body>
   <?php
   $servername = "localhost";
   $username = "username";
   $password = "password";
   $dbname = "restaurant";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);

   // Check connection
   if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
   }

   $sql = "SELECT item_name, description, price FROM menu_items";
   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
     echo "<table id='menu-table'>";
     echo "<thead><tr><th>Item Name</th><th>Description</th><th>Price</th></tr></thead>";
     echo "<tbody>";
     while($row = $result->fetch_assoc()) {
       echo "<tr>";
       echo "<td>".$row["item_name"]."</td>";
       echo "<td>".$row["description"]."</td>";
       echo "<td>".$row["price"]."</td>";
       echo "</tr>";
     }
     echo "</tbody></table>";
   } else {
     echo "0 menu items";
   }

   $conn->close();
   ?>

   <script>
     $(document).ready( function () {
        $('#menu-table').DataTable();
     } );
   </script>
</body>
</html>
1587 chars
55 lines

This will display the menu items in a table with sortable and searchable columns using the DataTables plugin.

gistlibby LogSnag