Returning data with SQL queries

0

Hello, I have a table called notebooks and news. I need to return 5 news from each notebook respecting the order of presentation:

Model:

//metodo retorna os dados do carderno
public function get_caderno ($idcaderno = null) {

    $this->db->select('Id, Nome, Url');
    $this->db->where('Status', 1);
    $this->db->order_by('Ordem','ASC');
        $query = $this->db->get('caderno');
        if($query->num_rows() > 0) {
            return $query->result();
        }
        else {
            return NULL;
        }

}    

//metodo que retorna as noticias por caderno
public function get_all_home($limit = null, $areahomenoticia = null , $ordem = null, $caderno = null)   {
    if ($limit > 0) {
        $this->db->select('noticias.Id as NoticiaId, DATE_FORMAT(noticias.Data, "%d/%m/%Y") as NoticiaData, noticias.Titulo as NoticiaTitulo, noticias.Imagem as NoticiaImagem, caderno.Nome as CadernoNome, caderno.CSSClass as CadernoCSS, noticias.IdArea as AreaNoticia');
        $this->db->from('noticias');
        $this->db->join('caderno', 'caderno.Id = noticias.IdCaderno','inner');
        $this->db->where('noticias.IdArea',$areahomenoticia);

        //verifica se exite ordem de apresentação da notícia
        if ($ordem) {
            $this->db->order_by('noticias.Ordem', 'asc');
            $this->db->order_by('noticias.Id', 'desc');             
        } 
        else {
            $this->db->order_by('noticias.Id', 'desc');
            $this->db->order_by('noticias.Ordem', 'asc');
        }


        $this->db->LIMIT($limit);
        $query = $this->db->get();                          
        if($query->num_rows() > 0) {
            return $query->result() ;
        }
        else {
            return NULL;
        }
    }
    else  {
        $this->db->order_by('id','desc');
        $query = $this->db->get('noticias', $limit);
        if($query->num_rows() > 0) {
            return $query->num_rows();
        }
        else {
            return NULL;
        }
    }                       
}

I need to return this query in an array where the index is the name or id of the notebook table and in each index the value will be an array coming from the database with the news values per notebook.

Some solution. Thank you.

    
asked by anonymous 26.09.2018 / 17:41

0 answers