call another controller from dashboard using template pages - codeigniter

I working on Codeigniter project, I create page template to load header left menu and footer, everything working good, when I try the open link in the menu I want to open another controller. I do it but when the controller view open the variable inside to load database table for each row not working.. but the controller who load the database when I open it without my template its working fine
The Dashboard controller
class Dashboard extends CI_Controller{
protected $data = array();
function __construct()
{
parent::__construct();
$this->data['pagetitle'] = 'Invoices Manager';
}
protected function render($the_view)
{
$this->data['the_view'] = (is_null($the_view)) ? '' : $this->load->view($the_view,$this->data, TRUE);
$this->load->view('templates/master_page', $this->data);
}
public function home() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'templates/homepage_view');
}
public function dashboard() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'dashboard/home');
}
public function purchaselist(){
$this->render('purchase/index');
}
}
The purchase controller that working good alone
class Purchase extends CI_Controller{
protected $data = array();
protected $mydata = array();
function __Construct()
{
parent::__Construct ();
$this->load->database(); // load database
$this->load->model('Purchase_model'); // load model
$this->mydata['purchase']=null;
}
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if($query)
{
$mydata['purchase'] = $query;
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}
}
when I call dashboard/purchaselist they say the
Message: Undefined variable: purchase
Filename: purchase/index.php
Line Number: 17
its should load database table inside the template

try using dashboard/index.php/purchaselist or load your modal $this->load->model('Purchase_model'); as global

Please pass null or empty array in purchase if $query is empty
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if(!empty($query)){
$mydata['purchase'] = $query;
}else{
$mydata['purchase'] = array();
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}

Related

Getting remarks of teacher and principal in view

I'm trying to get the comments of teacher and principal to show up in view, all to no avail.
This is a school web app where parents get to view the results of their respective children. Just below the scores is the teacher's and principal comments.
Now, these comments show up in students' view but I'm having a hard time making it show up on the parents' view.
This is the model:
Comment_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Comment_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->current_session = $this->setting_model->getCurrentSession();
$this->current_session_name = $this->setting_model->getCurrentSessionName();
$this->start_month = $this->setting_model->getStartMonth();
}
public function TeacherComment($data)
{
$this->db->insert('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function UpdateTeacherComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetTeacherComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('teacher_comments')->row();
}
public function PrincipalComment($data)
{
$this->db->insert('principal_comments', $data);
return $this->db->affected_rows();
}
public function UpdatePrincipalComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('principal_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetPrincipalComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('principal_comments')->row();
}
}
The Controller I'm battling with:
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
How do I properly put it in function?
The view:
<span> CLASS TEACHER'S REMARK:
<u style="text-transform: uppercase;"><?php echo $teacher_comment->teacher_comment; ?> </u>
</span><br>
<br>
<span>PRINCIPAL REMARK:
<u style="text-transform: uppercase;"><?php echo $principal_comment->principal_comment; ?></u>
</span>
Let suppose this is your controller file.
class customview extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("Comment_model");
}
public function yourViewFunction()
{
// get your $id and $student_session_id
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
$this->load->view('your_html_view',$data); //this is the view file name without php
}
}
And now in your html view you can call like this:
print_r($teacher_comment); //you can view the array or object get from model
print_r($principal_comment); //you can view the array or object get from model

Codeigniter global variable in view

I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}

Code Igniter session declaration

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

Codeigniter get specific record in table

I practicing CI and loop through all records in a view and produces each link as below:
sitename.com/products/2
where 2 is id of certain product, I want to click on the link and get this product information in product page, how can I create a model to retrieve specific record?
Model:
class Db_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function getProduct($id) {
$query = $this->db->get_where('tbl_product', array('id' => $id));
return $query->result();
}
}
Controller:
class Products extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('db_model');
}
public function index()
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}
}
Thanks for help.
Your index function doesn't take $id as a parameter, So you need to modify your index function, Use the following index function, which I've edited.
public function index($id)
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}

Codeigniter tidying up my controllers

I've followed Codeigniter's configuration straight out of the manual - and just wondered if there was a simpler or more efficient way of coding my controllers...
eg.
class Home extends CI_Controller {
public function index()
{
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$data['articles'] = $this->Leftsidebar_model->articles();
$this->load->model('segment1/Default_model');
$data['head'] = $this->Default_model->segment1();
$data['segment1'] = $this->Default_model->segment1();
$data['segment2'] = $this->Default_model->segment2();
$this->load->model('Rightsidebar_model');
$data['coming_up'] = $this->Rightsidebar_model->coming_up();
$data['featured_pages'] = $this->Rightsidebar_model->featured_pages();
$data['recommended_link'] = $this->Rightsidebar_model->recommended_link();
$data['testimonials'] = $this->Rightsidebar_model->testimonials();
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
helpers and models can be auto loaded, this can be specified in the confiq file. This will save you from having to manually load them.
As for the rest:
You can subclass CI_Controller. Eg:
My Controller extends CI controller and this would then contain a 2 methods
1) to load page head and header
2) to load page bottom
you could then subclass your controller from My_Controller and call those methods
load_page_top();
//insert whatever you have to load
load_page_bottom();
other than that the rest is up to you
Eg:
class Home extends MY_Controller
{
index()
{
$data = get_data();
load_page_top();
//insert your views here specific to the controller
load_page_bottom();
}
get_data()
{
//gather all your needed data here and return it as an array
return data;
}
}
I love this structure, clean and neat.
class Home extends CI_Controller {
public function index()
{
// Load libraries, helpers, models
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$this->load->model('segment1/Default_model');
$this->load->model('Rightsidebar_model');
// Data for views
$data = array(
'articles' => $this->Leftsidebar_model->articles(),
'head' => $this->Default_model->segment1(),
'segment1' => $this->Default_model->segment1(),
'segment2' => $this->Default_model->segment2(),
'coming_up' => $this->Rightsidebar_model->coming_up(),
'featured_pages' => $this->Rightsidebar_model->featured_pages(),
'recommended_link' => $this->Rightsidebar_model->recommended_link(),
'testimonials' => $this->Rightsidebar_model->testimonials()
);
// Load views
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
Of course, the view loading could be managed in other way, having a view loading all common sections.
I just write little additional library for rendering similar page blocks.
Something like this:
class Display_Lib{
private $_CI;
private $_template_data;
public function __construct()
{
$this->_CI =& get_instance();
}
public function set($key, $value)
{
$this->_template_data[$key] = $value;
}
public function get($key)
{
return $this->_template_data[$key];
}
public function get_template_data()
{
return $this->_template_data;
}
public function display_page($view, $data = array())
{
$this->set('content', $this->_CI->load->view($view, $data, TRUE));
$this->_CI->load->view('templates/main_template', $this->get_template_data());
}
}
Set this library in auto load:
$autoload['libraries'] = array('session', 'database', 'display_lib');
And call it in controller:
class Main extends CI_Controller{
public function index()
{
$some_data = array();
$this->display_lib->display_page('views/main_view', $some_data);
}
}

Resources