0

I want to assign some html code to variable and that variable is in controller.

headerview.php

<header id="header" class="header">
        <div class="container">

            <!-- LOGO -->
            <div class="logo"><a href="<?php echo base_url()?>admin/Adminhome"><img src="<?php echo base_url()?>public/images/logo.png" alt=""></a></div>
            <!-- END / LOGO -->
            
                <!-- NAVIGATION -->
                <nav class="navigation">

                    <div class="open-menu">
                        <span class="item item-1"></span>
                        <span class="item item-2"></span>
                        <span class="item item-3"></span>
                    </div>

                    <!-- MENU -->
                    <ul class="menu">
                    
                        <?php if($isAdmin ){ ?>
                        
                            <?php 
                                if($title=='Dashboard'){
                            ?>
                            <li class="current-menu-item"><a href="<?php echo base_url()?>admin/Adminhome">Admin Dashboard</a></li>
                            <?php 
                                }else{
                            ?>
                            <li><a href="<?php echo base_url()?>admin/Adminhome">Admin Dashboard</a></li>   
                            <?php 
                                }
                            ?>
                                
                        <?php } else { ?>   
                            <?php 
                                if($title=='Dashboard'){
                            ?>
                            <li class="current-menu-item"><a href="<?php echo base_url()?>admin/Userhomepage">User Dashboard</a></li>
                            <?php 
                                }else{
                            ?>
                            <li><a href="<?php echo base_url()?>admin/Userhomepage">User Dashboard</a></li> 
                            <?php 
                                }
                            ?>
                        <?php } ?>
                            
                            
                        <?php 
                            if($title=='Courses' || $title=='Add/Edit Categories' || $title=='Upload Manager'){
                            ?>
                        <li class="current-menu-item">
                            <a href="<?php echo base_url()?>admin/Coursedetails/courses">Courses</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Categories">Categories</a></li>
                                <li><a href="<?php echo base_url()?>admin/coursedetails/courses">Courses</a></li>
                                <li><a href="<?php echo base_url()?>admin/uploadmanager">Upload Manager</a></li> 
                            </ul>
                        </li>
                        <?php 
                            }else{
                            ?>
                        <li>
                            <a href="<?php echo base_url()?>admin/Coursedetails/courses">Courses</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Categories">Categories</a></li>
                                <li><a href="<?php echo base_url()?>admin/coursedetails/courses">Courses</a></li>
                                <li><a href="<?php echo base_url()?>admin/uploadmanager">Upload Manager</a></li> 
                            </ul>
                        </li>
                        <?php 
                            }
                            ?>
                        <?php 
                            if($title=='Users' || $title=='User Roles' || $title=='Subscription'){
                            ?>
                        <li class="current-menu-item">
                            <a href="<?php echo base_url()?>admin/Users">Users</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Roles">Roles</a></li>
                                <li><a href="<?php echo base_url()?>admin/Subscription">Subscription</a></li>
                            </ul>
                        </li>
                        <?php 
                            }else{
                            ?>
                         <li>
                            <a href="<?php echo base_url()?>admin/Users">Users</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Roles">Roles</a></li>
                                <li><a href="<?php echo base_url()?>admin/Subscription">Subscription</a></li>
                            </ul>
                        </li>
                        <?php 
                            }
                            ?>
                        <?php 
                            if($title=='Sales' || $title=='Payout Details'){
                            ?>
                        <li class="current-menu-item">
                            <a href="<?php echo base_url()?>admin/Transaction">Sales</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Transaction">Transactions</a></li>
                                <li><a href="<?php echo base_url()?>admin/Transaction/payout">Payouts</a></li>
                            </ul>
                        </li>
                        <?php 
                            }else{
                            ?>
                        <li>
                            <a href="<?php echo base_url()?>admin/Transaction">Sales</a>
                            <ul class="sub-menu">
                                <li><a href="<?php echo base_url()?>admin/Transaction">Transactions</a></li>
                                <li><a href="<?php echo base_url()?>admin/Transaction/payout">Payouts</a></li>
                            </ul>
                        </li>
                        <?php 
                            }
                            ?>
                    </ul>
                    <!-- END / MENU -->

                    <!-- LIST ACCOUNT INFO -->
                    <ul class="list-account-info">

                        <!-- MESSAGE INFO -->
                      
                        <!-- END / MESSAGE INFO -->

                        <!-- NOTIFICATION -->
                        
                        <!-- END / NOTIFICATION -->

                        <li class="list-item account">
                                <?php 
                                   $userId=$this->session->userdata('cp_adminid');
                                   $profilePic="";
                                   $userQ=$this->Adminmodel->getuser();
                                   foreach ($userQ->result() as $rowuser){
                                    $profilePic=$rowuser->profilePic;
                                    }
                                    ?>
                            <div class="account-info item-click">
                               
                                <?php if($profilePic!=''){ ?> 
                                    <img alt="" src="<?php echo base_url();?>private/<?php echo $userId;?>/<?php echo $profilePic;?>">
                                    <?php }else{?>
                                    <img src="<?php echo base_url()?>public/images/default-profile_pic.png" alt="">
                                    <?php }?>
                            </div>
                            <div class="toggle-account toggle-list">
                                <ul class="list-account">
                                    <li><a href="<?php echo base_url()?>admin/settings/"><i class="icon md-config"></i>Setting</a></li>
                                    <li><a href="<?php echo base_url()?>admin/Adminlogout"><i class="icon md-arrow-right"></i>Sign Out</a></li>
                                </ul>
                            </div>
                        </li>


                    </ul>
                    <!-- END / LIST ACCOUNT INFO -->

                </nav>
                <!-- END / NAVIGATION -->               
        </div>
    </header>

