Is it a bad practice to fill select option / option with data coming from the database? [closed]

-3

It would be a bad practice to use selects of HTML with items fetched through a database, even if they are dynamic (always occurring addition in the table that contains them).

Example without database:

<select>
  <option value="1">item 1</option>
  <option value="2">item 1</option>
  <option value="3">item 1</option>

Example with database:

<select>
<?PHP foreach($dadosbanco as $value): ?>
  <option value="<?php echo $value['id']; ?>"> <?php echo $value['item']; ?></option>
<?php endforeach; ?>
    
asked by anonymous 10.08.2016 / 05:31

3 answers

3

There is no such thing as "bad practice". There is what needs to be done to solve a problem, meet a requirement and do free things. There is the right to do it in the concrete case or not. I perceive a certain obsession with "good and bad practices" by those who are still starting, probably encouraged by more experienced programmers who want to "impose" their tastes and not show the basics. People will program better when they understand that ready-made recipes may even help, but applied without understanding further disturbs.

Then ask yourself, can you see any problem in this? Does this problem occur in any situation? Is there something simple to get around it? Is there a more viable alternative? Are there security, performance, maintenance issues? Do I need this?

If you need to grab the database options and mount the HTML with the options, you should do it, you have no reason not to. You have to do it right, of course. If you do not need to get the database, you do not have to do it. That is the question that needs to be asked.

In the example shown in the question it seems all right (of course I would need to see if the rest of the code is correct, but this snippet is fine), I would do so assuming a scenario that is useful. From the information you have posted you will need to grab the database.

    
10.08.2016 / 06:09
2

It is not feasible to determine if there is any bad practice in the displayed code.

The question is vague and creates a disconnected sense.

Something closer to "bad practice" may be the style used in PHP syntax.

ex:

foreach($dadosbanco as $value):

endforeach

could be

foreach ($dadosbanco as $value) {

}

I believe this style is more readable. I'd rather avoid alternative ways. Because it just makes code more complex, especially for beginners in language. But it is personal opinion and can ignore. Just can not ignore that there are standards defined and accepted by the PHP community in the whole world. Currently there is an effort to spread the "PSRs". See official website: link

Just do not confuse this as if it were a law, as no one is obliged to use the standards suggested by this site, nor should we judge the style and patterns of other codes compared to the "PSRs."

Another point is to invoke echo in a loop of repetition. It is usually slower than concatenating and giving only a echo . But that is neither very relevant nor considered bad practice.

Something that I imagine may come close to the purpose of the question is that you may have heard something about avoiding mixing HTML, PHP, etc. code. The one from MVC.

Still, as presented, there is no relevant bad practice.

    
10.08.2016 / 06:06
1

No, it is not bad practice. This is used all the time when we need select options that are volatile and stored in a database. As the above people have said, a lot of what they say is not good practice, it's really just priceless. Of course there is a limit, but in this case I see no problem.

    
10.08.2016 / 11:41