What is the right way to mount interfaces on PHP pages?

Asked

Viewed 62 times

1

In several projects I have seen around, where it is necessary to consult databases to obtain information, and later to assemble tables, I noticed the following approach presented in the code snippet below:

<?php
    while($row = mysql_fetch_array($res)) {
?>
    <tr>
        <td><?php echo $row['nome']; ?></td>
        <td><?php echo $row['idade']; ?></td>
        <td><?php echo $row['dia']; ?></td>
        <td><?php echo $row['turno']; ?></td>
    </tr>
<?php
    }
?>

In it, one opens several times <?php ?> to extract information from the bank, and assemble the page.

This does not seem correct, it would not be better to use javascript(ajax) to retrieve information, and then DOM to assemble the page?

My question is what would be the best way to build pages where it is necessary to query databases for information.

  • Your question, while interesting, is based on opinions. For example, as I am a PHP developer I would defend PHP, the same with JS, AJAX and such. There is no right answer, only opinions.

  • Not to consider wrong, with ajax is more beautiful, fluid and fast, but with a php loop is simpler to run.

  • @Thiagosantos It would not be considered a bad practice to use PHP this way?

  • It is not even a matter of taste, it is dependent on the target of the project. Mounting the data by Javascript brings complications to SEO. If the project requires SEO compatibility, it is a steal to assemble the content by Javascript / DOM. It does not have a "more correct" form. It has what is most suitable for the project with which one works.

  • @Danielcosta Really, using Ajax would be a little more complicated, I just want to confirm if doing so with PHP is not considered a bad practice.

  • It seems to me that actually the big problem of the question is that it is broad or perhaps not clear, and therefore indicates to be broad. Opening PHP several times is not a big problem, that’s how it is done even. It has to do different, but changes very little. Using AJAX can be more interesting even in many cases, but not at all. But if you don’t know how to do it right can be worse. Doesn’t look like you have a specific problem you want to solve here.

  • 1

    @Gamen and why would the code presented be bad practice? There is nothing wrong. Except for the use of the mysql_* function: http://answall.com/questions/579

  • @Danielomine Yes, the mysql_* function was just to illustrate, imagine that I used PDO in the example. I’m not very experienced with PHP, and this opening several times <?php ? > seemed wrong, so the question.

  • @bigown I’m not very experienced with PHP, I just wanted to know even if this was wrong and if there was a better way to solve the problem.

  • There is nothing wrong with opening and closing tags. This makes it easy to avoid mixing with HTML. In the example of the question is very simple, but if you put other elements like images, texts, javascript, css, there is complicated because it needs to escape special characters. There’s a code more "dirty".

  • @Danielomine Yes, I found that the code gets kind of messy doing that way. Especially in the part where the key to the while is opened in a tag <?php ?> and then closed in another. But since it’s not wrong to do this, I will use less DOM in my codes. Thank you very much!

  • what I said is that it gets more organized even with many elements. And that otherwise it gets messy..

  • @Gamem keep in mind that even with PHP there are several ways to work. If you value organized code you can easily adopt a template engine and apply MVC. You can pre-load all the data before mounting the HTML page. It all depends on what you want, PHP is known for its extreme flexibility.

  • @Adrianoluz I will research more on this, thank you. I asked this question, why do things like <a href="<?php echo 'caminho'; ?>">Link</a> seemed pretty wrong to me.

  • @Danielomine Oh yes, I misunderstood you. But I do not find it very pleasant to see in any way. Thank you for being willing to help.

  • @Gamen instead of using <?php echo $row['nome']; ?> can use <?= $row['nome']; ?>

  • There is nothing wrong, but many people call this method spaghetti code, although the code is easy to understand and to create at the beginning, soon is a headache to keep.

Show 12 more comments

1 answer

1


There is nothing wrong with opening and closing tags to compose HTML content.

Everything that is delimited by the PHP tag is parsed by the PHP compiler.

