Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Procedural To OOP
    primarykey
    data
    text
    <p>I'm trying to convert my procedural code to oop.</p> <pre><code> &lt;?php $dbc = get_dbc(); $info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc)); while ($info_row = mysqli_fetch_array($info)) { $info_id = $info_row['info_id']; $info_title = $info_row['info_title']; ?&gt; &lt;div style="width: 100%;"&gt; &lt;div style="float: left;"&gt; &lt;?php echo $info_id; ?&gt; &lt;/div&gt; &lt;div style="float: left;"&gt; &lt;?php echo $info_title; ?&gt; &lt;/div&gt; &lt;div style="clear: both;"&gt;&lt;/div&gt; &lt;/div&gt; &lt;?php } ?&gt; </code></pre> <p>My incomplete attempt at classes/objects without the HTML styling:</p> <pre><code> &lt;?php class InfoTest { private $info_id; private $info_title; public function __construct() { $dbc = get_dbc(); $info = $dbc-&gt;query ("SELECT info_id, info_title FROM text"); if ($dbc-&gt;error) { printf("Error: %s\n", $dbc-&gt;error); } while ($info_row = $info-&gt;fetch_array()) { $info_id = $info_row['info_id']; $info_title = $info_row['info_title']; } $info-&gt;free(); $this-&gt;info_id = $info_id; $this-&gt;info_title = $info_title; } public function setInfoID() { $this-&gt;info_id = $info_id; } public function getInfoID() { return $this-&gt;info_id; } public function setInfoTitle() { $this-&gt;info_title = $info_title; } public function getInfoTitle() { return $this-&gt;info_title; } public function __destruct() { } } ?&gt; &lt;?php $display = new InfoTest(); echo $display-&gt;getInfoID(); echo $display-&gt;getInfoTitle(); ?&gt; </code></pre> <p>My procedural code prints out: 1 One 2 Two.</p> <p>My oop code prints out: 2 Two</p> <p>From my understanding the oop prints out that way because $info_id and $info_title aren't arrays, and only print out the last stored information.</p> <p>So, if I change:</p> <pre><code>$info_id = $info_row['info_id']; $info_title = $info_row['info_title']; </code></pre> <p>To:</p> <pre><code>$info_id[] = $info_row['info_id']; $info_title[] = $info_row['info_title']; </code></pre> <p>And print the arrays, it displays all the information I want, but how to display it in non-array form?</p> <p>Is what I'm doing so far correct or am I approaching this wrong?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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