douzhun8615 2019-05-08 23:09
浏览 70
已采纳

在我调用函数的控制器上使用模型函数的可变数据

So I have a website where I buy items. I do an if to see if the session data "moedas" (user's coins) is greater than or equal to the item's price. I want to get the item's data from my model where I have the model's function dados_acessorio. So I call this function on my controller and what I want is to receive that data and be able to use it on the same controller after. The problem is I am getting an error. I am new to php/CodeIgniter and I could use some help.

Here is the controller's function:

public function confirmarCompra($id){

     $this->load->model('acessorios_model','acessorios');
     $dados['acessorio'] = $this->acessorios->dados_acessorio($id)[0];

     if($this->session->moedas >= $acessorio['preco']){


     }
}

Here is the model's function:

public function dados_acessorio($id){

            return $this->db->from('acessorios')->where('id_acessorio', $id)->get()->result_array();
}

Here is an image of my acessorios table:

https://imgur.com/tth9mKS

The error that I receive on the controller:

https://imgur.com/BdR2ZMq

The error is on the following line: if($this->session->moedas >= $acessorio['preco'])

Thanks!

  • 写回答

1条回答 默认 最新

  • douwen2072 2019-05-09 01:25
    关注

    Looks like it could be a typo? $acessorio is how it will appear in the view.

    change this:

    if($this->session->moedas >= $acessorio['preco']){
    

    to this

    if($this->session->moedas >= $dados['acessorio']['preco']){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?