If you pass things not related to PHP, this has an extra cost for the compiler to process the data.

Let’s take an example:

echo '<table><tr><td>'.$variavel.'</td></tr></table>';

Here in this example, PHP will just "spit" back the static chunk <table><tr><td> and </td></tr></table>.

That is, sent to the compiler to process a text that does not have to be processed. It is static information. PHP had to read this information, increasing memory consumption and processing.

Otherwise,

<table><tr><td><?php echo $variavel;?></td></tr></table>

The compiler will only receive the snippet $variavel;.

What would be a bad practice, is something like this

<?php
echo $variavel;
?>
<?php
echo $outra_variavel;

function alguma_coisa() {
    return null;
}

echo alguma_coisa();
?>

Here in this case yes, we even have a problem because it closed and opened unnecessarily because the following code is also PHP. Could just continue on the same tag

<?php
echo $variavel;

echo $outra_variavel;

function alguma_coisa() {
    return null;
}

echo alguma_coisa();
?>

Also, in doing

?>
<?php

It is generating an output that is usually the common cause among beginners with the famous "header already sent". But I’m not going into this subject because it’s more extensive and distracts from the focus of the issue.

Escaping quotes

A difficulty when going through all PHP is having to escape characters:

echo '<tr><td>texto com aspa simples \''.$variavel.'</td></tr>';

Can be solved using double quote

echo "<tr><td>texto com aspa simples '".$variavel."</td></tr>";

But this also depends a lot on the code pattern of the project. I particularly use single quote and there are those who prefer double quote. This is a matter of opinion and somewhat controversial. However, even with double quote falls into the same trap when the static text has double quote. You have to make the escape:

echo "<tr><td>texto com aspa dupla \"".$variavel."</td></tr>";

So far are very simple examples. In real life you will have things like this

lorem ipsum single quot'e double "quote"
<b>
<script>
var str = "foo'bar";
document.write(str);
</script>
<?php echo $variavel_do_php;?>
</b>

This is still a kid’s level... but it’s just to illustrate

See how it looks otherwise:

echo '
lorem ipsum single quot\'e double "quote"
<b>
<script>
var str = "foo\'bar";
document.write(str);
</script>
'.$variavel_do_php.'
</b>';

Tell me which of the two is best for code reading?

In terms of performance, they make no difference. So I usually choose the one that’s simplest and readable and, of course, it all depends on the purpose of use and that’s what matters most.

Javascript Template

Regarding the use of Javascript to assemble the view, the requirements of the project must be evaluated. If it is a web project, usually a website has to be SEO compatible. If you assemble the pages via Javascript you will have problems with SEO because the search engines do not interpret Javascript.

The layout montage scheme with Javascript was much used in the past before the "Google Age". It was very common for websites to assemble the content of pages with Javascript because it greatly reduces the HTML code. Unfortunately all were forced to increase redundant content on the pages due to SEO.

It is currently used more for private projects, intranets or even websites that don’t care about SEO.

MVC

If you analyze the question code for an MVC project, we can say that it is not a good practice because in MVC it usually does not invoke PHP functions directly in the View. It is usually using a template engine, helpers, etc.

In short

I have highlighted only obvious points. And there are still many other points to mention but this makes the subject too extensive.

In general, avoid thinking of "what is best for X or Y?". See this question as "what is best for project X or situation Y?". Because there is nothing that is "the universal solution". There are several solutions that are better suited to a given situation.

Minimizing the visual dirt

For those who care about the use of <?php echo $variavel;?>, it is possible to reduce using short tag.

The equivalent of this <?php echo $variavel;?> can be written as <?=$variavel;?>

Remembering that there was a time when the elimination of the short tag was considered. At that time, as a precaution, I stopped using. A while later they went back and decided to keep but I still avoid using it as a precaution. But this is a personal choice. I don’t mean you should think the same.

The use of <?php is recommended to avoid confusion with XML documents that also use <? tag.

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  ....

Browser other questions tagged

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