Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're doing it wrong. In your procedural example you're iterating over the data a row at a time; in your OO example, if you treat them as arrays and then print them, you're going through the data a column at a time instead. Rather than separating the data into separate ids and titles, I would treat them as a bundle (i.e. similar to how you did it in the procedural version) - an id goes with a title, not other ids, right?</p> <p>So, for example, you might have a member variable</p> <pre><code>private $texts = array(); </code></pre> <p>and then in your constructor, do:</p> <pre><code>while ($info_row = $info-&gt;fetch_array()) { $text = array( 'id' =&gt; $info_row['info_id'], 'title' =&gt; $info_row['info_title'] ); $this-&gt;texts[] = $text; } </code></pre> <p>and then provide a method to get at this array of arrays:</p> <pre><code>public function getTexts() { return $this-&gt;texts; } </code></pre> <p>Finally, you could iterate over it very similarly to how you did in the procedural example:</p> <pre><code>&lt;?php $display = new InfoTest(); foreach ($display-&gt;getTexts() as $text) { ?&gt; &lt;!-- html goes here --&gt; &lt;?php echo $text['info_id']; ?&gt; &lt;!-- more html --&gt; &lt;?php echo $text['info_title']; ?&gt; &lt;!-- other html --&gt; &lt;? } ?&gt; </code></pre> <p>Stepping back - you could ask if all this is really necessary. There's nothing inherently wrong with procedural PHP - if it does what you need it to do and does it clearly, you might be better off favoring simple over complex here.</p>
 

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