function run_query($sql) {
$mysqli = new mysqli('localhost', 'sienasel_p_user', 'pages2018', 'sienasel_pages');
if ($mysqli->connect_errno) {
die('Failed to connect to MySQL: '.$mysqli->connect_error);
}
$result = $mysqli->query($sql);
if (!$result) {
die('Error executing query: '.$mysqli->error.'
SQL: '.$sql);
}
return $result;
}
function echo_head($title) {
echo '
'.$title.'
';
}
function echo_foot() {
echo '
';
}
function echo_content($page) {
$result = run_query("SELECT title, content, parent FROM pages WHERE pageid = '".$page."'");
$content = $result->fetch_assoc();
echo '
';
}
function echo_nav_start($short_title) {
echo '
';
}
function echo_nav_item($pageid, $title, $current_page) {
if ($current_page == $pageid) $active = 'active';
else $active = '';
echo '
'.$title.'
';
}
function echo_nav_subitem($pages_level2) {
while ($page_lv2 = $pages_level2->fetch_assoc()) {
$pageid = $page_lv2['pageid'];
$title = $page_lv2['title'];
echo ''.$title.'';
}
}
function echo_nav_submenu($pageid, $title, $pages_level2, $current_page) {
if ($current_page == $pageid) $active = 'active';
else $active = '';
echo '
'.$title.'
';
}
function echo_nav($short_title, $current_page) {
echo_nav_start($short_title);
$pages_level1 = run_query("SELECT pageid, title FROM pages WHERE parent = 'home'");
while ($page_lv1 = $pages_level1->fetch_assoc()) {
$pageid = $page_lv1['pageid'];
$title = $page_lv1['title'];
$pages_level2 = run_query("SELECT pageid, title FROM pages WHERE parent = '".$pageid."'");
if ($pages_level2->num_rows === 0) {
echo_nav_item($pageid, $title, $current_page);
}
else {
echo_nav_submenu($pageid, $title, $pages_level2, $current_page);
}
}
echo_nav_end();
}
if ($_GET['pageid'] == null)
$page = 'home';
else
$page = $_GET['pageid'];
echo_head('Countries of the World');
echo_nav('Countries', $page);
echo_content($page);
echo_foot();
?>