Codeigniter -> Message: Trying to get Property of non-object

Asked

Viewed 3,030 times

0

I’m trying to fix this bug:

Severity: Notice Message: Trying to get Property of non-object Filename: contracts/view.php Line Number: 37

CODE:

contratos_model.php

public function getById($id){

    $this->db->where('idContrato',$id);
    $this->db->limit(1);
    $consulta = $this->db->get('contrato')->result();

            foreach($consulta as &$valor){
                    $this->db->where('idCliente', $valor->idCliente);
                    $valor->curso = $this->db->get('cliente')->result();
            }

            return $consulta;

}

php contracts.

public function visualizar(){

    if(!$this->permission->checkPermission($this->session->userdata('permissao'),'vContrato')){
       $this->session->set_flashdata('error','Você não tem permissão para visualizar Contratos.');
       redirect(base_url());
    }

    $this->data['custom_error'] = '';
    $this->data['result'] = $this->contratos_model->getById($this->uri->segment(3));
    $this->data['view'] = 'contratos/visualizar';
    $this->load->view('tema/topo', $this->data);

}

visualize.php

<?
    print_r($result);
?>

return of the viewer print_r

Array ( [0] => stdClass Object ( [idContrato] => 3 [idCliente] => 3 [idCedente] => 2 [forma_de_pagamento] => 96 [nota_fiscal] => 94 [fechamento_de] => 1 [fechamento_a] => 1 [vencimento] => 1 [guias] => 0 [status] => 0 [curso] => Array ( [0] => stdClass Object ( [idCliente] => 3 [razaosocial] => PIXX CRIATIVA SOLUCOES LTDA [nomefantasia] => PIXX CRIATIVA SOLUCOES LTDA [cnpj] => 0655544801120 [ie] => 0 [im] => 0 [endereco] => [endereco_numero] => 0 [endereco_complemento] => [endereco_bairro] => [endereco_cep] => 0 [endereco_cidade] => [endereco_estado] => 12 [responsavel] => André Baill [responsavel_phone_ddd] => 41 [responsavel_telefone] => 96479364 [responsavel_celular_ddd] => 0 [responsavel_celular] => 0 [email] => [data_ativo] => 1970-01-01 [codCedente] => 4 [fat_endereco] => [fat_endereco_numero] => 0 [fat_endereco_complemento] => [fat_endereco_bairro] => [fat_endereco_cep] => 0 [fat_endereco_cidade] => [fat_endereco_estado] => 12 [fat_responsavel] => [fat_telefone_ddd] => 0 [fat_telefone] => 0 [fat_celular_ddd] => 0 [fat_celular] => 0 [email_financeiro] => [vencimento] => 1 [nota] => 94 [fechamento_de] => 1 [fechamento_a] => 1 [guias] => 0 [valor_moto_normal] => 0 [valor_moto_metropolitano] => 0 [valor_moto_depois_18] => 0 [valor_moto_km] => 0 [valor_carro_normal] => 0 [valor_carro_metropolitano] => 0 [valor_carro_depois_18] => 0 [valor_carro_km] => 0 [valor_caminhao_normal] => 0 [valor_caminhao_metropolitano] => 0 [valor_caminhao_depois_18] => 0 [valor_caminhao_km] => 0 [status] => 0 ) ) ) )

I also cheated on the Pastebin

I’ve been trying for days and I can’t print the data properly.

Tend to give a echo $result->campo
Ai gives the message above.

  • Which version of the IC is?

  • The version is version 2.1.3

  • Could evidence the line that error occurs?

  • Line 37: <td><? php echo $result->idContract; ? ></td>

  • have tried echo $result[0]->campo

  • How do I retrieve the data that was printed in the $result[client]?

  • I didn’t understand the pq of that foreach in the getById(), think q da to simplify a little.

  • The final function was like this: and it worked.

  • 2

    Why a foreach if you gave a limit 1? Uses Row() instead of result();

  • @Andrébaill, enjoy and see how the website works it is different from a forum.

Show 5 more comments

5 answers

0

Try this:

print_r($result[0]->field);

Has another object nested in the same way inside the client array, so access:

$result[0]->client[0]->field

  • Returned the data from the Array, normal... like the one I pasted above.

  • 2

    I tried using the $result[0]->field, and it worked. This means that all data was plotted in an array... And to recover, I use point 0. I really appreciate the attention, I stayed all weekend in this and I couldn’t get it... Thank you Gentlemen.

0


Your object is inside an array item, in which case you can call it directly by the key.

It seems to me that here is made his definition:

$this->data['result'] = this->contratos_model->getById($this->uri->segment(3));

In your view you call it that way:

echo $result[0]->idContrato;

0

The final version of getById looks like this public Function getById($id){

    $this->db->where('idContrato',$id);
    $this->db->limit(1);
    $consulta = $this->db->get('contrato')->result();


    foreach($consulta as &$valor){
        $this->db->where('idCliente', $valor->idCliente);
        $valor->cliente = $this->db->get('cliente')->result();

        $sql = "SELECT 
                    cf.*, 
                    f.idFuncionario, 
                    f.nome as nomeFuncionario 
                FROM 
                    contrato_funcionario as cf, 
                    funcionario as f 
                WHERE 
                    cf.idContrato = '".$valor->idContrato."'
                AND
                    cf.idFuncionario = f.idFuncionario 
                GROUP BY 
                    f.idFuncionario";
        $valor->funcionarioLista = $this->db->query($sql)->result();
    }


    return $consulta;

}

And it worked.

0

You can also use in the query:

$valor->cliente = $this->db->get('cliente')->row();

Then you take the value directly from a single line. O ->result(); invariably generates an array indexed by numeric values: [0], [1], etc.

0

You are returning via array so in this case you need to use the loop in array for example using while instead of foreach, or else return string to loop with foreach. ex: Return $query->result();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.