Playing Controller Info for View CI3

0

I'm working with CodeIgniter3 in the MVC model and I'm having problems for a long time, slowly I'm solving some problems here and there and almost reaching the point.

I'm doing a query in random order in the DB by the model:

public function get_rand(){

$frase = $this->db->query("SELECT frase FROM frases ORDER BY RAND() LIMIT 1");
if($frase->num_rows() > 0):
    return $frase->result();
else:
    return NULL;
endif;

}

and pulling inside the Controller:

public function index(){
    if($frase = $this->randfrase->get_rand(1)):
            foreach ($frase as $linha) :
                    echo $linha->frase;
            endforeach;
    else:
            return NULL;
    endif;

}

I mounted it so I could see by URL whether the Random selection was working. The part of the Model is 100%, now I wanted to know how I can instead of printa, send the info to a view aside of my system. Someone could help. In case this information should appear in an aside that is standard for the whole site. I tried to put this controller code right in the view but it always gives the error:

  

A PHP Error was encountered

     

Severity: Notice

     

Message: Undefined property: CI_Loader :: $ randfrase

     

Filename: views / aside.php

     

Line Number: 3

     

Backtrace:

     

File: C: \ xampp \ htdocs \ codeigniter \ site \ application \ views \ aside.php   Line: 3 Function: _error_handler

     

File: C: \ xampp \ htdocs \ codeigniter \ site \ application \ views \ home.php   Line: 27 Function: view

     

File:   C: \ xampp \ htdocs \ codeigniter \ site \ application \ controllers \ page.php   Line: 14 Function: view

     

File: C: \ xampp \ htdocs \ codeigniter \ site \ index.php Line: 315 Function:   require_once

     

Fatal error: Call to a member function get_rand () on a non-object in   C: \ xampp \ htdocs \ codeigniter \ site \ application \ views \ aside.php on line 3   A PHP Error was encountered

     

Severity: Error

     

Message: Call to a member function get_rand () on a non-object

     

Filename: views / aside.php

     

Line Number: 3

     

Backtrace:

    
asked by anonymous 10.04.2018 / 02:29

1 answer

0

I've never used such a aside , but it seems to be a normal view at the end of the day.

Well .. what I think I understood was that you are not calling the model correctly, so see if it helps you in anything.

Let's assume your model is called Randfrase and your aside is called aside only.

So, in my opinion, you have to stay like this in your controller:

public function index () {

if($frase = $this->randfrase->get_rand(1)){
    //carrega a model
    $this->load->model('randfrase'); 

    //utiliza o método da model
    $dados['frase'] = $this->randfrase->get_rand(); 

    //sua aside seria uma view no meu exemplo que recebe o array
    $this->load->view('aside',$dados);
}            

}

Now in your aside view you do more or less what you tried to do before:

foreach ($frase as $linha) :
    echo $linha->frase;
endforeach;

I hope it's more or less what you need .. good luck.

    
17.04.2018 / 21:11