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

请教Call to a member function num_rows() on a non-object

[复制链接]
发表于 2008-10-10 17:11:41 | 显示全部楼层 |阅读模式
场景:
需要在一个页面显示数据表的所有数据
1定义了视图页面 ck_hlb.php
2定义了模型类 Hlb.php
3定义了控制器类 start.php

页面错误提示:
Fatal error: Call to a member function num_rows() on a non-object in D:\xampp\htdocs\zsPjdssxxsqc\system\application\views\ck_hlb.php on line 232

请各位大侠不吝赐教,谢谢
同时,我觉得CI相当方便以后的编码,非常想结交论坛里的朋友们

代码如下:
start.php
PHP复制代码
<?php
class Start extends Controller {
var $data = array (
   'KHM' => 'aaa,//客户名
   '
DQWZ' => '首页',//当前位置
   '
QTWZ' => '',//其他位置
   '
DBRES'//结果集
  );
function Start() {
  parent::Controller();
}
function index() {  
  $this->load->view('
start',$this->data);
}
 
function hlb($todo) {
  $this->load->helper('
url');
  //查看
  if ($todo == '
ck') {    
   if ( !isset($_REQUEST['
n']) || $_REQUEST['n'] == '' || !isset($_REQUEST['y']) || $_REQUEST['y'] == '' ) {  $this->data['DQWZ'] = '材料/设置年月';
    $this->data['
QTWZ'] = '<a href="'.base_url().'">首页</a>';
    $this->load->view('
ck_hlb_sz',$this->data);
   }
   else {    
    $this->load->model('
hlb');
    $this->data['
DBRES']=$this->hlb->ck($_REQUEST['n'],$_REQUEST['y']);
    $this->data['
DQWZ'] = '材料/查看报表';
    $this->data['
QTWZ'] = '<a href="'.base_url().'">首页</a>,编辑,打印';
    $this->load->view('
ck_hlb',$this->data);
   }      
  }  
}
}
?>
复制代码

[ 本帖最后由 ttl_web 于 2008-10-10 17:22 编辑 ]
 楼主| 发表于 2008-10-10 17:13:04 | 显示全部楼层
ck_hlb.php
PHP复制代码
<HTML>
<HEAD>
<TITLE>欢迎使用助手 - <?php echo $KHM; ?></TITLE>
<style type="text/css">
 
body {
background-color: #fff;
margin: 0px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;
}
 
a {
color: #003399;
background-color: transparent;
text-decoration: none;
font-weight: normal;
}
a:visited {
color: #003399;
background-color: transparent;
text-decoration: none;
}
a:hover {
color: #000;
text-decoration: none;
background-color: transparent;
}
 
#content {
border-top:1px solid #000;
margin: 0px 40px 0 40px;
padding: 0;
}
 
#content p {
margin: 10px 20px 12px 0;
}
 
#content h1 {
color: #333;
background-color: #efefef;
font-weight: normal;
font-size: 22px;
margin: 0 0 15px 0;
padding: 3px 2px 3px 10px;
}
 
#content h2 {
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
color: #444;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}
 
#content h3 {
background-color: transparent;
color: #333;
font-size: 14px;
font-weight: bold;
margin: 4px 0 12px 0;
padding: 0 0 0 0;
}
 
#content table tr td h3 {
background-color: transparent;
color: #333;
font-size: 14px;
font-weight: bold;
margin: 22px 0 12px 0;
padding: 0 0 0 0;
}
 
#content h4 {
background-color: transparent;
color: #444;
font-size: 14px;
font-weight: bold;
margin: 22px 0 0 0;
padding: 0 0 0 0;
}
 
#content img {
margin: auto;
padding: 0;
}
 
#content code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
 
#content pre {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
 
#content .path {
background-color: #EBF3EC;
border: 1px solid #99BC99;
color: #005702;
text-align: center;
margin: 0 0 14px 0;
padding: 5px 10px 5px 8px;
}
 
#content dfn {
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
color: #00620C;
font-weight: bold;
font-style: normal;
}
#content var {
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
color: #8F5B00;
font-weight: bold;
font-style: normal;
}
#content samp {
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
color: #480091;
font-weight: bold;
font-style: normal;
}
#content kbd {
font-family: Lucida Grande, Verdana, Geneva, Sans-serif;
color: #A70000;
font-weight: bold;
font-style: normal;
}
 
#content ul {
list-style-image: url(images/arrow.gif);
padding: 0 0 0 18px;
margin: 8px 0 12px 0;
}
 
#content li {
padding: 0;
margin: 0 0 6px 0;
}
 
#content li p {
margin-left: 0;
margin-right: 0;
}
 
