用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2590|回复: 4
收起左侧

新手求助一个问题

[复制链接]
发表于 2013-5-6 15:32:58 | 显示全部楼层 |阅读模式
纯新手,接触CI两三天,试着跟着视频教程做,跟用 CodeIgniter 开发新闻发布系统(上)这个视频做时,遇到一个问题




A PHP Error was encountered
Severity: Notice
Message: Undefined variable: row
Filename: views/header.php
Line Number: 14
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-objec



我的代码部分是这样
function get_category()
        {
                $query = $this->db->query('SELECT * FROM category');
                return $query->result();
               
        }

function index()
        {
                $this->load->model('Mhome');
                $data['category'] = $this->Mhome->get_category();
                $data['pagetitle'] = "CI Develop";
                $this->load->view('header',$data);
                $this->load->view('index');
                $this->load->view('footer');
        }


<? foreach ($category as $row):?>
<li><a href="#" ><?php echo $row->category_name; ?></a></li>
<?endforeach ;?>



因为不能用脚手架,我就在phpmyadmin中手动添加了数据,但是出现了上面了错误,希望大神可以指导一下~~
发表于 2013-5-6 17:44:27 | 显示全部楼层
具体贴一下哪行代码出问题了,你这个代码看起来没问题。
 楼主| 发表于 2013-5-6 18:03:50 | 显示全部楼层
说是header的第14行代码有问题,row没有定义?而且好像phpmyadmin里面的数据库中的数据也没用传上来,需要加入哪条语句呢?还有一个问题,验证Session时login界面怎么无法跳转到checklogin的界面?求指导,灰常感谢啊。
header部分的代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $pagetitle; ?></title>
<base href="<?base_url()?>"/>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<!--header start -->
<div id="header">
<ul>
<li><a href="#" class="hover">home</a></li>
<? foreach ($category as $row):?>
<li><a href="#" ><?php echo $row->category_name; ?></a></li>
<?endforeach ;?>
</ul>
</div>
<!--header end -->
<body>
<!--body start -->
发表于 2013-5-6 18:18:49 | 显示全部楼层
你这个 $row 明明是定义了。。。。。
看起来没问题啊
发表于 2013-5-7 15:22:09 | 显示全部楼层
function get_category()
        {
                $query = $this->db->query('SELECT * FROM category');
                return $query->result();
               
        }这个方法里返回的是一个以为数组
<? foreach ($category as $row):?>
<li><a href="#" ><?php echo $row->category_name; ?></a></li>
<?endforeach ;?>
这里遍历的是以为数组。 $row->category_name这是对象调用模式,而$row不是一个对象,你可以换成for循环试一试

本版积分规则