I test a tuto. i'm novice in php. I have a entity in news.php, a manager and a page for displaying results. I execute a query with PDO. i have results but ican't display them.
the entity is like that:
class News {
private $_id;
private $_titre;
private $_contenu;
public function id()
{
return $this->_id;
}
public function titre()
{
return $this->_titre;
}
public function contenu()
{
return $this->_contenu;
}
public function setId($id)
{
if(!is_int($id))
throw new Exception('L\'id n\'es pas de type int');
$this->_id = $id;
}
public function setTitre($titre)
{
if(!is_string($titre))
throw new Exception('Le titre n\'es pas de type string');
$this->_titre = $titre;
}
public function setContenu($contenu)
{
if(!is_string($contenu))
throw new Exception('Le contenu n\'es pas de type string');
$this->_contenu = $contenu;
}
}
My news manager :
class newsManager
{
private $_db;
const SELECT_5LAST_NEWS = 'SELECT * FROM news ORDER BY id DESC LIMIT 0,5';
public function select5News()
{
$query = $this->_db->query(self::SELECT_5LAST_NEWS);
$query->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'News');
return $query->fetchAll(PDO::FETCH_CLASS, 'News');;
}
}
Then i test my view:
require 'classes/newsManager.php';
$newsmngr = new newsManager(); $req = $newsmngr->select5News();
foreach ($req as $u) {
print_r($u); }
thats give:
News Object ( [_id:News:private] => [_titre:News:private] =>
[_auteur:News:private] => [_contenu:News:private] =>
[_dateAjout:News:private] => [_dateModif:News:private] => [id] => 3
[auteur] => howsDoingThis [titre] => where i am.... [contenu] => Lorem
ipsum dolor sit amet,(...)
my result is filled but when i try to display it nothing happens with that code:
foreach($req as $news)
{
echo '<section><p class="titre"><a href="administration.php?id='.$news-id().'">'.$news->titre().'</a></p>';
echo '<p class="description">'.$news->contenu().'</p></section>';
}
anyone can help me plz. thanks
id and contenu are private variable, you can't access them directly. You must use some public getter methods defined in your News class
public function getId(){
return $this->_id;
}
Then you get the data through the getter method like this $news->getId();
Hope this help