Is it a bad idea to store html in a DB so that users can edit it?

I just wanted to know your opinion on how I plan to handle the situation.

Situation: I am developing a school website where strict education regulations will not allow anyone BUT my FTP access to the site. However, the school will have a group of six students per class "web page" to manage 10-12 pages that need to be updated frequently.

All pages look pretty identical: header, navigation, side navigation (for this section (sports, academics, etc.)) and main content .

The main content is served by the div content, so I'm going to just load all the #content content from the Mysql db that has been edited by a different file (I quite promise to use prepared statements).

Do you have any opinions on this or maybe a better method? Thank!

+3


source to share


2 answers


When you do capp markup on the database to get the one you want:

  • Analysis configuration file (if any) and establish connection
  • Query and retrieve data
  • Add results

When you save your markup to a file, all you have to do is include that markup via the language construct require()

EDIT

Be very specific . Not. It is not a bad idea to store dynamic HTML content in a table. This is a common practice . But if you also store static content like header, table navigation for each page, it just leads to data duplication .

I will personally keep the details that I never change in the file. The parts that tend to be "dynamic" I will keep in a table. I would add / edit them via TinyMCE using jquery-ajax.

An example of how I do it:



File: page_view.php

<?php

class Page_View // <- This isn't MVC
{
    private $data = array();

    public function render()
    {
        include('page_template.phtml');
        exit();
    }

    private function getTitle()
    {
       return $this->data['title'];
    }

    private function getBody()
    {
        return $this->data['body'];
    }

    private function getStaticBlock($file)
    {
        include($file);
    }

    public function setBody($body)
    {
       $this->data['body'] = $body;
    }

    public function setTitle($title)
    {
       $this->data['title'] = $title;
    }
}

      

File: page_template.phtml? >

<!DOCTYPE html>
<html>
<head>
<title><?php echo $this->getTitle(); ?></title>

<?php $this->getStaticBlock('/templates/blocks/header.phtml'); ?> 

<body>
 <div id="navigation">
  <?php $this->getStaticBlock('/templates/blocks/navigation.phtml'); ?>
 </div>

   <div id="content"><?php echo $this->getBody(); ?></div>

</body>
</head>
</html>

      

Usage example:

<?php


// $data is what you got from a table


$page = new Page_View();
$page->setTitle($data['title']);
$page->setBody($data['body']);

$page->render();

      

+2


source


Yes, storing arbitrary HTML in a database is a maintenance and security nightmare . I would take a closer look at what you are trying to accomplish before developing a new solution.

I would highly recommend a content management system. They are equipped to handle this kind of work and there is no point in rebuilding it from scratch.

If the selected students will be updating the website frequently, consider separating that portion of the site.



For example, if the main website is static, but the frequently updated parts are a blog, set up a static site and either a subdomain or a subdirectory with a Wordpress block in it.

If students will be editing the actual content of the page, a CMS like Drupal is much more efficient for this task.

+1


source







All Articles