dpc57092 2013-06-27 03:18
浏览 33
已采纳

试图在Codeigniter中获取非对象的属性?

My controller code just generate ten copies of the same data. And I put the data in an array of array.Controller Code Below:

for($i=0;$i < $this->per_page;$i++)
{
    $temp['title'] = "test";
    $temp['link'] = "localhost/cib/index.php";
    $temp['month'] = '4';
    $temp['day'] = '21';
    $temp['authorlink'] = "localhost/cib/index.php/author";
    $temp['author'] = "Charles";
    $temp['content'] = "tetestersafaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    $results[] = $temp;
}

$data['main_content'] = 'report_list';
$data['posts'] = $results;

$this->load->view('include/templates', $data);

And I show the data generated by controller in the view page report_list, the code of report_list is

<div id="bg_top">
</div>
<div class = "container" id="content">
    <div class="row-fluid">
        <div class="span9" id="content_left">
        <h2>解题报告列表</h2>
        <link href="<?php echo base_url('assets/css/style.css') ?>" rel="stylesheet">
        <?php if(!empty($posts) && is_array($posts)):?>
            <?php foreach($posts as $post):?>
                <div class="entry">
                    <h2 class="entrytitle"><?php echo anchor($post->link, $post->title)?><span></span></h2>
                    <div class="entrymeta1"> 
                        <div class="meta-date">
                            <span class="meta-month"><?php echo $post->month?></span>
                            <span class="meta-day"><?php echo $post->day?></span>
                        </div>
                        <span class="meta-author"><?php echo anchor($post->authorlink, $post->author)?></span>
                        <span class="meta-category"></span>
                        <span class="meta-comment"></span>
                        <div class="clear"></div>
                        </div><!-- [entrymeta1] -->
                    <div class="entrybody">
                        <p><?php echo $post->content;?><?php echo anchor($post->link, '阅读全文')?></p>
                    </div>
                    <div class="entrymeta3">
                    </div>
                </div><!-- [entry] -->
        <?php endforeach;?>
    <?php endif;?>
        </div>
        <div class="span3">
        <?php echo "hello world2";?>
        </div>
    </div>
</div>

The question is where am wrong? why is there errors "Trying to get property of non-object"? I tried to print the data in the controller, it works quite well.

foreach($results as $post)
{
    foreach($post as $key=>$value)
    {
        echo $key.':'.$value;
        echo '<br/>';
    }
}
  • 写回答

3条回答 默认 最新

  • doujiu1447 2013-06-27 05:55
    关注

    You need to print the variables in the view file in array style like this:

    <?php echo anchor($post['link'], $post['title'])?>
    

    Still you can always print the array to view the structure:

    <?php echo "<pre>";print_r($posts);echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程