Code Igniter(类stdClass的对象无法转换为字符串)erro

时间:2016-04-29 00:46:57

标签: php mysql arrays string codeigniter

这是我的模特:

function Countosweb(){
    $sql="SELECT count(*) FROM `os` WHERE `status`='PENDIENTE ASIGNAR TÉCNICO'";
    return $this->db->query($sql)->row();
}

这是我的控制者:

public function indexp() {
    if((!$this->session->userdata('session_id')) || (!$this->session->userdata('logado'))){
        redirect('mapos/login');
    }
    $this->data['ordensW'] = $this->mapos_model->getOsWeb();
    $this->data['osn']= $this->mapos_model->Countosweb($sql);
    $this->data['ordens'] = $this->mapos_model->getOsAbertas();
    $this->data['produtos'] = $this->mapos_model->getProdutosMinimo();
    $this->data['os'] = $this->mapos_model->getOsEstatisticas();
    $this->data['estatisticas_financeiro'] = $this->mapos_model->getEstatisticasFinanceiro();
    $this->data['menuPainel'] = 'Painel';
    $this->data['view'] = 'mapos/panel';
    //$this->session->set_flashdata('success','mensaje de prueba');
    $this->load->view('tema/alte',$this->data);

}

这是我的观点:

<a href="#" class="dropdown-toggle" data-toggle="dropdown">
                <i class="fa fa-bell-o"></i>
                <?php
                if($osn != null){echo '<span class="label label-warning">'.$osn.'</span>';} // this is the line which is occasinating the problem.
                ?>
           </a>

所以我收到了这个错误:

PHP错误已经遇到

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename:tema/alte.php

line Number: 67

请帮助找到问题

PD:打扰一下,我的英语不是很好

3 个答案:

答案 0 :(得分:0)

你不应该使用echo来打印Object - 普通情况下的返回值是object或arrayuse:

 print_r($osn) ; 

查看输出然后遍历它或使用对象[$ object_name-&gt; item]

答案 1 :(得分:0)

我建议您将函数更改为此函数以获取返回的行数。

function Countosweb(){
    $sql="SELECT * FROM `os` WHERE `status`='PENDIENTE ASIGNAR TÉCNICO'";
    return $this->db->query($sql)->num_rows();
}

然后你的html应该是这样的。

<a href="#" 
   class="dropdown-toggle" 
   data-toggle="dropdown">

   <i class="fa fa-bell-o"></i>
   <?php
       if(isset($osn)){
           echo '<span class="label label-warning">'.$osn.'</span>';
       } // this is the line which is occasinating the problem.
   ?>
</a>

答案 2 :(得分:0)

感谢所有人帮助我,我解决了它:

我改变了这个:

<a href="#" class="dropdown-toggle" data-toggle="dropdown">
            <i class="fa fa-bell-o"></i>
            <?php
            if($osn != null){echo '<span class="label label-warning">'.$osn.'</span>';} // this is the line which is occasinating the problem.
            ?>   
</a>

为此:

<a href="#" class="dropdown-toggle" data-toggle="dropdown">
                <i class="fa fa-bell-o"></i>
                <?php
                if($osn != null){
                  foreach($osn as $on){echo'<span class="label label-warning">'.$on.'</span>';} 
                }
                ?>
</a>