Dynamic HTML via DB is bad practice in PHP?

1

I'm creating a platform where I need to change elements of the site through a control panel, and for example, would it be something very bad to insert into DB and pull via PHP?

Let's say I have a menu with the following options in my menu :

  

1. Articles

     

2. Matters

     

3. Partners

     

4. Promotions

     

5. Contact

     

6. Submit your Text

Imagine that BD looks like this:

Id  | titulo                                        | status
1   | <li><a href="#">Artigos</a></li>              | 1
2   | <li><a href="#">Matérias</a></li>             | 1
3   | <li><a href="#">Parceiros</a></li>            | 1
4   | <li><a href="#">Promoções</a></li>            | 1
5   | <li><a href="#">Contato</a></li>              | 1
6   | <li><a href="#">Envie seu Texto</a></li>      | 1

Let's say that:

status 0 = oculto
status 1 = ativo

Just to illustrate, the code, for example, would look something like this:

<ul class="menu">
    <?php
    include('includes/conn.php');

    $query      = 'SELECT titulo FROM '.$tabela.' WHERE status = 1';

    if($stmt = $mysqli->prepare($query)){

        $stmt->execute();
        $stmt->bind_result($titulo);

            while($stmt->fetch()){

    ?>

        <?php echo $titulo; ?>
        <!-- COMNENT :: SAÍDA --
        <li><a href="#">titulo</a></li>
        <li><a href="#">Artigos</a></li>
        <li><a href="#">Matérias</a></li>
        <li><a href="#">Parceiros</a></li>
        <li><a href="#">Promoções</a></li>
        <li><a href="#">Contato</a></li>
        <li><a href="#">Envie seu Texto</a></li> <<< ESTE NÃO APARECE
        -->

    <?php
            }
            $stmt->close();
    }

    $mysqli->close();
    ?>
</ul>

Well, I could simply change this in the database and put, for example, the option 6 with status of " hidden " (status = 0) and hide this menu option when needed.

Basically, this is a platform with dynamic elements if used extensively in the project or is it bad practice?

    
asked by anonymous 26.04.2018 / 10:05

2 answers

4

There is not a single way to develop in PHP and we always have to evaluate the context to define the best form of coding. A good practice is to use a software architecture standard that is consistent with what is needed. An example of a default is the Model-view-controller (MVC), used by frameworks like Zend, CodeIgniter, CakePHP, etc.

If this pattern is used, the database will have only the data, not the codes (or notations, as is the case). The view layer will have only the visual part and not the logic to search the DB, deal with and display. The controller that will do this part.

So, in the database the data will be stored in a simple way (which could be used in other parts of the application and not just in the menu):

Id  | titulo               | status      | link
1   | Artigos              | 1           | #
2   | Matérias             | 1           | #
3   | Parceiros            | 1           | #
4   | Promoções            | 1           | #
5   | Contato              | 0           | #
6   | Envie seu Texto      | 1           | #

(A column with the link was included because it would not make sense to have the menu items without a link.)

The controller looks for the DB data and creates an object or array with the ACTIVE menus. In a simple way, it could be a function that would return an array with the menu items.

In the view I would just grab the array and loop to create list items such as:

foreach($array as $key => $value) {
    echo '<li><a href="' . $value['link'] . '">' . $value['titulo'] . '</a></li>';
}
    
26.04.2018 / 11:54
8

I disagree somewhat with the other answer:)

The ideal is to do what you need for the project context, to follow rules imposed by others who do not know the context of that project is bad practice, ie it is bad practice to follow good practices. It is good practice to know good practices, to deeply understand their motivation to apply when appropriate. Many software projects are bad because they follow best practices without understanding what they are doing. And most people do not understand, even though they think they understand.

So I can not say that MVC is suitable. Actually PHP and MVC for me is a bit strange, especially in the way it is implemented. Separating responsibilities is good, but it is necessary to do this, because there will be clear gains without significantly increasing the complexity of the code.

I can not tell if what you're doing is appropriate or not because I do not know your case. Looking over looks like it should not because it has no purpose, no need.

How difficult it is to do:

<li><a href="#"><?php $titulo?></a></li>

and do not have HTML inside the database? For me it's even simpler to do so.It's more DRY . You can compose as you wish.

You could put in the database if the HTML is really a data that varies in each operation. Even this I would say is not the most appropriate, but again, I can not say without knowing the context.

    
26.04.2018 / 15:15