这是什么"消息:未定义的属性:stdClass"意思?

时间:2015-01-16 15:31:38

标签: php codeigniter

我是codeigniter和php的新手,我现在正遇到这个错误,我不知道我哪里出错了。我应该在我的foreach上添加一些东西吗?我上次遇到的是一个未定义的属性,由于我的模型中的拼写错误的单词现在是一个stdClass而被解决了?这次没有拼错的单词。有人可以用这个问题来启发我吗?

错误:

  

遇到PHP错误

     

严重性:注意

     

消息:未定义的属性:stdClass :: $ blockcode

     

文件名:views / v_schedule.php

     

行号:10

视图:v_schedule.php

<?php foreach ($query as $row){ ?>


                       <?php echo $row->blockcode;?> <br>
                       <?php echo $row->subjectcode;?> <br>
                       <?php echo $row->daystart;?> <br>
                       <?php echo $row->dayend;?>   <br>
                       <?php echo $row->stime;?>    <br>
                       <?php echo $row->sday;?> <br>
                       <?php echo $row->instructorname;?>   <br>
                       <?php echo $row->instuctorlastname;?>    <br>
                       <?php echo $row->roomcode;?> <br>
                       <?php echo $row->building;?> <br>

        <?php } ?>  

controller:c_test.php

 function getSchedule() {
       $data['query'] = $this->m_test->result_getSchedule();
       $this->load->view('v_schedule',$data);
 }

model:m_test.php

function result_getSchedule()

    {
        $this->db->select('grades.studentid', 'grades.blockcode', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
        $this->db->from('grades');
        $this->db->join('subjectblocking', 'grades.blockcode=subjectblocking.blockcode');
        $this->db->join('instructorinfo', 'subjectblocking.instructorid=instructorinfo.instructorid');
        $this->db->join('room', 'subjectblocking.roomcode= room.roomcode');
        $this->db->distinct();
        $this->db->where('studentid', '2013-F0218');
        $query=$this->db->get();
        return $query->result();
    }

3 个答案:

答案 0 :(得分:3)

你犯了一个错误

$this->db->select('grades.studentid', 'grades.blockcode', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');
//this is is not right way codeigniter select.
//it only selects studentid
//blockcode is not seleted that's why you got that error

这是正确的方法

 $this->db->select('grades.studentid , grades.blockcode , subjectblocking.subjectcode , subjectblocking.daystart , subjectblocking.dayend , subjectblocking.stime , subjectblocking.sday , instructorinfo.firstname , instructorinfo.lastname ,  subjectblocking.roomcode , room.building');

希望这能解决你的问题

答案 1 :(得分:0)

成绩和主题块表中有两个块码列。

您应该在m_test.php中更改您的sql:

 $this->db->select('grades.studentid', 'grades.blockcode as block_code', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');

你应该在v_schedule.php中将$ row-&gt; blockcode变量更改为$ row-&gt; block_code

希望它有所帮助。

答案 2 :(得分:0)

首先我也使用了上面的代码,我得到了同样的错误。

<LinearLayout android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:orientation="horizontal" 
          android:background="@drawable/background" 
          android:padding="10dp" >  

<TextView style="@style/TextView_port" 
          android:id="@+id/text" 
          android:text="Text"  />

<RelativeLayout style="@style/LinearLayout" 
                android:layout_below="@id/text" 
                android:layout_margin="10dp" 
                android:gravity="right" >

    <TextView style="@style/TextView_port" 
              android:text="3:59pm" 
              android:textColor="#73B661"  />

</RelativeLayout>

</LinearLayout>

但改变了

$this->db->select('grades.studentid', 'grades.blockcode as block_code', 'subjectblocking.subjectcode','subjectblocking.daystart','subjectblocking.dayend', 'subjectblocking.stime','subjectblocking.sday','instructorinfo.firstname', 'instructorinfo.lastname', 'subjectblocking.roomcode','room.building');

我解决了错误。

如果我们可以将$this->db->select('grades.studentid, grades.blockcode as block_code, subjectblocking.subjectcode,subjectblocking.daystart,subjectblocking.dayend, subjectblocking.stime,subjectblocking.sday,instructorinfo.firstname, instructorinfo.lastname, subjectblocking.roomcode,room.building'); 的单个名称设为grades.studentid,那么我们可以直接调用:

studentID