Answer the question
In order to leave comments, you need to log in
How to correctly generate links for menu items and display pages?
In the course of studying OOP, I began to write a small site for a video course. Over time, there were small problems, but most of all I'm interested in how to correctly display the menu pages. The course offered the abstract class code:
<?php
abstract class func {
public function __construct() {
include "modules/db_connection.php";
}
protected function get_header() {
include "header.php";
}
protected function get_sidebar() {
include "sidebar.php";
}
public function get_body() {
$this->get_header();
$this->get_sidebar();
$this->get_content();
}
abstract function get_content();
}
?>
<?php
session_start();
header("Content-Type:text/html; charset:CP1251");
require_once("classes/func.php");
if(isset($_GET['option'])) {
$class = trim(strip_tags($_GET['option']));
$class = htmlspecialchars($class);
} else {
$class = "main";
}
if(file_exists("classes/".$class.".php")) {
include ("classes/".$class.".php");
if(class_exists($class)) {
$Page = new $class;
$Page->get_body();
} else {
echo "Страница не найдена!";
}
} else {
echo "Страница не найдена!";
}
?>
<?php
$result = @mysql_query('SELECT * FROM `menu` ');
if(!$result) {
exit("Ошибка запроса в БД!");
}
$row = array();
echo "<div class='sidebar f_left'>
<div class='top_bar'>
<div class='nav'>Навигация</div>
<ul>
<li><a href='index.php'>Главная</a></li>";
for($i = 0;$i < mysql_num_rows($result);$i++) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
printf("<li><a href='?option=menu&page=%s'>%s</a></li>", $row['href_menu'], $row['name_menu']);
}
echo "</ul>
</div>
</div>";
?>
<?php
class menu extends func {
public function get_content() {
$page = $_GET['page'];
$page = trim($page);
$page = htmlspecialchars($page);
$page = strip_tags($page);
$query = "SELECT name_menu FROM menu WHERE href_menu = '$page'";
$result = @mysql_query($query);
if(!$result) {
echo "Ошибка запроса в БД!";
}
$row = array();
$row = @mysql_fetch_array($result, MYSQL_ASSOC);
if(!empty($row['name_menu'])) {
include "../www/modules/".$page.".php";
} else {
echo "Страница не найдена!";
}
}
}
?>
Answer the question
In order to leave comments, you need to log in
Hello.
Whatever content you have can be for any page.
A layout is better to fit the same type. But if you need a different layout, then you need to edit the abstract class. You need to open the header and footer there for redefinition. Then in the derived classes you will be able to fully control the layout.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question