Using variables in the Model

Asked

Viewed 86 times

0

Well, I have the following function in my Model :

public function get_learning_category_list() {
     $categorias = $this->db->select('t1.id, t1.title, t1.metadata, t1.meta_title, t1.description, t1.meta_description, t1.meta_spam, t1.url, t1.alt_img')
        ->from('learning_category t1')
        ->join('learning_rel_category t4', 't1.id = t4.category_id', 'left')
        ->join('learning t2', 't4.learning_id = t2.id', 'left')
        ->join('learning_rel_language t3', 't2.id = t3.learning_id', 'left')
        ->where('t3.language', $this->language)
        ->group_by('t1.id')
        ->get()->result_array();
    foreach($categorias as $c){
        $total = $this->db->select('count(learning_rel_language.learning_id) as total', false)
        ->from('learning')
        ->join('learning_rel_language', 'learning.id = learning_rel_language.learning_id', 'inner')
        ->join('learning_rel_category', 'learning.id = learning_rel_category.learning_id', 'inner')
        ->where('learning_rel_category.category_id', $c['id'])
        ->where('learning_rel_language.language', $this->language)
        ->group_by('learning_rel_language.language')->get()->first_row('array');

        $metadata = json_decode($c['metadata']);
        $tt = $c['title'];
        $url = $c['url'];
        if($this->language != 'pt_br'){
            $tt = $metadata->{$this->language};
            $tt_pt_br = $c['title'];
        }

        $return[] = array('url' => $url, 'title'=>$tt, 'id'=>$c['id'], 'total'=>$total['total'], 'title_pt_br'=>$tt_pt_br);

    }

    return $return; //$categorias;
}

Giving a print_r($this->learn->get_learning_category_list()); results me in this value :

Array ( [0] => Array ( [url] => [title] => Cursos sobre como gerar leads qualificados [id] => 2 [total] => 6 [title_pt_br] => )

Beauty? But I need to use (in the Model itself) these values, url title and etc. How can I do this?

Being more specific, I need to use here :

$category_format = (Aqui url title, ou seja : Cursos sobre como gerar leads qualificados ).'-cmdo-'.(Aqui o id, ou seja :2);.

3 answers

1


I solved using:

 $categorias = $this->learn->get_learning_category_list();
    foreach($categorias as $cat){
        $cat = (object) $cat;
 if($cat->title != '') {
            $return = strtolower(url_title($cat->title)).'-cmdo-'.$cat->id;
        }else{
            $return = 'cursos-de-marketing-digital-online-'.$cat->id;
        }   
    return $return;   

0

if your Return is returning the values of the function you can use it within the view and give a print_r to see which values are returned within the view, otherwise create a variable to store the return of the function and load the view by passing it $this->load->view('nome_view', $dados_funcao)

  • I think you misunderstood my question, I added a few things to the question, showing where I need to use these values.

  • try to use get()->row(); in place of get()->result_array();

0

It is not clear to me what the purpose is to store the return of this function back in your Model.

But you can do this by creating a property in the class of your Model to store the data you will use.

<?php

class Meu_Model {

    protected $learning_category_list;

    // ...

    public function get_learning_category_list() {

         // ...

        $this->learning_category_list = $return;            

        return $return;
    }
}

So after executing $this->learn->get_learning_category_list(), within your class you can use $this->learning_category_list.

Again, if you inform in more detail why you want to do it this way it is clearer to suggest a better solution.

  • Thank you for the answer. I want to set the url format using these values, like this : $category_format = (Aqui url title, ou seja : Cursos sobre como gerar leads qualificados ).'-cmdo-'.(Aqui o id, ou seja :2); Got it ?

  • @GWER remains confused to me why this needs to be inside your Model.

  • So I’m creating a check to see if the url format is correct, and if it’s not, I redirect it to the correct url. This check is in my Controller, okay? if($this->uri->uri_string != $this->learn->url_format_category()) {&#xA; redirect($this->learn->url_format_category(),'location','301');&#xA; exit;. In the model I just need to set the CORRECT format of this url. I was clear?

  • @GWER with his comment made it clearer what you want to do. Edit your question explaining better what you want to do, because the question remains meaningless (the title for example).

Browser other questions tagged

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