I'm trying to convert my procedural code to oop.
<?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'];
?>
<div style="width: 100%;">
<div style="float: left;">
<?php echo $info_id; ?>
</div>
<div style="float: left;">
<?php echo $info_title; ?>
</div>
<div style="clear: both;"></div>
</div>
<?php } ?>
My incomplete attempt at classes/objects without the HTML styling:
<?php
class InfoTest {
private $info_id;
private $info_title;
public function __construct() {
$dbc = get_dbc();
$info = $dbc->query ("SELECT info_id, info_title FROM text");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
while ($info_row = $info->fetch_array())
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
}
$info->free();
$this->info_id = $info_id;
$this->info_title = $info_title;
}
public function setInfoID() {
$this->info_id = $info_id;
}
public function getInfoID() {
return $this->info_id;
}
public function setInfoTitle() {
$this->info_title = $info_title;
}
public function getInfoTitle() {
return $this->info_title;
}
public function __destruct() {
}
}
?>
<?php
$display = new InfoTest();
echo $display->getInfoID();
echo $display->getInfoTitle();
?>
My procedural code prints out: 1 One 2 Two.
My oop code prints out: 2 Two
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.
So, if I change:
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
To:
$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];
And print the arrays, it displays all the information I want, but how to display it in non-array form?
Is what I'm doing so far correct or am I approaching this wrong?