I have two controllers, two views and a masterpage:
dashboard controller:
class Dashboard extends CI_Controller {
public function index()
{
if($this->session->userdata('login') == true){
$data['title'] = 'Dashboard';
$data['content'] = 'pages/dashboard';
$this->load->view('layout/master', $data);
}
else{
redirect('auth');
}
}
customers controller:
class Customers extends CI_Controller {
public function index()
{
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
public function add(){
$data['content'] = 'pages/add_customer';
$this->load->view('layout/master', $data);
}
}
And my master page:
<head>
<meta charset="utf-8" />
<title><?php echo isset($title)? $title: NULL; ?></title>
<?php $this->load->view('layout/header'); ?>
</head>
<body>
<!-- BEGIN PAGE BASE CONTENT -->
<?php $this->load->view($content);?>
<!-- END PAGE BASE CONTENT -->
</body>
The problem is When i call dashboard, the view runs in the template and everything is fine. When i call customers, again everything runs fine and the template is OK. But when i call the add method from customers controller, It's like the master page doesn't work and the template messed up. Like there is no CSS or something.
What's the problem?
Thanks in advance :)
I personally do not rely on native CI functionality for any template/view stuff. I use Stencil, which isn't actively developed anymore, but it worked on 2 and works on 3. I modified the core library file to handle session and config variables, but this thing is beautiful. It's at the center of every one of my CI deployments.
Stencil on scotch.io via Github
This absolutely doesn't answer your question and certainly deserves to be down-voted or flagged for that reason. But CI view handling is awkward, so I never endorse using it. I don't know why Stencil isn't built in.
Related
<a class="navbar-brand" href="<?php echo base_url ('welcome/aboutus');?> "> About Us </a>
Here welcome is controller name and aboutus is page name. I have set autoload. If I run this then it shows "Object not Found", what's wrong with that?
Your question is a bit hard to help you with because you have limited code to go by.
You may need to set your base url
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = '';
Welcome is the controller name as you said and aboutus would be a function name
Follow the PHP Codeigniter Naming Style Guide
Welcome.php
<?php
class Welcome extends CI_Controller {
public function index() {
}
public function aboutus() {
}
}
You may need a suitable .htaccess file to remove it so can work with out index.php
application
system
.htaccess
index.php
Try with <?php echo base_url ('index.php/welcome/aboutus');?>
In CodeIgniter, it dose not work like that.
<?php base_url ('welcome/aboutus') ?>
What it means is that Welcome is the name of the controller and aboutus is the name of the function in the controller.
So when you click on the link generated by the above code, it will search for a function named aboutus in the controller file Welcome.
To load about us using your link, the controller should look like this,
<?php
class Welcome extends CI_Controller {
public function index() {}
public function aboutus() {
$this->load->view('Name of the view file');
}
} ?>
I think you should know What is MVC Framework? and How Dose it Work?.
Have a look at these::
CI MVC Framework
CI MVC Static Page
I have Controller on CI like this
class Testing extends CI_Controller {
//put your code here
public function xx() {
$this->load->helper('url');
$this->load->view('testing');
}
public function linkURL() {
$this->load->helper('url');
$data['test'] = "testing123";
$this->load->view('xxx_view', $data);
}
}
I'm running on function linkURL and call view xxx_view, the code on view like this
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$segment = array('Testing', 'xx');
?>
Link
</body>
view call a href and using helper site_url to call Controller Testing and function xx. But the link is not working. I am already capture on firebug and link looks like weird. The Link on href contain *http://::1*. How to solved that link
You can print_r($_SERVER) in your controller and check it. or You can use
$config['base_url'] = 'http://localhost:8081/your-project/'
In my opinion, seem to you sent data test to view xxx_view but not using this variable. try echo $test and I see in url using xxx not xx
You need to autoload URL helper in your config/autoload.php.This time you are loading URl helper in function. That will not work for your view.
I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>
So i'm using latest Codeigniter and my problem is that i have this situation where i should use query inside foreach in controller. I have view that shows all production orders in one list, and i want all user made comments and notes underneath them from other table. Quite general situation in coding, but how i should do this with Codeigniter, cause you should keep your SQL clauses in Models.
Before i just fork raw SQL inside controller, i wanted to ask what is more gentle and proper way to achieve this. Thanks!
Controller
<?php
public function show()
{
$this->load->database();
$this->load->model('report_model');
$this->load->helper('url');
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
$this->load->view('all_reports', $data);
}
?>
Model
<?php
function getAllProductionOrders()
{
$this->db->select(*);
$this->db->from('dbo.QualityControl_ProductionOrders');
$query = $this->db->get();
return $query->result();
}
?>
View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
foreach ($productionOrdersas $row)
{
?>
<div>
ProdNo: <?php echo $row->Prodno; ?>
Descriptions:
Hours:
etc etc etc etc
</div>
[I want here comments and all user made notes]
<?php
}
?>
</body>
</html>
You should make an effort to avoid spinning over a result set with additional queries like that..imagine if you had 500 production orders. That's 501 queries to make a simple report
Instead, use a join & parse your results either by only printing out the header info once or using Underscore.php to make a nested result, as demonstrated here:
http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Not sure what you asking but try this -
In controller ,
$data['productionOrders'] = $this->report_model->getAllProductionOrders();
foreach($data['productionOrders']['id'] as $id)
{
$data['comment'][$id] = $this->report_model->getCommantsByOrderId($id);
}
i'm trying to learn codeigniter (following a book) but don't understand why the web page comes out empty.
my controller is
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3, $skip);
$data['main'] = "home";
$this->load->vars($data);
$this->load->view('template');
}
the view is:
<--doctype declaration etc etc.. -->
</head>
<body>
<div id="wrapper">
<div id="header">
<?php $this->load->view('header');?>
</div>
<div id='nav'>
<?php $this->load->view('navigation');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('footer');?>
</div>
</div>
</body>
</html>
Now I know the model is passing back the right variables, but the page appears completely blank. I would expect at least to see an error, or the basic html structure, but the page is just empty. Moreover, the controller doesn't work even if I modify it as follows:
function index()
{
echo "hello.";
}
What am I doing wrong?
Everything was working until I made some changes to the model - but even if I delete all those new changes, the page is still blank.. i'm really confused!
thanks,
P.
I've isolated the function that gives me problems.
here it is:
function getMainFeature()
{
$data = array();
$this->db->select("id, name, shortdesc, image");
$this->db->where("featured", "true");
$this->db->where("status", "active");
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get("products");
if ($Q->num_rows() > 0)
{
foreach($Q->result_arry() as $row)
{
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
"image" => $row['image']
);
}
}
$Q->free_result();
return $data;
}
I'm quite convinced there must be a syntax error somewhere - but still don't understand why it doesn't show any error, even if I've set up error_reporting E_ALL in the index function..
First port of call is to run php -l on the command line against your controller and all the models you changed and then reverted.
% php -l somefile.php
It's likely that there is a parse error in one of the files, and you have Display Errors set to Off in your php.ini. You should set Display Errors on for development and off for production, in case you haven't already.
(Edit: in the example above you have missed off the closing } of the class. It might be that.)
Make sure error_reporting in index.php is set to E_ALL and post your code for the model in question.
After looking through your function I suspect it's caused by $this->db->orderby("rand()");
For active record this should be $this->db->order_by('id', 'random');
Note that orderby is deprecated, you can still use it for now but the new function name is order_by
Not sure, but it can be also caused by php's "display_errors" is set to false.
You can change it in your php.ini file.