I am having a controller simu_mono.php, it loads a view
<?php
class simu_mono extends CI_Controller {
public function index()
{
$return = array(
'view' => $this->load->view('simu-mono')
);
}
}
?>
When I call it in the browser http://www.mydomaine.com/phpexcel/simu_mono it returns error object not found.
When I try http://www.mydomaine.com/phpexcel/index.php/simu-mono, the view is loaded but I have a error message before the page
{"view":null}simu mono
Cheers
Related
I put the line
$route['404_override'] = 'my404';
in the routes.php file, and I made a controller with a name my404:
<?php
include 'page.php';
class My404 extends Page {
private $page;
public function __construct() {
$page = new stdClass();
parent::__construct();
}
public function index() {
// $this->page->page_title = "Page not found!";
$this->page_view($this->load->view('my404', $this->page, true));
}
}
?>
and I made a View with a name of my404
It is working fine when I make a syntax error in the controller name, but it won't work if I write a wrong method and a Server error will occur instead of the customized 404 page, have I missed something in the routes.php file ?
From Codeigniter user guide:
This route indicates which controller class should be loaded if the requested controller is not found.
Apparently that rout will only work when controller isn't found.
you can instead override the default error_404 page at 'application/errors/error_404.php'.
I am new to CodeIgniter framework. I am using 2.1.4 version. I designed a simple login form, with a javascript validation, and the home page of a site. Can you please help me to understand how to declare session , and how to destroy the session on clicking signout link.
controller file of login page ( to load the view page login.php ):-
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index(){
$this->load->view('login');
}
function success() {
redirect ('home');
}
}
The controller file home.php for the view home.php
class Home extends CI_Controller {
// local constructor will be overriding the one in the parent controller class
// for using a constructor in any of my Controllers
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
}
I have designed the view page home.php, and gave the signout link:-
<div class="logout">Signout</div>
For initializing the session, i need to know, what all constructor changes/ config changes need, and the method of session destoy.
To start session library, Go to application/config/config.php and change the below line:
$autoload['libraries'] = array('session');
It would be better if you start your session in the autoload.php. To destroy session you would use :
$this->session->sess_destroy();
To set session :
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
here is an controller... first of all u need to declare a session so that you have two choice to declare one is Go to application/config/config.php change the code as
$autoload['libraries'] = array('session');
and follow this following method (controller)
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
}
function index(){
$this->load->view('login');
}
function success() {
$user=$this->input->post('user');
$psw=$this->input->post('pswd');
$this->load->model('validation');
$result=$this->validation->useraccess($user,$psw);
if($result)
{
$this->session->set_userdata('username', $user); //setting session
redirect ('home');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->unset_userdata('username');
redirect('login','refresh');
}
}
this is model where validation done
Class Validation extends CI_Model{
function __construct(){
parent::__construct();
}
function useraccess($user,$pswd)
{
$query = $this->db->query("select * from user where username='$user' AND password='$pswd'");
foreach ($query->result_array() as $row)
{
if($row['username']==$user AND $row['password']==$pswd)
{
return true;
}
else
{
return false;
}
}
}
}
here is a view
login page
create 2 text box and 1 submit button and declare form action as
localhost/index.php/login/success
for logut
localhost/index.php/login/logout
this is my controller
<?php
class Site extends CI_Controller{
public function index(){
$this->load->view('home');
}
}
?>
This is my model
<?php
class AdminModel extends CI_Model{
//function to get all the questions
function getAllQA(){
$result=$this->db->get('question');
if($result->num_rows()>0){ //this checks if the result has something if blank means query didn't get anything from the database
return $result;
}
else{
return false;
}
}
}
?>
and this is my view PAge
<form method="get" action="">
<div id="container">
<?php
$this->load->model('adminmodel');
if($result=$this->adminmodel->getAllQA()){
foreach($result->result() as $rows){
echo $rows->question;
echo $rows->option1;
}
}
else{
echo "No question found";
}
?>
</div>
</form>
So am calling the model in view called home.php page but its showing an error Call to a member function getAllQA() on a non-object in So but when am calling the model in controller its working fine but why is it showing error when am loading and calling the method in view page
Load your models inside the constructor of your controller
your controller should be like this
<?php
class Site extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('adminmodel');
}
//if you want to pass data or records from database to view do as blow code
public function index()
{
//define an empty array
$data = array();
$records = $this->adminmodel-> getAllQA();
$data['records'] = $records;
//pass the data to view you can access your query data inside your view as $records
$this->load->view('home',$data);
}
}
?>
You should not be loading Models from view files. Codeigniter is a MVC framework which means that all communication with the model should be handled by the controller.
The technical reason that this isn't working is likely that the view file is not a php class and therefore $this does not exist. Thats regardless, if you want to do something like this, don't use codeigniter!
not sure if you've done it.
Usually you call your model from within the controller.
In your case it would be:
class Site extends CI_Controller{
public function index(){
// load your model
$this->load->model('adminmodel');
// do your fancy stuff
$data['allQA'] = $this->adminmodel->getAllQA();
// here you can pass additional data e.g.
$data['userdata'] = $this->adminmodel>getuserinfo();
// pass data from controller --> view
$this->load->view('home',$data);
}
}
You can access the data in your view file by acessing $allQA or $userdata respectively. E.g.
foreach ($allQA as $qa){
echo $qa->question . "<br>" . $qa->option . "<br>";
}
or somewhere within a div
<div class="userbadge">
<?php echo $userdata; ?>
</div>
I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
I want to create a page, which I will be able to browse like this: /page/5.
I have created a page.php controller and I can view this page, here is its content:
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('template/header');
$id = $this->uri->segment(2);
$this->load->view('template/middle', array('id' => $id));
$this->load->view('template/footer');
}
}
The problem is that I cant reach the page number from the uri.
To be clear:
This is reachable: index.php/page.
But at this url, I'm getting the 404: index.php/page/5
I know I can make another controller function to view the page like this /index.php/page/id/5, but I want it be be accessable via the index function.
Is that possible?
CodeIgniter doesn't know whether you're looking for 5() or index(5)
To fix this, Try setting up a custom route in config/routes.php:
$route['page/(:num)'] = 'page/index/$1';
Also, In your index function:
public function index($id)
{
$this->load->view('template/header');
$this->load->view('template/middle', array('id' => $id));
$this->load->view('template/footer');
}