This header is come to all pages in application. So I want it to assign some variable called headerContent and it is located in Admin_controller. That render function called from any another controller that extends Admin_controller and then it loads the headeview to that pages.

Admin_controller

class Admin_controller extends CI_Controller{
function __construct()
{
    parent::__construct();
    $this->load->model("Adminmodel","",true);

    $headerview = 'headerview';
    $this->render($headerview); # calling render() function in same class

}

protected $headerview = 'headerview';
    protected function render($headerContent) { 
        $view_data = array( 'headerContent' => $headerContent);
        $this->load->view($this->headerview);
    }

}

Another controller below that extends Admin_controller called this render function.

Adminhome_controller.php

<?php
require APPPATH . '/controllers/admin/Admin_controller.php';

class Adminhome extends Admin_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->model("Adminmodel","",true);
        
        public function index(){
        
        $data['content']=$this->load->view("admin/adminhomeview",'',true);
        $data['title']='Dashboard';
        $adminId=$this->session->userdata('cp_adminid');
        $permissions=$this->Adminmodel->getpermissions($adminId);
       
       
        $row = $permissions->row();
        
        $data['isAdmin'] = 0;
        if($row->view == 1 or $row->add == 1 or $row->edit == 1 or $row->deleteRole == 1 ){
            $data['isAdmin'] = 1;
        }
        
       $this->render($headerContent);
       
      // $this->load->view("admin/headerview",$data);
        
    }
}
?>

But it showing error like Message: Undefined variable: headerContent in Adminhome_controller.php.

3
  • You're not defining the variable $headerContent before you're trying to use it in your Adminhome_Controller. Commented Feb 21, 2017 at 8:02
  • From where you are getting value in $headerContent? I think you may be calling instance variable as a local variable is your issue. Call like $this->headerContent Commented Feb 21, 2017 at 8:04
  • I want assign that <header></header> tag to headerContent variable present in headerview.php. Commented Feb 21, 2017 at 8:15

2 Answers 2

1

you culd create a template view file containing

template view:

   <html>
<?php  $data = array('permission' => $permission); ?>
         <?php $this->load->view('templateincludes/header',$data); ?>


                        <?php $this->load->view($main_content); ?>  


             <?php $this->load->view('templateincludes/footer'); ?>  
    </html>

controler:

 $data = array('permission' => '1', 'main_content' => 'folder_to_your_view/view_file_you_want_to_load');

 $this->load->view('template', $data);

header view:

<?= $permission ?>
Sign up to request clarification or add additional context in comments.

Comments

0

But it showing error like Message: Undefined variable: headerContent in Adminhome_controller.php

There is no variable with the name $headerContent inside extended class. Seems like you want access the parent/base class's variable.

To overcome this, you can declare protected field inside Parent Controller (Admin_controller.php), and access it inside child controller :

class Admin_controller extends CI_Controller{

  protected $headerContent; // declare protected field
  protected $headerview = 'headerview';

  function __construct()
  {
     parent::__construct();
     $this->load->model("Adminmodel","",true);

     $headerview = 'headerview';
     $this->render($headerview); # calling render() function in same class

  }      

  protected function render($headerContent) { 
     $view_data = array( 'headerContent' => $headerContent);
     $this->headerContent = $headerContent; // assign value into $headerContent field
     $this->load->view($this->headerview);
  }

}

And you can access it directly inside child class (Adminhome_controller.php) by using :

$this->render($this->headerContent);

1 Comment

I want assign that <header></header> tag to headerContent variable present in headerview.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.