Is dynamic HTML via BD bad practice in PHP?

Asked

Viewed 189 times

0

I’m creating a platform where I need to change elements of the site by a control panel, and for example, it would be something very bad way insert into BD 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. Send Your Text

Imagine that in the comic book is 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:

status 0 = oculto
status 1 = ativo

Just to illustrate, the code, for example, would be 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 just change that in the database and put, for example, the option 6 with status of "occult" (status=0) and hide this option from the menu when needed.

Basically, this consists of a platform with dynamic elements if used widely in design or is bad practice?

  • 1

    I think the good practice part, goes from person to person... I personally do like you and I think a lot of projects are programmed this way. What other way to complete this operation?

  • Thanks for the feedback. The other way would be to simply leave these convenience objects aside in PHP and try some javascrpit solution.

  • 2

    If you need to create a screen that manages the options, will it force your user to write all the HTML code of the option? In my view, it makes much more sense for you to just store the information in the bank and build the HTML only when displaying.

  • 2

    Bad practice is not, wordpress does this and is one of the most used frameworks today. From a researcher how he does it, how he keeps it in the comic book, will give you an idea of the best way to save this data.

  • 3

    @Guilhermecostamilam do not think Wordpress is a Frameworks necessarily (you can even categorize as such), but also do not think it is a good example of how to do things, in fact I think it is an example of how not to (at least in most aspects of it).

2 answers

9

I disagree a little on the other answer :)

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

That’s why I can’t say that MVC is appropriate. Actually PHP and MVC for me is a little strange, especially in the way it is implemented. Separating responsibilities is good, but it takes criterion, to do because there will be clear gain without significantly increasing the complexity of the code.

I can’t tell if what you’re doing is appropriate or not because I don’t know your case. Looking up it seems like you shouldn’t because it has no purpose, no need.

How hard it is to do:

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

I put in the Github for future reference.

and not having 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 bank if really HTML is 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.

  • 7

    +1 - This sentence summarizes almost all the problems I see happening with the codes in PHP: "... it is a bad practice to follow good practices, It is good practice to know good practices, deeply understand her motivation to apply when it is the case"

  • I can even agree in part with what you said but to say that "it is a bad practice to follow good practices" is to push TOO hard. No radicalisms, please... ;)

  • 8

    @Fernando is not radicalism, it is the pure truth. So much so that evangelists of good practices never have anything to present of real code. They are very good in empty theory, but in the market, and especially in PHP, "not glue". See projects like Moodle and Wordpress, for example. Very good up to a certain epic as examples, but it’s a maintenance nightmare. The guy has to become an expert just in order to maintain any customization.

  • 5

    And before anyone distorts, it is not a criticism of the functioning of the mentioned platforms. My compliments to both, for the whole philosophy of sharing programming time and donating to the community WELL complete products. The only point at issue here is the idea that "architecture standards" and "good practices" do not work in a real environment as a solution to problems, because in the two cases mentioned, ended up generating unnecessary complexities in the name of a dubious versatility for programmers who arrive later.

  • I really wanted to see applications like Moodle and Wordpress made with no standard code... it would be beautiful! As I said, I agree in part because we really have to take into account the whole context (environment, maturity, complexity, etc.) before defining any architecture. Now, to say that standards are empty theories and that "evangelists of good practices never have anything to present of real code", is rather pure radicalism. We have to evaluate all options and adapt the best to our reality and then move on with it to not have a legacy garbage.

  • 4

    @Fernando who spoke in any pattern? One thing I usually say is that good practice is a mechanism invented with good intention and is great. Their problem is that almost everyone follows them blindly. Most people can’t even interpret the text in it, and they think they just do what someone said and everything looks beautiful. The problem is the attitude of people who radicalize in following something without understanding why they’re doing it, not knowing if they need it. Your answer did the opposite of what you are saying now, which is wrong?

  • @Maniero, my answer was to try to help someone who asked in the community how to do something that for me is very simple and what I thought best was to help indicate a path with explanation and example code. My hope is always to have different visions that show different ways to implement for those who ask the questions in search of some "light", that’s what we’re here for. The problem for me is the use of words that indicate that one should not take too much into account the patterns to those who are lost and seeking some direction and so I quoted the phrases that I do not agree.

  • 1

    The same goes in the opposite direction. Even I only responded by using your words.

  • Actually, reading my reply again may seem like a very directed position even though I said there’s no way to encode, I will edit my answer to avoid controversy... sometimes what is written turns out to be something different than what is and we are here to help people and grow together in knowledge.

  • 5

    @Everson, I’m sorry, but I don’t think you quite understand what MVC is. And even if this were true, in practice this occurs in 0.0001% of cases, that is, complicating something for a tiny possibility of being useful is not a good reason.

  • 2

    @Maniero I meant that MVC is suitable because you have a gain on that front and backend. But not related to the question that theoretically is simple. And in the question of the other answer I agreed on the part Não existe uma só maneira de desenvolver em PHP e temos sempre que avaliar o contexto para definir a melhor forma de codificação. I may not have the notion that you wish I had, but that doesn’t mean I don’t have the right to give my opinion. I may be wrong, but I’m wrong that you learn.

  • 3

    Nobody said you can’t give your opinion, and I don’t want you to have anything, I’m just telling you that MVC is not what you think it is, at least in this context.

Show 7 more comments

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 pattern is the Model-view-controller (MVC), used by frameworks such as Zend, Codeigniter, Cakephp, etc.

If this standard is used the database will have only the data and not codes (or notations, as is the case). The presentation layer (view) will have only the visual part and not the logic to search in the BD, treat and display. The controller that will do this part.

Therefore, in the database the data will be stored in a simple way (which could be used in other parts of the application and not only 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 fetches the data from the BD and creates an object or array with the ACTIVE menus. Simply put, it could be a Function that returns an array with menu items.

In the view would only take the array and loop to create the list items as for example:

foreach($array as $key => $value) {
    echo '<li><a href="' . $value['link'] . '">' . $value['titulo'] . '</a></li>';
}
  • agree with you

  • Thanks, @Everson . I made the change to develop... here in my land "encode" refers to writing source code :)

  • @Fernando I think in the land of everyone :)

  • @Fernando show, if you think that the previous way was correct could have left. I may be wrong, we are not always with the reason. As I put "but I don’t think", it says that for ME it was not correct, so not always "in the land of everyone" can be read the same way. But thank you ;)

  • 1

    @Everson shows where coding is synonymous with compiling

  • 1

    https://www.dicio.com.br/encode/ - in part from Sinônimos de Codificar, it was here that I sought the information.

  • 1

    @Everson Contexts completely different from what we use. This is what I’m talking about on this whole page, without context we can’t apply the general rule.

  • Good solution, Fernando!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.