Consider my code:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
Does anyone know what I'm doing wrong?
Thanks
In your library My_Controller you should be using the parent keyword instead of $this.
your code should look like so:
class MY_Controller extends Controller
{
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array())
{
parent::load->view('structure/header', $data);
parent::load->view($page, $data);
parent::load->view('structure/footer', $data);
}
}
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
Hope this helps!
Related
I have a problem that I am making the dynamic menu so that I have to send $data['coursemenu'] array type variable which I am fetching from
$data['coursemenu']=$this->CourseModel->fetchParentCourses();
and send this data to all controllers so that it is common for all functions of the controller after that it'll send to view which is common for headers which I had included in this way in all functions like this way.
$this->load->view('common/header', $data);
$this->load->view('mainpages/'.$page, $data);
$this->load->view('common/footer', $data);
first of all create a new file MY_Controller extends CI_Controller in application/core directory
create just a function public function __construct() in it and process your data there. for the result you want to use in all your controllers use name something like $this->User
here is my code for example
MY_Controller.php
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Options_model');
$this->load->model('User_model');
if (user_logged_in()) {
$this->User = user_logged_in();
}
foreach ($this->Options_model->get_global_settings() as $result) {
$this->global_data[$result->option_key] = $result->option_value;
}
}
}
for my view simply I use it
Header_view.php
<title><?php echo isset($page_title) ? $page_title . $this->global_data['site_name'] : $this->global_data['dashboard_title'] . ' – ' . $this->global_data['site_name']; ?></title>
Or this one
<a class="navbar-brand" href="#">Hi <?php echo $this->User['first_name']; ?>,Welcome to your dashboard</a>
My template parser looks like this (p/s the .'/'. is for readability):
$this->parser->parse($this->settings['theme'].'/'.'header', $data);
$this->parser->parse($this->settings['theme'].'/'.'register', $data);
$this->parser->parse($this->settings['theme'].'/'.'footer', $data);
I don't want to declare $this->parser->parse($this->settings['theme'].'/'.'header', $data); and $this->parser->parse($this->settings['theme'].'/'.'footer', $data); every time in my controller's functions.
How can I extend the MY_Parser.php so that I could use it like this instead:
$this->parser->parse($this->settings['theme'].'/'.'register', $data); will include the register.php between my header.php and footer.php automatically.
The benefit of doing this is to save 2 lines and if I have 20 functions, I can save 40 lines.
Just create a function (can be a helper, library extension or model):
function tpl($view, $data) {
$this->parser->parse($this->settings['theme'].'/'.'header', $data);
$this->parser->parse($this->settings['theme'].'/'.$view, $data);
$this->parser->parse($this->settings['theme'].'/'.'footer', $data);
}
If you want you can extend Parser and make a MY_Parser in the libraries folder and do:
class MY_Parser extends CI_Parser {
function tpl($view, $data) {
$this->parse($this->settings['theme'].'/'.'header', $data);
$this->parse($this->settings['theme'].'/'.$view, $data);
$this->parse($this->settings['theme'].'/'.'footer', $data);
}
}
Usage:
$this->parser->tpl($view, $data);
You could do this using $this->parser->parse() but that would require more code as you overwriting the default method and it's just as easy to introduce a new method.
UPDATE:
Using the MY_Parser method you might have to access $this->settings via $this->CI->settings thereby referencing the CI instance in CI_Parser depending on where this variable is coming from.
Create class with the name of your prefix class name in application/core folder and follow below code. $this->input->is_ajax_request() will only load view other then header and footer if request is from ajax. and in each controller you need to extend YOUR-PREFIX_Controller instead of CI_Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class YOUR-PREFIX_Controller extends CI_Controller {
protected $header_data;
protected $footer_data;
protected $header_view;
protected $footer_view;
public function __construct() {
parent::__construct();
$this->header_view = 'path-to-header';
$this->footer_view = 'path-to-footer';
}
public function _output($output) {
if ($this->input->is_ajax_request()) {
echo ($output);
} else {
echo $this->load->view($this->header_view, $this->header_data, true);
echo ($output);
echo $this->load->view($this->footer_view, $this->footer_data, true);
}
}
}
?>
I'm with a doubt at this post:
http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/
I've done up to libraries part (Excel.php).
But in the tutorial, where it starts Example Usage, where exactly I need to put all that code? In a new controller? Here in my project I tried to create a new Controller called Report. In report I've this code:
public function readReport() {
$this->load->library('excel');
$this->excel=PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
$number_value= $this->excel->getActiveSheet()->getCell('C1')->getValue();
$data['header'] = $number_value;
$this->load->view('pages/home', $data);
}
But I have also a Pages controller to control the pages of Views, and when I try to output something of PHP Excel is not possible. In my Pages.php I've wrote: $data['header'] = $number_value; and in view . But the variable "number_value" is not in Pages.php because it's only at Report.php. How can I do to output the excel data at my home.php (view) correctly?
Here is my pages.php controller
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
show_404();
}
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $number_value;
$this->load->helper('url');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
I'm not very familiar with PHPExcel but here are some thoughts.
First, to use a library in a Controller it must be loaded in that controller. You cannot access one controller from another. You could duplicate the code from Report in Pages but that seems a waste. You need reuseable code.
One "reuseable" approach is to create a model that uses the excel library. This will make it easy to reuse PHPExcel code just by loading the model in any controller.
A model version including a readReport() function might look like this.
class excel_model extends CI_Model
{
protected $excel;
function __construct()
{
parent::__construct();
$this->load->library('excel');
}
function readReport()
{
$this->excel = PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
return $this->excel->getActiveSheet()->getCell('C1')->getValue();
}
}
Pages controller should be modified as follows.
class Pages extends CI_Controller
{
public function view($page = 'home')
{
//The following check isn't needed, codeigniter will do this automatically
//if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
//show_404();
//}
$this->load->model('excel_model');
$this->load->helper('url');
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $this->excel_model->readReport();
$this->load->view('templates/header', $data);
//You don't have to keep sending $data to the views because
//any variables loaded by the first call to load->view()
//will be visible the all other views loaded in this function.
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
First time!
I'm learning Code Igniter, and as my first project I'm rewriting an existing site in CI.
On the existing site, all pages, dynamic or static, use a PHP include to load sidebar.php which is populated from the categories table in the database.
<div id="sidebar">
<?php
$result = mysql_query('SELECT category_id, name, url FROM categories ORDER BY category_id ASC');
while ($row = mysql_fetch_array($result)) {
$name=$row['name'];
$url=$row['url'];
print "<p>$name</p>";
}
?>
</div>
So now I've started in CI, I figured that the way to go was to make a sidebar model with the database call, a sidebar controller, a sidebar view, and then to load this view in the default page controller.
So, in /application/models I've got sidebar_model.php
<?php
class Sidebar_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_categories()
{
$query = $this->db->get('categories');
return $query->result();
}
}
Then in applications/controllers there is sidebar.php
<?php
class Sidebar extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('sidebar_model');
$data['result'] = $this->sidebar_model->get_categories();
$this->load->view('templates/sidebar_view', $data);
}
}
And then in applications/views/templates there is sidebar_view.php
<div id="sidebar">
<?php foreach($result as $row): ?>
<p><?php echo $row['name'] ?></p>
<?php endforeach ?>
</div>
This is called from my main page controller -
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/sidebar_view', $data);
$this->load->view('templates/footer', $data);
}
}
The trouble I'm having is that whilst the page controller is obviously loading the sidebar view (the box is showing with the correct CSS styling) it's throwing up PHP errors.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: templates/sidebar_view.php
Line Number: 2
Can anyone point me in the right direction here? Whilst just using a php include for the sidebar would be quick and easy, it doesn't seem like the MVC way of doing things.
Apologies for the lengthy post, and thanks in advance!
you are loading Pages controller and calling a view of Sidebar controller the variable $result gets data when Sidebar controller will load and you are loading Pages controller you have to load model of sidebar in pages controller
like
class Pages extends CI_Controller {
public function view($page = 'home')
{
$this->load->model('sidebar_model');
$data['result'] = $this->sidebar_model->get_categories();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/sidebar_view', $data);
$this->load->view('templates/footer', $data);
}
}
if you need sidebar for your all pages you can do in a good way like this first you need to extend MY_Controller and then extend you all controllers with MY_Controller
MY_Controller put in application core directory
<?
MY_Controller extends CI_Controller{
public $_sidebar = '';
public function __construct() {
parent::__construct();
$this->_sidebar = $this->sidebar();
}
private function sidebar(){
$this->load->model('sidebar_model');
$data['result'] = $this->sidebar_model->get_categories();
return $this->load->view('templates/sidebar_view', $data,TRUE);
}
}
now your page Controller
<?php
class Pages extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['sidebar'] = $this->_sidebar;
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
now you have sidebar as variable and it will be available to your controllers every time just define sidebar in you main template html any where you want
Your $data array in the Pages controller needs to have a "results" entry. Values in the array passed to a view are converted to local variables for use in the view. You do it correctly in the Sidebar controller (which you don't really need).
I am using the CodeIgniter framework for PHP. I have created a view named "login.php". Once I
created the view, I then loaded the view inside of a function named "index" which is located
inside a class named "CCI" that extends the Controller but I keep receiving this error: Fatal
error: Call to undefined function site_url() in C:\wamp\www\FinalP_CCI_Clone\system
\application\views\login.php on line 12. I don't understand the issue I an having because the
welcome page loads fine and my second function inside of the "CCI" class loads fine as well.
Here is some of the code:
Controller Files:
function CCI()
{
parent::Controller();
}
function index()
{
$this->load->view('login');
}
function test()
{
echo "Testing Data";
}
}
/* End of file login.php /
/ Location: ./system/application/controllers/cci.php */
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
}
function test()
{
echo "Testing Data";
}
}
/* End of file welcome.php /
/ Location: ./system/application/controllers/welcome.php */
You have to load the helper. The function site_url() is provided by the url helper, as described here.
$this->load->helper('url');
You can try this:
First Load the URL Helper with:
$this->load->helper('url'); or set the following value in application/config/autoload.php
$autoload['helper'] = array('url');
Then you can show the site url with:
base_url() (result: http://example.com/) or
site_url() (result: http://example.com/index.php/)
Note: Results depends on values stored in application/config/config.php