Welcome Guest, Not a member yet? Register   Sign In
Dynamic load view in CodeIgniter
#1

[eluser]greedyman[/eluser]
I have a structure as below, it's in views folder:
Quote:views
--user folder
----include folder
--------header.php
--------footer.php
----welcome.php
----about.php
----account.php
----etc.php
I include header.php and footer.php into every other file in user folder (about.php, account.php, ...etc). In user controller I put some codes in default method:
Code:
public function index() {
            $this->load->model('product_model');
            
            $data['categories'] = $this->product_model->get_all_category();
            $data['new_products'] = $this->product_model->get_new_products();
            $data['sale_products'] = $this->product_model->get_sale_products();
            $data['random_products1'] = $this->product_model->get_random_products();
            $data['random_products2'] = $this->product_model->get_random_products();
            
            $this->load->view('user/welcome', $data);
        }
Of course, it's working normally. My problem here is, I have a lot of page and it means that I have to write again code many times. Example:
Code:
public function account() {
            $this->load->model('product_model');
            
            $data['account'] = $this->product_model->get_account();
            $data['new_products'] = $this->product_model->get_new_products();
            $data['sale_products'] = $this->product_model->get_sale_products();
            $data['random_products1'] = $this->product_model->get_random_products();
            $data['random_products2'] = $this->product_model->get_random_products();
            
            $this->load->view('user/account', $data);
        }

        public function details() {
            $this->load->model('product_model');
            
            $data['details'] = $this->product_model->get_details();
            // used for footer.php
            $data['new_products'] = $this->product_model->get_new_products();
            $data['sale_products'] = $this->product_model->get_sale_products();
            $data['random_products1'] = $this->product_model->get_random_products();
            $data['random_products2'] = $this->product_model->get_random_products();
            // end
            
            $this->load->view('user/details', $data);
        }
// etc...
I have other way, I don't use parse parameter and put directly code into footer.php:
Code:
<div class="right_box">
                <div class="title">
                    <span class="title_icon">
                            <img src="&lt;?php echo base_url();?&gt;assets/images/bullet5.gif" alt="" title="" />
                    </span>
                    Random products
                </div> &lt;!-- end .title --&gt;

                <ul class="list">
                    &lt;?php
                        $getRamdomProducts1 = mysql_query("SELECT * FROM product ORDER BY RAND() LIMIT 11");
                        while ($row1 = mysql_fetch_assoc($getRamdomProducts1)) {
                    ?&gt;
                    <li>
                        <a href="">&lt;?php echo $row1['name']; ?&gt;</a>
                    </li>
                    &lt;?php
                        }
                    ?&gt;                                            
                </ul>

                <div class="title">
                    <span class="title_icon">
                            <img src="&lt;?php echo base_url();?&gt;assets/images/bullet6.gif" alt="" title="" />
                    </span>
                    Random products
                </div> &lt;!-- end .title --&gt;

                <ul class="list">
                    &lt;?php
                        $getRamdomProducts2 = mysql_query("SELECT * FROM product ORDER BY RAND() LIMIT 10");
                        while ($row2 = mysql_fetch_assoc($getRamdomProducts2)) {
                    ?&gt;
                    <li>
                        <a href="#">&lt;?php echo $row2['name']; ?&gt;</a>
                    </li>
                    &lt;?php
                        }
                    ?&gt;
                </ul>
            </div> &lt;!-- end .right_box --&gt;
I think this way is not nice, have you other solution?
#2

[eluser]greedyman[/eluser]
I searched on google and found some solutions, the simplest way is use global variable. Everybody, thank for watching!




Theme © iAndrew 2016 - Forum software by © MyBB