PHP MYSQL

CREATE A DYNAMIC MENU WITH PHP MYSQL

item_id    | INT(11) | primary key item_text | VARCHAR(255) item_link | VARCHAR(255)

DATABASE MENU TABLE

01

ID | TEXT         | LINK  1  | Home       | /  2  | Blog         | /blog/  3  | Products | /products/

DUMMY MENU ITEMS

02

GET MENU ITEMS (PART A)

03

CONNECT TO DATABASE $pdo = new PDO("mysql:host=HOST; dbname=NAME;charset=utf8", USER, PASSWORD);

GET ALL MENU ITEMS $stmt = $pdo->prepare("SELECT * FROM `menu_items`"); $stmt->execute(); $items = $stmt->fetchAll();

04

GET MENU ITEMS (PART B)

<nav><?php foreach ($items as $i) {   echo "<a href='". $i['item_link'] ."'>";   echo $i['item_text'];   echo "</a>"; } ?></nav>

DRAW THE HTML MENU

05