Hi I am very new to PHP and Objects and have become very confused on the best way to create a collection of objects created from a database. Below is an example of what is working for me but its a single object with array properties. Is this the best options or do I create multiple objects for each book and if I create multiple objects how do I loop through to display on my grid.
<?php
declare(strict_types=1); // FAIL FAST
//##############################################
class Books {
// Properties
protected $id_type=array();
protected $title=array();
protected $brief=array();
//==============================================
function set_books($id, $mysqli)
{
//DB Query
$result = $stm->get_result();
$stm->close();
while ($rows = $result->fetch_assoc()) {
$this-title[] = $rows['title'];
$this->brief[] = $rows['brief'];
}
}
//==============================================
function get_title()
{
return $this->title;
}
//==============================================
}
$books = new Books();
$books->set_books(4, $mysqli);
$book_count = count($books->get_title());
$i=0;
while ($i < $book_count) {
echo "<h1>".$books->get_title()[$i]."</h1>";
$i++;
}
?>
I have google and cannot find the exact answer I am looking for most examples show a single object like displaying a single book not a list of books.