L
L
lol_vova2015-08-12 22:35:27
PHP
lol_vova, 2015-08-12 22:35:27

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();

  }
?>

Index file:
<?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 "Страница не найдена!";
  }

?>

And the file itself, where the menu is located, links are generated in it:
<?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>";

?>

The links are generated and seem to be correct, but at the rate it is necessary to add the "content" field to the database in the menu table and the script will select it from it and display it on the page using the generated link. According to the course, all menu pages are of the same type, but I need different data and layout for each page. Including pages, I think, is wrong ... Googled a lot, but I could not find an answer to this question. There are ideas to change the code of the func class itself and, in accordance with it, somehow display a page with a menu, but I doubt that it will work.
Here is the menu class code:
<?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 "Страница не найдена!";
      }
    }

  }

?>

Maybe pass "$Page = new $page;" to the index file through the return construction?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
heartdevil, 2015-08-13
@lol_vova

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 question

Ask a Question

731 491 924 answers to any question