I'm going through an array of data with a foreach, but at the time of crossing it I release this Invalid argument supplied for foreach () and it tells me that the variables are undefined. This is my code:
<?php
include 'Models.php';
class Restaurant extends Models{
private $datos;
public function __construct(){
$this->datos=array();
parent::__construct();
}
public function InfoRest(){
session_start();
$id = $_SESSION['usuario']['id_usuario'];
$sql = $this->pdo->prepare("SELECT * FROM restaurants WHERE id_usuario = '$id'");
$sql->execute();
if($sql->rowCount()>=1){
while($row=$sql->fetch()){
$this->datos[]=$row;
}
return $this->datos;
}else{
$sql->errorInfo()[2];
}
}
}
?>
$obj = new Restaurant();
$values = $obj->InfoRest();
foreach($values as $value){
}
<?php echo $value['name_rest'] ?>
When I go through it, it throws me those errors. I have given a lot of laps but I can not even solve. I have done var_dump in the execute and it releases true, and in the same project I have something done in the same way and it does not throw error, for example this:
class Models{
protected $pdo;
private $datos;
public function __construct(){
$this->datos=array();
try{
$this->pdo = new
PDO('mysql:host=localhost;dbname=vmenu.us;charset=utf8','root','');
}
catch(PDOExcepcion $e)
{
echo $e->getMessage();
exit;
}
}
public function DetailsRestaurant($id){
$sql = $this->pdo->prepare("SELECT * FROM restaurants WHERE id_restaurant = :id");
$sql->bindParam(':id',$id);
$sql->execute();
if($sql->rowCount()>=1){
while($row = $sql->fetch()){
$this->datos[]=$row;
}
return $this->datos;
}else{
$sql->errorInfo()[2];
}
}
}
And then this in my Vista:
$obj = new Models();
$id = $_GET['id_restaurant'];
$details = $obj->DetailsRestaurant($id);
foreach($details as $result){
}
<?php echo $result['name_rest'] ?>
As you can see, it's the same