I have two views ie, views/pages/home.php and views/pages/search_result.php. I have a controller to load this views ie, controllers/Pages.php. And also i have one more folder inside view ie, views/templates/header.php and views/templates/footer.php
When i am pointing the browser to http://localhost/codeigniter/home everything working perfect.
But the problem is when i am pointing the browser to http://localhost/codeigniter/search_result, the view footer.php is also showing. Actually am not given anything inside search_result.php
My controller code is,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function home($page = 'home')
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
public function search_result($page = 'search_result') {
//code to show search_result.php (http://localhost/codeigniter/search_result)
}
}
Inside search_result function i didn't given any code and while am pointing to http://localhost/codeigniter/search_result the footer from function home is showing ie, $this->load->view('templates/footer', $data);
What am doing wrong. Is there any solution to solve this issue. I am beginner in codeigniter.
Just simply try this
public function home()
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/home.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('pages/Home', $data); # changed
$this->load->view('templates/footer', $data);
}
}
Related
I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
Actually you can.
Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
I create a Home Page and when I want to access it's written Page Not Founded
I create in my rount.php access but again there is same error
$route['pages/index']= 'pages/home';
Do I need to change my controller or model or just route.php file
Pages controller
<?php
class Pages extends CI_Controller{
public function index(){
$this->load->view('pages/home');
}
public function view($page='home'){
if(!file_exists(APPPATH. 'views/pages/'.$page.'.php')){
show_404();
}
$data['title']= ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page,$data);
$this->load->view('templates/footer');
}
}
try like this . and make sure you are linking right base_url for home index function like this. follow this if its help .
echo base_url("pages/home");
$route['pages/home']= 'pages/index';
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');
}
}
I have anchor in header.php as below:
Backend
Also have the controller named backend.php in Controllers folder.
I also routed this code in routes.php file like
$route('default_controller')='frontend';
$route('backend')='backend';
Backend controller page is:
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Although getting error 404 Page not found.
Make sure all controller and models etc Ucfirst example Frontend.php instead of frontend.php
Codeigniter Docs http://www.codeigniter.com/docs
Auto load url helper in application.config/autoload.php
Change route from
$route('default_controller') ='frontend';
to
$route['default_controller'] = "frontend";
$route['backend'] = "backend";
I you have not set up you config to remove index.php with htaccess then use index.php in link.
Backend
Example controllers/Backend.php
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Example controllers/Frontend.php
<?php
class Frontend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Frontend Page';
$this->load->view('templates/header', $data);
$this->load->view('frontend/reg', $data);
$this->load->view('templates/footer');
}
}
Link here for htaccess examples for removing index.php on windows os https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
Try this one :
Backend // include also the folder where your backend file is stored
I hope this helps.
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).