douqi5079 2015-11-11 18:28
浏览 5

我的PDO结果不适用于显示

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

  • 写回答

1条回答 默认 最新

  • dongpo8250 2015-11-11 19:44
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题