Is it bad practice to fill <select> <option></option> with data from the database?

Asked

Viewed 493 times

-2

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

Example without a 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; ?>
  • 1

    Just to give an allowance for my answer, do you imagine any problem that might occur doing this? Someone said it’s bad?

  • In another Post I commented on using a static method to do this and I received responses not recommending so I wanted to know why. Pq to my view this would make it much easier the work type has an organ table that always has a new and several screens of the system uses this select ai manually I always go in HTML and add on each screen and so would not need more.

  • 1

    Now it’s getting better, you can [Dit] the question by posting a link referencing that answer.

  • @bigown after the comment I was even more confused... Thailes you wonder if it is bad practice to do so statically or dynamically?

  • @Jorge B. The serious static method of the class to invoke it to execute the dynamic select.

  • @Thallesdaniel ask [Edit] your question and add this information and if possible put the example of this code.

  • @Thallesdaniel did not understand...

  • @bigown thanks for editing maybe now it became clear the goal. The reason for the negativations was because she n was clear?

Show 3 more comments

3 answers

4

It is not feasible to determine whether there is bad practice in the presented 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 that this style is more readable. I prefer to avoid alternative forms. Because it only makes the code more complex, especially for beginners in the language. But it is personal opinion and can ignore. Just do not ignore that there are standards defined and accepted by the PHP community np worldwide. Currently there is an effort to spread the "Psrs". See official website: http://www.php-fig.org/psr/

Just don’t confuse this as if it were a law, because no one is required to use the patterns suggested by this site and neither should we judge the style and patterns of other people’s codes compared to "Psrs".

Another point is to invoke echo in a repeat loop. It is usually slower than concatenating and giving only one echo. But this is not very relevant and not considered bad practice.

Something that I imagine can come close to the point of the question is that maybe you’ve heard something about avoiding mixing HTML, PHP, etc. The MVC thing.

Still, the way you put it there’s no relevant bad practice.

4


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 doing in the specific case or not. I notice 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 fundamentals. People will program better when they understand what ready-made recipes can even help, but applied without understanding more hinder.

So ask yourself: can you see any problem with that? Does this problem occur in any situation? Is there anything simple about getting around it? Is there a more viable alternative? Are there safety, performance, maintenance problems? I need this?

If you need to take the database options and mount the HTML with the options, you should, there is no reason not to. You have to do it right, of course. If you don’t have to take it from the database, you don’t have to do it. That’s 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 is all right), I would do so, assuming a scenario that this is useful. From the information posted on what you’re going to use, you seem to need to take from the database.

3

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

Browser other questions tagged

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