试图获得非对象的属性

时间:2016-05-03 22:22:30

标签: php codeigniter

由于2天我无法找到解决此问题的方法。 我提到我想从DB中检索一些字段并将它们自动引入视图中。

以下是代码:

控制器:

function edit($seo_url){
    $info=$this->info_pages->get_page($seo_url);
    print_r($info);

    $TITLE="Page".$info->page_title; // line 44
    $CONTENT=$this->parser->parse('info/modify',
    $info,
    TRUE);      //line 47
    $data = array(
                    "TITLE"=>$TITLE,
                    "CONTENT"=>$CONTENT             
                 );

    $this->parser->parse("template/full-width",$data);          
}

型号:

    function get_page($seo_url){
    $this -> db -> select('page_title,page_content');
    $this -> db -> from('pages');
    $this -> db -> where('seo_url', $seo_url);
    $page = $this -> db -> get();

    echo $this->db->last_query();

    if($page -> num_rows()>0 ) {
        $x=$page->row();
            return $x;
            }
            else return false;

}

查看:

   <h3>Title<input name="page_title" id="page_title" value="{page_title}" /></h3></div>

    <h3>Content
        <textarea type="text" name="page_content" id="page_content" rows="10" class="form-control markdown" data-height="300" data-lang="en">{page_content}</textarea>
    </h3></div>

这是发生的错误:

   #1
   Severity: Notice
   Message:  Trying to get property of non-object
   Filename: controllers/pagini.php
   Line Number: 44 - I marked it above.

   #2
   Severity: Warning
   Message:  Invalid argument supplied for foreach()
   Filename: libraries/Parser.php
   Line Number: 144

按下编辑页面的按钮时会发生错误。 链接如下所示:http://localhost/admin.php/pagini/edit/57

先谢谢,我对Codeigniter&amp; amp; PHP:D!

2 个答案:

答案 0 :(得分:0)

;WITH 
numbers AS          -- added
(
    SELECT  n = 1
    UNION ALL
    SELECT  n = n + 1
    FROM    numbers
    WHERE   n < 999
),
ctetest AS 
        (
            SELECT      n AS Data,                   -- change
            @startdate AS CDate, 
                        CAST (n as FLOAT) as Case2,   -- change
                        0 as OLNo
        FROM        numbers                           -- change
        WHERE   n   <= @setpoint                 -- change

            UNION ALL

            SELECT      Data,
            dateadd(day,1,CDate),
                        CASE WHEN Case2 < @recoveredmw THEN @setpoint ELSE (1 - @declinerate) * Case2 END,
                        OLNo + (CASE WHEN Case2 < @recoveredmw THEN 1 ELSE 0 END)
            FROM        ctetest 
            WHERE       dateadd(day, 1, CDate) <= @finishdate
        )
        SELECT      * 
        FROM        ctetest 
    order by    Data DESC, CDate     -- change
    OPTION (MAXRECURSION 0)

答案 1 :(得分:0)

控制器功能:

function edit($seo_url)
{
    $info=$this->info_pages->get_page($seo_url);

    if(!empty($info))
    {
        $TITLE= "Page".$info['page_title']; 
        $CONTENT=$this->parser->parse('info/modify',$info,TRUE);  
        $data = array(
                        "TITLE"=>$TITLE,
                        "CONTENT"=>$CONTENT             
                     );

        $this->parser->parse("template/full-width",$data);  
    }           
}

模型功能:

function get_page($seo_url)
{
    $this->db->select('page_title,page_content');
    $this->db->from('pages');
    $this->db->where('seo_url',$seo_url);
    $query = $this->db->get();
    $result = $query->result_array();

    if(count($result)> 0)
    {
        return $result
    }
    else
    {
        return false;
    }
}