I m new to CodeIgniter so i m facing some problem regarding adding contents from one view to another.
I have three views header, contents and footer and want to put these views in one main container with 960px width.
coz i m new so expecting some simple answer.
My code is this
public function index()
{
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
}
Thanks
You need this.
$output = $this->load->view('header', 'your_data', true);
$output .= $this->load->view('content', 'your_other_data', true);
$output .= $this->load->view('footer', 'your_last_data', true);
$this->output->set_output($output);
For more Info
Just call the views inside the main view
public function index()
{
$data['content']['title'] = 'title';
$data['content']['body'] = 'body';
$this->load->view('layout',$data);
}
In views/layout.php
<div id="main_container">
<?php
$this->load->view('header',$content);
$this->load->view('content',$content);
$this->load->view('footer');
?>
</div>
Ex. in views/header.php
<title>.<?php echo $title;?></title>
in views/content.php
<div id="main_body"><?php echo $body;?></div>
function index()
{
$data['title'] ="Details";
$this->load->view("headerview",$data);
$data['batches'] =$this->detailsmodel->getname();
$this->load->view('content',$data);
$this->load->view("footerview");
}
// something similar to this ?
Didn't understand ur question properly brief what u want ? is this the answer u looking for
Related
I am trying to make a CRUD Module in Codeigniter HMVC but I seem to be missing something in the process. Here is what I am facing.
I have a News Module which has a Manage function
function manage(){
$grid = Modules::run('Crud/renderGrid', 'News' , 'News management');
}
Render Grid Function
function renderGrid($module , $page_title){
$data['page_title'] = $page_title; //Dynamic
$data['module'] = $module;
$data['view_module'] = 'Crud';
$data['displayfields'] = Modules::run($module.'/get_displayfields');
$data['key'] = Modules::run($module.'/get_key');
$data['rows'] = Modules::run($module.'/get' , $data['key']);
$data['view_file'] = 'manage';
$this->load->module('dashboard');
$this->dashboard->show_dashboard($data);
}
Here, the show_dashboard function just loads up a template layout with a desired view in it.
function show_dashboard($data = NULL){
if($data == NULL){
$data['view_file'] = "manage";
$data['page_title'] = 'Sigma Web Solutions';
}
$this->load->module('templates');
$this->templates->admin($data);
}
Templates->admin
function admin($data){
$this->load->view('admin' , $data);
}
The View (omitting the header n Footer)
<?php
if (!isset($view_file)) {
$view_file = "";
}
if (!isset($view_module)) {
$module = $this->uri->segment(1);
}
if (($view_module!="") && ($view_file!="")) {
$path = $view_module."/".$view_file;
$this->load->view($path);
}
?>
Now, when I try the url news/manage, it gives me a blank page with no source code in it. But when I try something like
crud/renderGrid/news/sometitle/ it works just fine.
Kindly point out what did I miss here. Thanks.
Working Solution:
Thanks to wolf I added a route
$route['managenews']= 'crud/renderGrid/news/News';
And it works like charm. But why do I need a route here? Shouldn't it just work. And this means for every module I need to have 4 entries in my route file for the CRUD system to work. can anyone suggest a better method?
Can someone help me out with this piece of code.
I have a template set up like this:
system/core/Loader.php :
public function template($view, $vars = array(), $return = FALSE)
{
$template = $this->view('includes/header', array(), $return);
$template = $this->view('includes/navigation', array(), $return);
$template .= $this->view($view, $vars, $return);
$template .= $this->view('includes/footer', array(), $return);
if ($return)
{
return $template;
}
}
It works perfect but when I try this to display a dynamic title, it doesn't want to display the title. I guess because the data is passed to content_home rather then to includes/header:
my controller:
public function home() {
$this->load->model("model_get");
$data["page_title"] = "Home Page";
$data["results"] = ($this->model_get->getData("home"));
$this->load->template("content_home",$data);
}
includes/header.php
<title><?php echo (isset($page_title)) ? $page_title : 'Default title text'; ?> </title>
Any ideas how to work it out ??
Thanks : )
I've implemented dynamic titles in an even simpler way:
Controller:
$data['header_data'] = array('title'=>'Foo bar');
$data['page'] = "login";
$this->load->view('template', $data);
View - template.php
$this->load->view('includes/header', $header_data);
$this->load->view('pages/'.$page);
$this->load->view('footer');
We have two folders in views - includes and pages.
includes folder: header.php, footer.php (etc..)
pages folder: login.php, profile.php (etc..)
views/includes/header.php
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
...
This way, you can use the same template to load all your pages. You may send data other than the title too!
In your views folder make a template file like this,
we'll call it template_web.php
<?php
// the preset view files for your template
$this->load->view('includes/header');
$this->load->view('includes/navigation');
// in this example we will have our content view files in a folder called 'web'
$templatefolder = 'web/';
// this is where you can pass in 'content' view files that are unique for the page
// this example has 3 placeholders you can put as many as you need
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
// preset template footer file
$this->load->view('includes/footer');
ok now in your controller methods
// data you want to pass to your header
$data['page_title'] = 'Awesome Home Page';
// other data
$data['bigbagofdata'] = $this->bigBagOf->awesomeData ;
// the name of the view files
$data['content01'] = 'homepage';
$data['content02'] = 'homepage_banner';
// your template
$this->load->view( 'template_web', $data );
data can be passed to any of the view files. and its very easy to set up different templates this way.
Try
$template = $this->view('includes/header', $view, $return);
instead of
$template = $this->view('includes/header', array(), $return);
If you want to access your $data["page_title"] variable in your header view, then you should pass it into your header view:
change this line:
$template = $this->view('includes/header', array(), $return);
to
$template = $this->view('includes/header', $vars, $return);
I think MrMarchello finds oout the real issue. the issue is not passing the $data variable to header.
$data['title'] = "Edit User";
$this->load->view('template/header', $data);
$this->load->view('user/edit_member', $data);
$this->load->view('template/footer');
Hope this help you a little bit.
In your config file add this parameter:
$config['pageTitle'] = 'Book Store';
In template page :
<title><?=$this->config->config["pageTitle"]?></title>
In any page you like change this config in action:
$this->config->config["pageTitle"] = $detail[0]['Title'];
Every time you can change the title easily.
Where should we process mysql queries in CodeIgniter application?
For example in a simple project we do like this :
for controller:
class Blog extends CI_Controller {
function posts(){
$data['query'] = $this->blog_model->index_posts();
$this->load->view('blog_view', $data);
}
}
and in view :
<?php
while ($post = mysql_fetch_object($query)):
?>
<div>
<p><?= $post->body; ?></p>
</div>
<?php endwhile; ?>
But, if we want to do something with body of post before print where should it be done?
For example, I want to write a function that formats the body of post and pass the body to it before doing echo.
Where should it be placed according to CoeIgniter's structure and recommended practices? (best option)
in the controller? (if so , how to use it)
in the view?
write a helper?
other approaches ?
Here's what is recommended:
Controller:
function posts() {
$this->load->model("blog_model");
$data['rows'] = $this->blog_model->index_posts();
$this->load->view("blog_view", $data);
}
Model: (blog_model.php)
function index_posts() {
$this->load->database();
$query = $this->db->get('your_table');
$return = array();
foreach ($query->result_array() as $line) {
$line['body'] = ... do something with the body....
$return[] = $line;
}
return $return;
}
View: (blog_view.php)
<?php foreach ($rows as $line): ?>
<div>
<p><?php echo $line['column']; ?></p>
</div>
<?php endforeach; ?>
Basically what happens is your model returns a multidimensional array that is passed the view and processed using a foreach() loop.
Good luck!
If you want to reuse that function create a helper. If you want this function only once put it in your controller and call from that controller.
Models are just for accessing database or maybe in few other cases, but mostly just for accessing things in database or editing, deleting etc. and sending the result to controller for further processing.
In your case I would stick with helper.
E.g. you will create a file top_mega_best_functions.php and put it inside helpers folder.
Than you write ther e.g. something like
function red_text($input) {
echo '<span style="color: red;">';
echo $input;
echo '</span>';
}
Then load the helper in your autoloader file or load before using.
And use in your view or controller like
$blablabla = "This text will be red";
red_text($blablabla);
It's becoming increasingly annoying to manage my /divs in codeigniter.
There seems to be a a lot of times where I want to load multiple views under a more encompassing div.
I am forced to either include a opening div in the first view, and a closing div in the last view, or, I've also tried creating a view solely for opening and closing divs so that its easier to keep track of.
function index($page = '1-home')
{
/*if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else { */
$this->blurb_model->session_load();
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['title'] = $this->page_model->make_title($data['title']);
$data['page'] = $page;
//top of page
$this->load->view('templates/head', $data);
/* div wrapper */ $this->load->view('templates/wrapper-start', $data);
$this->load->view('templates/nav', $data);
$this->load->view('templates/logo');
$this->load->view('leftbar/create', $data);
$this->load->view('leftbar/category', $data);
$this->load->view('leftbar/addfilter', $data);
$this->load->view('page/board', $data);
/* div wrapper */ $this->load->view('templates/wrapper-end', $data);
$this->load->view('templates/footer', $data);
}
Is there something I'm missing? Or an easier way to do this?
How about making a general template with the following sections and using CSS to style it:
Head
Navigation
Left/Rightcolumn
Maincontent
Footer
<div id="innerWrapper">
<div id="header">
<div id="logo"><!-- logo HTML -></div>
<div id="navigation"><?php echo $navigation; // or navigation html and logic in here ?></div>
</div>
<div id="mainbody">
<div id="leftmenu"><?php echo $leftmenu; ?></div>
<div id="maincontent"><?php echo $content; ?></div>
</div>
<div id="footer">
<div id="column1"><!-- HTML -></div>
<div id="column2"><!-- HTML -></div>
</div>
</div>
You can then assign $leftmenu and $content (and any other variable in your template) as the following:
// this assigns the returned HTML to the $content variable in your maintemplate
$data['content'] = $this->load->view('somtetemplate, NULL, TRUE);
$this->load->view('innerpage', $data); // this is your main template
Does that make sense? It'll make for better organisation IMO.
I have a problem with displaying a view. When I pass var to view, view doesn't render.
Controller:
public function indexAction()
{
$branchModel = new Application_Model_Branches();
$branches = $branchModel->getAllBranches();
$this->view->menu = $branches;
}
View (index.phtml):
<h2>Menu</h2>
<?php
$this->htmlList($this->menu);
?>
When I try debug $branches without assign it to view, all seems to be ok, but when I try push it to view,index.phtml don't appear.
Regards
You're just missing an echo in your code, the htmlList view helper returns a value - it doesn't echo it. Some examples of the various form view helpers can be seen here
<h2>Menu</h2>
<?php
echo $this->htmlList($this->menu);
?>
controller
$this->view->variableName = "Hello World!";//assign here
$this->view->assign('variableName1', "Hello new World!");//assign here
view
echo $this->variableName;//echo here
echo $this->variableName1;//echo here