#content .tableborder {
border: 1px solid #999;
}
#content th {
font-weight: bold;
text-align: left;
font-size: 12px;
background-color: #666;
color: #fff;
padding: 4px;
}
 
#content .td {
font-weight: normal;
font-size: 12px;
padding: 6px;
background-color: #f3f3f3;
}
 
#content .tdpackage {
font-weight: normal;
font-size: 12px;
}
 
#content .important {
background: #FBE6F2;
border: 1px solid #D893A1;
color: #333;
margin: 10px 0 5px 0;
padding: 10px;
}
 
#content .important p {
margin: 6px 0 8px 0;
padding: 0;
}
 
#content .important .leftpad {
margin: 6px 0 8px 0;
padding-left: 20px;
}
 
#content .critical {
background: #FBE6F2;
border: 1px solid #E68F8F;
color: #333;
margin: 10px 0 5px 0;
padding: 10px;
}
 
#content .critical p {
margin: 5px 0 6px 0;
padding: 0;
}
 
</style>
</HEAD>
<BODY>
 
 
<div id="content">
<h1>助手 -<?php echo $KHM; ?>
<h2>当前位置:<?php echo $DQWZ.'('.$_REQUEST['n'].'年'.$_REQUEST['y'].'月)' ?></h2>
<h3>其他位置:<?php echo $QTWZ ?></h3>
 
<code><strong>操作提示:</strong>  可以通过点击左侧的编号进行数据修改</code>
 
<?php
$num_results = $DBRES->num_rows();
 
if( $num_results == 0 ) {
  echo '<div class="zs_ts">...指定年月的表没有数据</div>';
}
else {
 
  $sum_c7 = 0.00;
  $sum_c10 = 0.00;
  $sum_c13 = 0.00;
  $sum_c16 = 0.00;
  $sum_c19 = 0.00;
  $sum_c22 = 0.00;
  $sum_c25 = 0.00;
  $sum_c28 = 0.00;
  $sum_c31 = 0.00;
 
  echo '<div class="zs_ts">...'.$_REQUEST['n'] .'年'.$_REQUEST['y'].'月 报表如下</div>';
  echo '<div class="zs_ts">...<A HREF="#hj">查看合计数</A>,<A HREF="#" name="ys">页首位置</A>,<a href="./hlb_dy_jl.php?n='.$_REQUEST['n'] .'&y='.$_REQUEST['y'].'" target="_blank">打印a表</A>,<a href="./hlb_dy_hl.php?n='.$_REQUEST['n'] .'&y='.$_REQUEST['y'].'" target="_blank">打印b表</A></div>';
 
  if ( isset($_REQUEST['id']) ) {
   echo '<div class="zs_ts">...<A HREF="#'.$_REQUEST['id'].'">查看最近修改的数据</A></div>';
  }
?>
<TABLE>
<?php
  for($i = 0 ; $i< $num_results;$i++) {
   $row = $result->fetch_assoc();
 
   if ($i == 0 || $i%15==0) {
?>
<TR class="odd">
<TD rowspan="3">编号</TD>
<TD rowspan="3">名称</TD>
<TD rowspan="3">规格</TD>
<TD rowspan="3">单位</TD>
<TD rowspan="3">账册
编号</TD>
<TD rowspan="2" colspan="3">上期库存</TD>
<TD rowspan="2" colspan="3">本期进料</TD>
<TD rowspan="2" colspan="3">本期估价进料</TD>
<TD rowspan="2" colspan="3">本期耗用</TD>
<TD rowspan="1" colspan="12">本期耗用明晰</TD>
<TD rowspan="2" colspan="3">本期结余</TD>
</TR>
<TR class="odd">
<TD colspan="3">车间:消</TD>
<TD colspan="3">车间:冷</TD>
<TD colspan="3">车间:金</TD>
<TD colspan="3">车间:电</TD>
</TR>
<TR class="odd">
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
<TD>数量</TD>
<TD>金额</TD>
<TD>单价</TD>
</TR>
<?php
   }
   
   if ($i%2) {
    echo '<TR class="oddq2">';
   }
   else {
    echo '<TR class="even">';
   }
?>
 
<TD>
<a href= "./hlb_bbbj.php?todo=bbbj&n=
<?php  echo $_REQUEST['n'].'&y='.$_REQUEST['y'].'&id='.$row['c1'] ?>
" name="<?php echo $row['c1'] ?>">
<?php echo $row['c1'] ?> </a>
</TD>
<TD><?php echo $row['c2'] ?></TD>
<TD><?php echo $row['c3'] ?></TD>
<TD><?php echo $row['c4'] ?></TD>
<TD><?php echo $row['c5'] ?></TD>
<TD><?php echo $row['c6'] ?></TD>
<TD><?php echo $row['c7'] ?></TD>
<TD><?php echo $row['c8'] ?></TD>
<TD><?php echo $row['c9'] ?></TD>
<TD><?php echo $row['c10'] ?></TD>
<TD><?php echo $row['c11'] ?></TD>
<TD><?php echo $row['c12'] ?></TD>
<TD><?php echo $row['c13'] ?></TD>
<TD><?php echo $row['c14'] ?></TD>
<TD><?php echo $row['c15'] ?></TD>
<TD><?php echo $row['c16'] ?></TD>
<TD><?php echo $row['c17'] ?></TD>
<TD><?php echo $row['c18'] ?></TD>
<TD><?php echo $row['c19'] ?></TD>
<TD><?php echo $row['c20'] ?></TD>
<TD><?php echo $row['c21'] ?></TD>
<TD><?php echo $row['c22'] ?></TD>
<TD><?php echo $row['c23'] ?></TD>
<TD><?php echo $row['c24'] ?></TD>
<TD><?php echo $row['c25'] ?></TD>
<TD><?php echo $row['c26'] ?></TD>
<TD><?php echo $row['c27'] ?></TD>
<TD><?php echo $row['c28'] ?></TD>
<TD><?php echo $row['c29'] ?></TD>
<TD><?php echo $row['c30'] ?></TD>
<TD><?php echo $row['c31'] ?></TD>
<TD><?php echo $row['c32'] ?></TD>
</TR>
<?php
 
   $sum_c7 += $row['c7'];
   $sum_c10 += $row['c10'];
   $sum_c13 += $row['c13'];
   $sum_c16 += $row['c16'];
   $sum_c19 += $row['c19'];
   $sum_c22 += $row['c22'];
   $sum_c25 += $row['c25'];
   $sum_c28 += $row['c28'];
   $sum_c31 += $row['c31'];
  }
?>
<TR class="odd">
<TD></TD>
<TD><A HREF="#" name="hj">合计</A></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c7) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c10) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c13) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c16) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c19) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c22) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c25) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c28) ?></TD>
<TD></TD>
<TD></TD>
<TD><?php echo sprintf('%01.2f',$sum_c31) ?></TD>
<TD></TD>
</TR>
</TABLE>
<A HREF="#ys">返回页首</A>
<?php
}
 
