i have some problem with hmvc in codeigniter because im newbie with hmvc.
i have a template bootstrap with ci , so the problem like i not able to show my view
my view name index.php in directory "theme" :
<!DOCTYPE html>
<html lang="en">
<head>
<?php echo $this->load->view('theme/header') ?>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<?php echo $this->load->view('theme/navbar') ?>
</nav>
<div id="wrapper">
<div id="page-wrapper">
<?php $this->load->view($content) ?>
</div>
</div>
<?php echo $this->load->view('theme/footer') ?>
</body>
My Controller name dashboard in directory "theme" :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content'] = 'dashboard';
return $this->load->view('theme/index', $data);
}
}
i the error is in :
<?php echo $this->load->view('theme/navbar') ?>
and
<?php echo $this->load->view('theme/footer') ?>
the error Says :
"A PHP Error was encountered"
"Severity: 4096"
"Message: Object of class MY_Loader could not be converted to string"
"Filename: theme/index.php"
"Line Number : 8"
Please help
dont use echo
just $this->load->view('viewfile')
example if your view file is on application/view/theme/footer then it is
$this->load->view('theme/footer')
if you want to convert it to string you can pass a boolean true in Third parameter$this->load->view('theme/footer',array(),true)
Please refer to this codeigniter's view's documentation
Related
Hi I have a basic session code for testing which I have uploaded to two different servers.
File1 -
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
File2 -
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>
</body>
</html>
we can access the session information we set on the first page.
Links for Server 1 :
http://thycart.in/adaptyapp/1.php
http://thycart.in/adaptyapp/2.php
Links for Server 2 :(not working)
http://103.231.209.162:60070/1.php
http://103.231.209.162:60070/2.php
PHP Info Server 1:
http://thycart.in/adaptyapp/phpinfo.php
PHP Info Server 2:
http://103.231.209.162:60070/phpinfo.php
For some reason the session is not working in one server.Please help to find the reason.Thanks
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
if(isset($_SESSION)) {
echo "Favorite color is " .$_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " .$_SESSION["favanimal"]. ".";
}
?>
</body>
</html>
Why the header and footer comes after body when rendered? (Planets Tutorial). The planets who are using the planet.php template are children of a page Planets which is based on basic-page.php template. The About page children pages works fine though.
site/templates/planet.php
<html>
<head>
<title><?php echo $page->title; ?></title>
</head>
<body>
<h1><?php echo $page->title; ?></h1>
<h2>
Type: <?php echo $page->planet_type; ?>,
Age: <?php echo $page->planet_age; ?> years
</h2>
<p><?php echo $page->planet_summary; ?></p>
</body>
</html>
Doing a manual intermediate install solved it.
This is my page in View.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/', $subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>
This is my menu page inside partials folder.
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>
Below is the controller part.
<?php
class Example extends CI_Controller {
public $data = array('subview' => 'Oops, forgot to set a subview');
public function __construct(){
parent::__construct();
$this->load->helper('url');
}
public function listing() {
$this->data['subview'] = 'listing';
$this->load->view('layouts/layout', $this->data);
}
public function detail() {
$this->data['subview'] = 'detail';
$this->load->view('layouts/layout', $this->data);
}
}
I could not display the content in the file 'listing.php' which is in partials folder. When i run the project in localhost it saying not found. Can anyone help me to correct my problems.
modify views as bellow.
<!DOCTYPE html>
<?php $this->load->view('partials/page_head');?>
<body>
<div class="wrapper">
<header>
<div class="logo">Logo</div>
</header>
<?php $this->load->view('partials/menu');?>
<div id="content">
<?php $this->load->view('partials/'.$subview); ?>
</div>
<footer>© 2012 Codeigniter.tv</footer>
</div>
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>') </script>
</body>
</html>
modify your menu file as below
<menu>
<ul>
<li>Listing</li>
<li>Detail</li>
</ul>
</menu>
I'm new in CodeIgniter. I want to create Master Page or Layout with base style that will be contain Menu, footer and etc. I don't want to write repeating content in all pages and load it automatically for all pages. For example, I can create Master Page in asp.net or Layout in asp.net mvc. I'm sure I can do it in CodeIgniter.
lets assume you have an html page
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<!-- this is the dynamic part -->
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
you could split it into
1- header
2- menu
3- main content
4- footer
you basically put
<html>
<head>
<title> Hello World </title>
</head>
<body>
in one view called "view_header"
then you put
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
in a view called "view_menu"
and then you put
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in a view called "view_footer"
then in your controller
$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');
The other solution, which I see is better: create a view called view_template.php
<html>
<head>
<title> Hello World </title>
</head>
<body>
<div id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</div>
<div id="main-content">
<?php $this->load->view($content); ?>
</div>
<div id="footer">
Copy Right 2013 Hello World
</div>
</body>
</html>
in the controller lets say you want to call a view called About
$data = array('content'=>'about');
$this->load->view('view_template',$data);
Dynamic Layout
You would create a new Controller with a public $template variable
Your extended Controller will then inherit the $template variable from the Master Controller.
MY_Controller
class MY_Controller extends CI_Controller
{
public $template=null;
public function __construct()
{
if(is_null($this->template)){
$this->template = 'layouts/default';
}
}
}
Admin_controller
class Admin_Controller extends MY_Controller
{
public function __construct()
{
//Still inherits from MY_Controller
//this time, any controller extending Admin_Controller
//will point to 'views/layouts/admin'
if(is_null($this->template)){
$this->template = 'layouts/admin';
}
}
}
-
class User extends MY_Controller
{
public function index()
{
//$this->template is inherited
//from MY_Controller
//which point to 'views/layouts/default'
//We can also load a view as data
//ie no master layout, INTO our master layout
//note we don't pass $this->template
//and set the third param as true
$dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
'data' => 'some_data'
), true);
return $this->load->view($this->template, array(
'partial' => 'users/index' //partial view,
'dynamic_sidebar' => $dynamic_sidebar
));
}
}
Views/Layouts/default
<body>
//load the main view
//in our example we have also loaded
//a dynamic sidebar with this view
<?php $this->load->view($partial); ?>
<?php $this->load->view($dynamic_sidebar); ?>
//load a static view
//views/sidebar/static
<?php $this->load->view('sidebar/static'); ?>
</body>
Following the idea of a master page in Laravel, we can do this:
Controller code
$this->load->view('show');
View "show.php"
Set values for master page and then pass the variables to master page. Keep your master page codes inside ob_start() & ob_get_clean().
<?php $page_title = "My first page"; ?>
<?php ob_start(); ?>
Your header stylesheet links and scripts
<?php $page_header = ob_get_clean(); ?>
<?php ob_start(); ?>
<div>
<h1>This is a Header</h1>
<?php $this->load->view('Partials/list'); ?>
</div>
<?php $page_content = ob_get_clean(); ?>
<?php ob_start(); ?>
Your footer html or scripts
<?php $page_footer = ob_get_clean(); ?>
<?php $this->load->view('Layout/app',array(
'page_title' => $page_title,
'page_header' => $page_header,
'page_content' => $page_content,
'page_footer' => $page_footer
)); ?>
Partial view "Partials/list.php"
In case you don't want your code to be crowded. You can create some partial views to keep things simple.
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
Master Page "Layout/app.php"
<html>
<head>
<title><?= v($page_title) ?></title>
<?= v($page_header) ?>
</head>
<body>
<?= v($page_content) ?>
<?= v($page_footer) ?>
</body>
</html>
<?php
function v(&$var)
{
return isset($var) ? $var : '';
}
So that will genarate code:
<html>
<head>
<title>My first page</title>
Your header stylesheet links and scripts
</head>
<body>
<div>
<h1>This is a Header</h1>
<ul>
<li>LIst item 1</li>
<li>LIst item 2</li>
<li>LIst item 3</li>
</ul>
</div>
Your footer html or scripts
</body>
</html>
What we probably do is separate view files for header, menu, footer, etc.. that is common for all pages. And include them inside each view. like
$this->view('header');
$this->view('menu');
//Some specific content
$this->view('footer');
If you need same functionality without copying the above to all views, you need to create a function in your controller as follows:
private function myviewfunction($current_view)
{
$this->load->view('header');
$this->load->view('menu');
$this->load->view($current_view);
$this->load->view('footer');
return NULL;
}
and call this function in all your pages (methods)
$this->myviewfunction('about'); //About is the specific view for the method
I am going to show $Content in a new window. after some searches I found the following code. but it doesn't open a new window and echoes $content in the same page.
<form method="POST" action=" <?php echo $Content; ?> " target="blank"></form>
update:
THIS IS THE CODE:
<?php
if(isset($_POST['submit']))
{
include 'library/config.php';
include 'library/opendb.php';
$Unit_Code = $_POST['Unit_Code'];
$File_Name = $_POST['File_Name'];
$_SESSION['Unit'] =$Unit_Code;
$_SESSION['file'] =$File_Name;
$query = "SELECT Unit_Code,File_Name, type,size, Content FROM Units WHERE ((Unit_Code = '$Unit_Code')&&(File_Name='$File_Name'))";
$result = mysql_query($query) or die(mysql_error());
list($Unit_Code, $File_Name, $type, $size, $Content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$File_Name");
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="dblclick.js"></script>
</head>
<body>
<form method="POST" action=" <?php echo $Content; ?> " target="_blank"></form>
</body>
</html>
<?php
include 'library/closedb.php';
exit;
}
?>
It's target="_blank". The underscore is important.
You missed the _
<form action="URL TO POST THE FORM" method="POST" target="_blank">
<?php echo $Content; ?>
</form>
Missing: _ before targetin your HTML attribute: < .. target="_blank">