Welcome Guest, Not a member yet? Register   Sign In
Use a model query in a view
#1

[eluser]Xtrex[/eluser]
Hi, Iam using a model to preform a query:
Code:
function noticias_url($uri,$tipo) {
        $query = $this->db->query('SELECT * FROM noticias WHERE uri = "'. $uri .'" AND tipo = "'. $tipo .'"' );
        return $query->result();    
    }


How can I use the information passed by this controller in the "noticias" view? And how can I use $query->num_rows()?


Code:
$this->load->model('noticiar', '', TRUE);
        $data['noticias'] = $this->noticiar->noticias_url($uri,1);
        $this->load->view('noticias', $data);
#2

[eluser]Yash[/eluser]
Read [url="http://ellislab.com/codeigniter/user-guide/database/active_record.html"]user guide - All database functions are here with less coding[/url]
#3

[eluser]Xtrex[/eluser]
I can use all the db functions, and everything that is displayed in the user guide.. But I cant retrieve de data of a model to the view.. I only can do if I perform the query in the controller..
#4

[eluser]Xtrex[/eluser]
with the print_r it returns:

Array ( [0] => stdClass Object ( [cod_noticia] => 1 [titulo] => isto é um teste [descricao] => descricao [noticia] => noticia [tags] => paulo, hehe [data] => [uri] => isto-e-um-teste [tipo] => 1 ) )

I cant used like an array, how can I use it?
#5

[eluser]Hannes Nevalainen[/eluser]
If you would like to use num_rows() you have to return $query (not $query->result()).

If you want to show your data in your view you will get it like this:
Code:
//View
<?php echo $noticias->field_name;?>




Theme © iAndrew 2016 - Forum software by © MyBB