New to the PHP and the MVC model structure so please be gentle as I try to figure this out as I know I making some mistakes but cannot figure out where. I have been trying to search and experiment with other submitted questions and YouTube with no luck.
Trying to query my database table personal_best and return a looped list <li></li> for the columns pb_species, pb_weight_lbs and pb_weight_oz for a specific user and display them to my webpage.
I have my model class (class PBInfo extends Dbh)
controller class (class PBInfoContr extends PBInfo)
view class (class PersonalBestInfoView extends PBInfo).
In my model class I have the following:
protected function getPBRecords($userId) {
$stmt = $this->connect()->prepare('SELECT * FROM personal_best WHERE users_id = ?;');
$stmt->execute(array($userId));
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
$results [] = $row;
}
return $results;
}
In my controller class I have the following:
public function displayPBRecords() {
$row = $this->getPBRecords();
$this->row = $pbRecords;
}
In my view class I have the following code:
public function fetchPBRecords($userId) {
$personalBestInfo = $this->getPBRecords($userId);
foreach ($pbRecords as $data) {
echo "<li>".$data['pb_species']."<br>".$data['pb_weight_lbs']." lbs ".$data['pb_weight_oz']." oz</li>";
}
}
In the webpage to display the looped data I have:
<?php $personalBestRecord->fetchPBRecords($_SESSION['userid'])?>
I get the following 2 error messages when I try to run the page:
- Undefined variable $pbRecords in my view class.
- foreach() argument must be of type array|object null given in my view class.
Any suggestions would be appreciated and thanks in advance.