Welcome Guest, Not a member yet? Register   Sign In
Common header problem
#1

[eluser]ravindral[/eluser]
I am new to CI. Developing first site. I have header that is common for home page and inner page. Now in header i want to display some records from database. They are visible on home page but not in inner page.

In welcome controller i have written
//////////////////////////////////
function index()
{
$data = array('title' => 'My first CI site');

$this->load->model('subject_model');
$subject_Result=$this->subject_model->subjects();
if($subject_Result->num_rows()>0)
{
$data['subject_result']=$subject_Result->result();
}

$this->parser->parse('header',$data);
$this->parser->parse('home',$data);
$this->parser->parse('footer',$data);
}
//////////////////////////////////

On home page, it is showing list of subjects fetched in subject_model->subjects() function. But in inner page i am getting error :

/////////////Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subject_result
Filename: views/header.php
Line Number: 42
///////////////


Is that i need to copy below code in each page controller?


$this->load->model('subject_model');
$subject_Result=$this->subject_model->subjects();
if($subject_Result->num_rows()>0)
{
$data['subject_result']=$subject_Result->result();
}

Please help otherwise i will code in wrong way. Help me.

Thanks in anticipation,
Ravindra.
#2

[eluser]slowgary[/eluser]
Hello Ravindra, welcome to the forums! I'm reposting your post with [ code ] tags so it's easier to read (and thus, easier for people to help you).



Original post below

===================================================================

I am new to CI. Developing first site. I have header that is common for home page and inner page. Now in header i want to display some records from database. They are visible on home page but not in inner page.

In welcome controller i have written
Code:
//////////////////////////////////
        function index()
    {
        $data = array('title' => 'My first CI site');            
            
        $this->load->model('subject_model');
        $subject_Result=$this->subject_model->subjects();
        if($subject_Result->num_rows()>0)
        {
            $data['subject_result']=$subject_Result->result();
        }

        $this->parser->parse('header',$data);
        $this->parser->parse('home',$data);
        $this->parser->parse('footer',$data);
    }
//////////////////////////////////

On home page, it is showing list of subjects fetched in subject_model->subjects() function. But in inner page i am getting error :

Code:
/////////////[b]Error:[/b]
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subject_result
Filename: views/header.php
Line Number: 42
///////////////

Is that i need to copy below code in each page controller?

Code:
$this->load->model('subject_model');
$subject_Result=$this->subject_model->subjects();
if($subject_Result->num_rows()>0)
{
     $data['subject_result']=$subject_Result->result();
}

Please help otherwise i will code in wrong way. Help me.

Thanks in anticipation,
Ravindra.
#3

[eluser]slowgary[/eluser]
Okay, if you want to pass $data['subject_result'] to your view file from each and every function, just place the code in the constructor. Also, instead of passing $data to your view, you'll need to make it a property of the class... $this->data. Example:
Code:
class Welcome extends Controller
{
     var $data;

     function Welcome()
     {
          parent::Controller();

          $this->load->model('subject_model');
          $subject_Result=$this->subject_model->subjects();
          if($subject_Result->num_rows()>0)
          {
               $this->data['subject_result']=$subject_Result->result();
          }
     }

     function index()
     {
          $this->parser->parse('header', $this->data);
          $this->parser->parse('home', $this->data);
          $this->parser->parse('footer', $this->data);
     }

     function inner()
     {
          $this->parser->parse('header', $this->data);
          $this->parser->parse('inner', $this->data);
          $this->parser->parse('footer', $this->data);
     }
}

Does this make sense?
#4

[eluser]slowgary[/eluser]
You should never copy and paste code. It almost always means there's a better way. The example above would mean all of the functions in your controller would already have called the subject model. If you wanted to apply this same concept to ALL of your controllers, you can extend the native Controller Class. You'd just create a file called MY_Controller.php and place it in system/application/libraries. Inside that file, create a class like so:
Code:
class BaseController extends Controller
{
     var $data;

     function BaseController()
     {
          parent::Controller();

          $this->load->model('subject_model');
          $subject_Result=$this->subject_model->subjects();
          if($subject_Result->num_rows()>0)
          {
               $this->data['subject_result']=$subject_Result->result();
          }
     }
}

Next, in all of your site's controllers you'll extend the BaseController instead of Controller:
Code:
class Welcome extends BaseController
{
     function index()
     {
          $this->parser->parse('header', $this->data);
          $this->parser->parse('home', $this->data);
          $this->parser->parse('footer', $this->data);
     }
}

I hope this helps.
#5

[eluser]ravindral[/eluser]
Thank you for instant reply. I will check the way you have written.

Thanks.
#6

[eluser]ravindral[/eluser]
I created BaseController.php file and put in /ci/application/libraries folder and extended welcome class from BaseController. But getting following error:

Fatal error: Class 'BaseController' not found in C:\Program Files\typo3\htdocs\ci\application\controllers\welcome.php on line 2

Thank you.
#7

[eluser]slowgary[/eluser]
The file should be named "MY_Controller.php", as specified in system/application/config/config.php:
Code:
$config['subclass_prefix'] = 'MY_';

The class INSIDE the file can be called whatever you'd. BaseController is just a suggestion. It MUST extend the Controller class and if you plan to have a constructor, the constructor MUST call the parent::Controller() method.
#8

[eluser]ravindral[/eluser]
I have MY_Controller. In that i have put code that fetch data to display in header.php view file.

///////////
<?php
class MY_Controller extends Controller{
function MY_Controller(){
parent::Controller();

$this->load->model('teacher/teacher_model');
$teacher_data = $this->teacher_model->edit_profile($this->session->userdata('userid')Wink;
$this->data['teacher_data'] = $teacher_data->result();

}
}
?>
//////////////////

header.php view file has
///////////
foreach($teacher_data as $tdata):
echo $tdata->photo;
endforeach;

/////

But I am getting error :
/////////////////////////
Error was encountered
Severity: Notice
Message: Undefined variable: teacher_data
Filename: views/header.php
Line Number: 96
//////////////


Can you help me to find the problem?


Thanks,
Ravindra.
#9

[eluser]InsiteFX[/eluser]
The CodeIgniter Template Parser Class in for one template file!

You are should only parser it one time.

Code:
// Call it only one time not three times!
$this->parser->parse('template_name', $this->data);

// in template_name include the following if you need them.
$this->load_view('header');
$this->load_view('home');
$this->load_view('footer');

Or you can do it like this.
Code:
$data['params'] = 'your values';

$this->load_vars($data);

$this->load_view('header');
$this->load_view('home');
$this->load_view('footer');

InsiteFX
#10

[eluser]ravindral[/eluser]
Some problem here. I used $this->load_vars($data);

but no use. Not clear where am I going wrong?

I am noting the code again :
<?php
class MY_Controller extends Controller{

function MY_Controller(){
parent::Controller();

$this->load->model('teacher/teacher_model');
$teacher_data = $this->teacher_model->edit_profile($this->session->userdata('userid')Wink;

$this->data['teacher_data'] = $teacher_data->result();

$this->load_vars($data);
}
?>


<?
class Welcome extends MY_Controller {
function Welcome()
{
parent::Controller();
$this->load->library(array('input','parser','session')); $this->load->helper(array('form','html','url','security'));
}

function index()
{
$this->parser->parse('header',$data);
$this->parser->parse('home',$data);
$this->parser->parse('footer',$data);
}

?>


Welcome is my default controller to load home page.


Error :
Error was encountered
Severity: Notice
Message: Undefined variable: teacher_data
Filename: views/header.php


Thank you,
Ravindra.




Theme © iAndrew 2016 - Forum software by © MyBB