Rescue controller data in multiple views

Asked

Viewed 125 times

0

I’m developing a website in Codeigniter in which all pages must have a footer dynamic with data coming from the bank.

I charged the view footer (I created a controller footer) in the controller initial that carries a layout standard (which goes on all pages) and the content of view initial and worked well, but the other pages do not appear.

I would like, if possible, suggestions on how to do this, how to pass data from a controller for several views.

Index function of the initial controller.

    public function index()
{

    ################### DESTAQUE ########################
    //$sql2['not_in']       =   array('field'   => 'id',    'search'    =>  $data['destaque1'][0]['id']);
    $sql1['not_secure']     =   FALSE;
    $sql1['where']          =   array(
        'status'    =>  1,  
        "arquivo != " => "''");
    //$sql1['where']            =   array('status'  =>  1,      'destaque'  =>  1,  "arquivo != " => "''", 'data_pub <= ' => "'".date("Y-m-d H:i:s")."'");
    $sql1['order']          =   array(
            'field' =>  'ci_vitrines.data',
            'hang'  =>  'DESC'
    );
    $sql1['limit']          =   3;
    // core/MY_Controller.php
    $data['vitrines']       =   $this->get_data('vitrines', NULL, $sql1);


    ################### UNIDADES ########################

    $sql2['where']          =   array(
            'status'    =>  1,
            'tipo'      =>  1
    );

    $sql2['order']          =   array(
            'field'     =>  'data',
            'hang'      =>  'DESC'
    );

    $sql2['limit']          =   14;

    $data['unidades']       =   $this->get_data('unidades', NULL, $sql2);

    ################### FRASES ASSOCIE-SE #######################

    $sql3['where']          =   array(
            'status'    =>  1
    );

    $sql3['order']          =   array(
            'field'     =>  'data',
            'hang'      =>  'DESC'
    );

    $sql3['limit']          =   4;

    $data['frases']         =   $this->get_data('frases', NULL, $sql3);

    ################### ULTIMAS NOTICIAS ########################
        /*$sql2['where']            =   array(
                'ci_noticias.status'    =>  1,
                'ci_noticias.destaque'  =>  2
        );
        $sql2['order']          =   array(
                'field' =>  'ci_noticias.data',
                'hang'  =>  'DESC'
        );
        $sql2['limit']          =   5;
        // core/MY_Controller.php
        $data['last_noticias']  =   $this->get_data('noticias', NULL,   $sql2);*/


    $this->site_template_load('layout', $this->_namemodel, $data);

}

Function site_template_load

    public function site_template_load($template = '', $view = '', $view_data = array(), $return = FALSE)
{
    if($this->check_painel() === FALSE) // SITE
    {
        if(isset($_GET['versaoclassica'])) session("versaoclassica", 1);
        $viewatual = ($this->agent->is_mobile() && !session("versaoclassica")) ? "site" : "site";

        if(file_exists(APPPATH.'views/'.$viewatual.'/' . $template . '.php'))
        {
            $path_template = $viewatual.'/' . $template;
        }
        else
        {
            $path_template = 'demo/' . $template;
        }

        if(file_exists(APPPATH.'views/'.$viewatual.'/' . $view . '.php'))
        {
            $path_views = $viewatual.'/' . $view;
        }
        elseif(file_exists(APPPATH.'views/demo/' . $view . '.php'))
        {
            $path_views = 'demo/' . $view;
        }
        else
        {
            show_404();
        }
    }

    $this->template_set('contents', $this->load->view($path_views, $view_data, TRUE));
    return $this->load->view($path_template, $this->template_data, $return);
}
  • What have you tried? Received an error message? If possible, post a snippet of the code you believe is the source of the problem

  • If you want the footer to be dynamic and change depending on the VIEW which is called this is not work for a controller, and yes to a helper.

  • By the way, if you show how you are doing this will help to have ideas on how to improve your code ;)

  • I’ve actually started testing options now. I have a default layout, which is loaded on all pages, and the internal of each page is mounted depending on what it will show. I tried using include in the layout, in the initial view showed the data, but in the others not. The return value is null.

  • How would that be done on a helper? It doesn’t need code itself, I’m more looking for an idea of how to do this and why in the initial view the content was loaded, and in the others not, although all the views use the same layout file.

  • I know you’re looking for the concept, but understand: if I see your code I’ll know what you’re calling it layout standard; whether you are using direct loading or by template, etc. Just to hear you speak, it becomes difficult, because there are numerous possibilities, you understand? What is certain is that your solution goes through helper, I only have to know how to adapt in your way of carrying this layout.

  • Show the controller who carries the views with that layout, Here comes the answer ;)

  • I’ll edit the answer with the codes.

  • template_set() and site_template_load() are your methods, right? You created them to try to load the layout standard in all views, correct?

  • They’re my methods. They actually work perfectly. The only question would be the passage of the parameters to all views, because if it were static, it would have no problem.

  • Personally, I don’t think you need to invent a mechanism to carry views. There’s already a library for that see here. I’ll try to give you an answer with helper, but I couldn’t understand this method of loading.

  • Interesting library, I’ll take a closer look later. In fact these methods already existed and were used here in the company, I only started to use it when I was hired. I await your reply and thank you for your attention.

Show 7 more comments
No answers

Browser other questions tagged

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