?>
</div>
 
 
 
 
 
</BODY>
</HTML>
复制代码

[ 本帖最后由 ttl_web 于 2008-10-10 17:17 编辑 ]
 楼主| 发表于 2008-10-10 17:19:54 | 显示全部楼层
Hlb.php
PHP复制代码
<?php
class Hlb extends Model {
function Hlb() {
  parent::Model();
}
function ck($n,$y) {
  $this->load->database();
  $sql = 'select a.id as c1,b.mc as c2,b.gg as c3,b.dw as c4,b.id_zc as c5,
  a.sqkc_sl as c6,a.sqkc_je as c7,a.sqkc_dj as c8,
  a.bqjl_sl as c9,a.bqjl_je as c10,a.bqjl_dj as c11,
  a.bqgj_sl as c12,a.bqgj_je as c13,a.bqgj_dj as c14,
  a.bqll_sl as c15,a.bqll_je as c16,a.bqll_dj as c17,
  a.bqll_cj1_sl as c18,a.bqll_cj1_je as c19,a.bqll_cj1_dj as c20,
  a.bqll_cj2_sl as c21,a.bqll_cj2_je as c22,a.bqll_cj2_dj as c23,
  a.bqll_cj3_sl as c24,a.bqll_cj3_je as c25,a.bqll_cj3_dj as c26,
  a.bqll_cj4_sl as c27,a.bqll_cj4_je as c28,a.bqll_cj4_dj as c29,
  a.bqjy_sl as c30,a.bqjy_je as c31,a.bqjy_dj as c32
  from hlb'
.$_REQUEST['n'].$_REQUEST['y'].' a,clxx b where a.id = b.id ORDER BY a.id_zc ASC ';
  //echo ''.$sql;
  $result = $this->db->query($sql);  
  return $result->result();
}
}
?>
复制代码
发表于 2008-10-10 17:27:00 | 显示全部楼层
num_rows() 是 query 对象的方法,而你用了 query->result(),result() 返回的是一个结果集,只有你的数据库记录。
具体请参考:http://codeigniter.org.cn/user_guide/database/results.html
 楼主| 发表于 2008-10-11 08:08:30 | 显示全部楼层
谢谢Hex,问题已解决。
发表于 2008-10-14 22:25:57 | 显示全部楼层
这个代码的可读性实在有点。。。,逻辑操作尽量在控制器里完成,视图尽量简单些,要不然看起来太难受了

本版积分规则