Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How I separate logic from presentation?</p> </blockquote> <h1>Separate it.</h1> <p>Though it is called <strong>business logic</strong> from <strong>presentation logic</strong> separation. You have logic in both layers. The name says it all:</p> <ul> <li>perform your "logic" first</li> <li>then turn to "presentation."</li> </ul> <p>Taking your example it have to be something like this</p> <pre><code>$res = $stmt-&gt;get_result(); $data = array(); while ($row = mysqli_fetch_assoc($res)) { $data[] = $row; } </code></pre> <p>though I would use some more intelligent approach to get data from database, something like this:</p> <pre><code>$data = $db-&gt;getArr("SELECT ..."); </code></pre> <p>then repeat all the steps for all the database or other other services interaction. Your goal is to <strong>have all the data ready that business logic have to supply.</strong> Then your business logic ended and you can turn to presentation. </p> <p>You can tell a good separation from the bad one <strong>if you can easily interchange template engines</strong> (you can't do it with approach from other answer, mind you) - so, particular engine doesn't matter. Let's take a simplest one - PHP</p> <p>Create a file called <code>tpl.php</code> and put this code there</p> <pre><code>&lt;table&gt; &lt;?php foreach ($data as $row): extract($row); ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?=$title&lt;/td&gt; and so on &lt;/tr&gt; &lt;?php endforeach ?&gt; </code></pre> <p>then include this file in your business logic file. Or - better - in some higher level template.</p> <p>You can see a good real life example of the approach in <a href="https://codereview.stackexchange.com/questions/6359/better-separation-of-programming-and-presentation-logic">this topic</a></p> <p>Using such a separation, you can: </p> <ol> <li>Use power of good editor that can do syntax highlighting and hinting (though I doubt you are using one). </li> <li>outsource your design to a professional designer. </li> <li>easily change presentation technology from HTML to AJAX.</li> <li>use the same engine for the different sites with different appearances.</li> <li>Implement any number of "themes" or "skins".</li> <li>Stay alive when adding some rich decorations from either HTML, CSS or JS. </li> </ol> <p>While all this is just impossible with your current approach or approach from other answer. The idea is <strong>separation</strong> of matters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload