A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$designation_id
Filename: dashboard/index.php
Line Number: 548
Backtrace:
File: /home/newtalent/public_html/demo/localhost/application/views/dashboard/index.php
Line: 548
Function: _error_handler
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 362
Function: include
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 304
Function: _ci_load
File: /home/newtalent/public_html/demo/localhost/application/controllers/Dashboard.php
Line: 86
Function: view
File: /home/newtalent/public_html/demo/localhost/index.php
Line: 292
Function: require_once
my controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Announcement extends MY_Controller {
public function __construct() {
Parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
//load the model
$this->load->model("Announcement_model");
$this->load->model("Xin_model");
$this->load->model("Designation_model");
}
/*Function to set JSON output*/
public function output($Return=array()){
/*Set response header*/
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
/*Final JSON response*/
exit(json_encode($Return));
}
public function index()
{
$data['title'] = $this->Xin_model->site_title();
$data['all_designations'] = $this->Designation_model->all_designations();
$session = $this->session->userdata('username');
$data['breadcrumbs'] = 'Announcements';
$data['path_url'] = 'user/user_announcement';
if(!empty($session)){
$data['subview'] = $this->load->view("user/announcement_list", $data, TRUE);
$this->load->view('layout_main', $data); //page load
} else {
redirect('');
}
}
public function announcement_list()
{
$data['title'] = $this->Xin_model->site_title();
$session = $this->session->userdata('username');
if(!empty($session)){
$this->load->view("user/announcement_list", $data);
} else {
redirect('');
}
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$announcement = $this->Announcement_model->get_announcements();
$data = array();
foreach($announcement->result() as $r) {
// get user > added by
$user = $this->Xin_model->read_user_info($r->published_by);
// user full name
$full_name = $user[0]->first_name.' '.$user[0]->last_name;
// get date
$sdate = $this->Xin_model->set_date_format($r->start_date);
$edate = $this->Xin_model->set_date_format($r->end_date);
if($r->designation_id == '') {
$ol = 'All Designations';
} else {
$ol = '<ol class="nl">';
foreach(explode(',',$r->designation_id) as $desig_id) {
$_des_name = $this->Designation_model->read_designation_information($desig_id);
$ol .= '<li>'.$_des_name[0]->designation_name.'</li>';
}
$ol .= '</ol>';
}
$data[] = array('<span data-toggle="tooltip" data-placement="top" title="View"><button type="button" class="btn btn-secondary btn-sm m-b-0-0 waves-effect waves-light" data-toggle="modal" data-target=".view-modal-data" data-announcement_id="'. $r->announcement_id . '"><i class="fa fa-eye"></i></button></span>',
$r->title,
$r->summary,
$ol,
$sdate,
$edate,
$full_name
);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $announcement->num_rows(),
"recordsFiltered" => $announcement->num_rows(),
"data" => $data
);
echo json_encode($output);
exit();
}
}
model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class announcement_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_announcements()
{
return $this->db->get("xin_announcements");
}
public function read_announcement_information($id) {
$condition = "announcement_id =" . "'" . $id . "'";
$this->db->select('*');
$this->db->from('xin_announcements');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
// Function to add record in table
public function add($data){
$this->db->insert('xin_announcements', $data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
// Function to Delete selected record from table
public function delete_record($id){
$this->db->where('announcement_id', $id);
$this->db->delete('xin_announcements');
}
// Function to update record in table
public function update_record($data, $id){
$this->db->where('announcement_id', $id);
if( $this->db->update('xin_announcements',$data)) {
return true;
} else {
return false;
}
}
}
?>
View
<?php
$session = $this->session->userdata('username');?>
<div class="box box-block bg-white">
<h2><strong>List All</strong> Announcements </h2>
<div class="table-responsive" data-pattern="priority-columns">
<table class="table table-striped table-bordered dataTable" id="xin_table">
<thead>
<tr>
<th>Action</th>
<th>Title</th>
<th>Summary</th>
<th>Published For</th>
<th>Start Date</th>
<th>End Date</th>
<th>Published By</th>
</tr>
</thead>
</table>
</div>
</div>
if anyone have solution please provide
thanks in advance
This error indicates that there is no property in stdClass.
For example:
<?php
$exampleObj = new stdClass();
$value = $exampleObj->designation_id; // Undefined property: stdClass::$designation_id
To avoid errors, you need to create this property:
<?php
$exampleObj = new stdClass();
$exampleObj->designation_id = 1;
$value = $exampleObj->designation_id;
Related
An uncaught Exception was encountered
Type: Error
Message: Call to a member function num_rows() on bool
Filename: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\cart_data.php
Line Number: 2
Backtrace:
File: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\sale_form.php
Line: 134
Function: view
File: C:\xampp\htdocs\sikan_v2\application\libraries\Template.php
Line: 14
Function: view
File: C:\xampp\htdocs\sikan_v2\application\controllers\Sale.php
Line: 26
Function: load
File: C:\xampp\htdocs\sikan_v2\index.php
Line: 315
Function: require_once
My Controllers: controllers/sale.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Sale extends CI_Controller {
function __construct()
{
parent::__construct();
check_not_login();
check_admin();
$this->load->model(['sale_m','item_m', 'supplier_m', 'stock_m']);
}
public function index()
{
$this->load->model(['customer_m', 'item_m']);
$customer = $this->customer_m->get()->result();
$item = $this->item_m->get()->result();
$cart = $this->sale_m->get_cart();
$data = array(
'customer' => $customer,
'item' => $item,
'cart' => $cart,
'invoice' => $this->sale_m->invoice_no(),
);
$this->template->load('template', 'transaction/sale/sale_form', $data);
}
public function process()
{
$data = $this->input->post(null, TRUE);
if(isset($_POST['add_cart'])) {
$this->sale_m->add_cart($data);
}
if($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
}
}
My Models: models/Sale_m.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Sale_m extends CI_Model {
public function invoice_no()
{
$sql = "SELECT MAX(MID(invoice,9,4)) AS invoice_no
FROM t_sale
WHERE MID(invoice,3,6) = DATE_FORMAT(CURDATE(), '%y%m%d')";
$query = $this->db->query($sql);
IF($query->num_rows() > 0) {
$row = $query->row();
$n = ((int)$row->invoice_no) + 1;
$no = sprintf("%'.04d", $n);
}else{
$no = "0001";
}
$invoice = "MP".date('ymd').$no;
return $invoice;
$query = $this->db->get();
return $query;
}
public function get_cart($params = null)
{
$this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
$this->db->from('cart');
$this->db->join('p_item', 't_cart.item_id = p_item.item_id');
if($params != null) {
$this->db->where($params);
}
$this->db->where('user_id', $this->session->userdata('userid'));
$query = $this->db->get();
return $query;
}
public function add_cart($post)
{
$query = $this->db->query("SELECT MAX(cart_id) AS cart_no FROM t_cart");
if($query->num_rows() > 0) {
$row = $query->row();
$car_no = ((int)$row->cart_no) + 1;
} else {
$car_no = "1";
}
$params = array(
'cart_id' => $car_no,
'item_id' => $post['item_id'],
'price' => $post['price'],
'qty' => $post['qty'],
'total' => ($post['price'] * $post['qty']),
'user_id' => $this->session->userdata('userid')
);
$this->db->insert('t_cart', $params);
}
}
My View: transaction/sale/cart_data.php
<?php $no = 1;
if($cart->num_rows() > 0) {
foreach ($cart->result() as $c =>$data) { ?>
<tr>
<td><?=$no++?>.</td>
<td><?=$data->barcode?></td>
<td><?=$data->item_name?></td>
<td class="text-right"><?=$data->cart_price?></td>
<td class="text-center"><?=$data->qty?></td>
<td class="text-right"><?=$data->discount_item?></td>
<td class="text-right"id="total"><?=$data->total?></td>
<td class="text-center" width="160px">
<button id="update_cart" data-toggle="modal" data-target="#modal-item-edit"
data-cartid="<?=$data->cart_id?>"
data-barcode="<?=$data->barcode?>"
data-product="<?=$data->item_name?>"
data-price="<?=$data->cart_price?>"
data-qty="<?=$data->qty?>"
data-discount="<?=$data->discount_item?>"
data-total="<?=$data->total?>"
class="btn btn-xs btn primary">
<i class="fa fa-pencil"></i> Update
</button>
<button id="del_cart" data-cartid="<?=$data->cart_id?>" class="btn btn-xs btn-danger">
<i class="fa fa-trash"></i> Delete
</button>
</td>
</tr>
<?php
}
} else {
echo '<tr>
<td colspan="8"> class="text-center">Tidak ada item</td>
</tr>';
} ?>
please help me solve the error
It will return bool when there's any errors in your query. Check this function again:
public function get_cart($params = null)
{
$this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
$this->db->from('cart');
$this->db->join('p_item', 't_cart.item_id = p_item.item_id');
if($params != null) {
$this->db->where($params);
}
$this->db->where('user_id', $this->session->userdata('userid'));
$query = $this->db->get();
return $query;
}
I think it should be:
$this->db->from('t_cart');
If you check again and find no errors, you can use method: last_query to get the query then try to query directly in phpmyadmin or MYSQL Workbench to check if there's any errors. Like this:
public function get_cart($params = null)
{
// above source code
$query = $this->db->get();
return $this->db->last_query();
}
You can do a
var_dump($data)
before calling this line to see if the 'cart' gets what you expect.
$this->template->load('template', 'transaction/sale/sale_form', $data);
My guess is that you are missing the result() at the end:
return $this->db->query("<your query>")->result();
I am having a syntax issue in the controller in the function index where the view is being loaded. it is saying that I have Severity: Parsing Error
Message: syntax error, unexpected ';' I cannot figure out what the issue is I am trying to get the count of all my records in the database. Any help would be appreciated.
controllers/test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('test_model','test_table');
}
public function index()
{
$this->load->view('includes/header');
$this->load->view('includes/table_main');
$this->load->view('includes/ajax_functions');
$this->load->view('includes/add_employee_form');
$total_records = $this->test_model->count_all_records();
//$this->load->view('test_view');
$this->load->view('test_view', array('total_records' => $total_records);
}
public function ajax_list()
{
$list = $this->test_table->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $test_table) {
$no++;
$row = array();
$row[] = $test_table->Name;
$row[] = $test_table->Department;
$row[] = $test_table->Manager;
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="javascript:void()" title="Edit" onclick="edit_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->test_table->count_all(),
"recordsFiltered" => $this->test_table->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
public function ajax_edit($id)
{
$data = $this->test_table->get_by_id($id);
echo json_encode($data);
}
public function ajax_add()
{
$this->_validate();
$data = array(
'Name' => $this->input->post('Name'),
'Department' => $this->input->post('Department'),
'Manager' => $this->input->post('Manager'),
);
$insert = $this->test_table->save($data);
echo json_encode(array("status" => TRUE));
}
public function ajax_update()
{
$this->_validate();
$data = array(
'Name' => $this->input->post('Name'),
'Department' => $this->input->post('Department'),
'Manager' => $this->input->post('Manager'),
);
$this->test_table->update(array('id' => $this->input->post('id')), $data);
echo json_encode(array("status" => TRUE));
}
public function ajax_delete($id)
{
$this->test_table->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}
//validation section were user must enter data in all fields
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('Name') == '')
{
$data['inputerror'][] = 'Name';
$data['error_string'][] = 'Name is required';
$data['status'] = FALSE;
}
if($this->input->post('Department') == '')
{
$data['inputerror'][] = 'Department';
$data['error_string'][] = 'Department is required';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
}
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class test_model extends CI_Model {
var $table = 'test_table';
var $column = array('Name','Department','Manager'); //set column field database for order and search
var $order = array('id' => 'desc'); // default order
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$column[$i] = $item; // set column array variable to order processing
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function count_all_records(){
$this->db->select('id');
$this->db->distinct();
$this->db->from('test_table');
$query = $this->db->get();
return $query->num_rows();
}
function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row();
}
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
view
Total Records: <?php echo $total_records ?>
In the class Test index function last statement ) is missing. Here is the correct one
$this->load->view('test_view', array('total_records' => $total_records));
It might cause due to script contain in view ,set short_open_tag 0 in your htaccess file and check if solves
<IfModule mod_php5.c >
php_value short_open_tag 0
</IfModule >
There is an error message when my view is being loaded. "Call to a member function result() on a non-object"
How do I fix this?
This is the loop in my View: discussion/view.php
<?php foreach ($query->result() as $result) : ?>
<tr>
<td>
<?php echo anchor('comments/index/'.$result->ds_id,$result->ds_title) . ' '
. $this->lang->line('comments_created_by') . $result->usr_name; ?>
<?php echo anchor('discussions/flag/'.$result->ds_id,
$this->lang->line('discussion_flag')) ; ?>
<br />
<?php echo $result->ds_body ; ?>
</td>
</tr>
<?php endforeach ; ?>
and this is the function index in my Comments Controller:
public function index() {
if ($this->input->post()) {
$ds_id = $this->input->post('ds_id');
} else {
$ds_id = $this->uri->segment(3);
}
$page_data['discussion_query'] = $this->Discussions_model->fetch_discussion($ds_id);
$page_data['comment_query'] = $this->Comments_model->fetch_comments($ds_id);
$page_data['ds_id'] = $ds_id;
$this->form_validation->set_rules('ds_id', $this->lang->line('comments_comment_hidden_id'), 'required|min_length[1]|max_length[11]');
$this->form_validation->set_rules('comment_name', $this->lang->line('comments_comment_name'), 'required|min_length[1]|max_length[25]');
$this->form_validation->set_rules('comment_email', $this->lang->line('comments_comment_email'), 'required|min_length[1]|max_length[255]');
$this->form_validation->set_rules('comment_body', $this->lang->line('comments_comment_body'), 'required|min_length[1]|max_length[5000]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('comments/view', $page_data);
$this->load->view('common/footer');
} else {
$data = array('cm_body' => $this->input->post('comment_body'),
'usr_email' => $this->input->post('comment_email'),
'usr_name' => $this->input->post('comment_name'),
'ds_id' => $this->input->post('ds_id')
);
if ($this->Comments_model->new_comment($data)) {
redirect('comments/index/'.$ds_id);
} else {
// error
$this->data['message'] = 'Error!';
}
}
}
When I use only 1 controller in my application, user, everything works fine. When I try to use 2 or 3 to divide the code and create some structure my application always give the following error :
A PHP Error was encountered
Severity: Warning
Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php:99)
Filename: Session/Session.php
Line Number: 140
Backtrace:
File: /Applications/MAMP/htdocs/BT_dashboard/application/controllers/Project.php
Line: 11
Function: __construct
File: /Applications/MAMP/htdocs/BT_dashboard/index.php
Line: 292
Function: require_once
Googled it and tried many things but can't find an answer. I'm using codeigniter 3. My User controller looks like :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* User class.
*
* #extends CI_Controller
*/
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
$this->load->model('project_model');
}
public function createProject() {
if ($_SESSION['userlevel']) {
if ($_SESSION['userlevel'] < 3) {
$userid = $this->uri->segment(3);
} else {
$userid = $this->input->post('userproject');
}
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('project_name', 'project name', 'trim|required|min_length[2]|callback_is_project_name_unique[' . $this->input->post('project_name') . ']');
$this->form_validation->set_rules('project_address', 'project address', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_description', 'project description', 'trim|required|min_length[2]');
$this->form_validation->set_rules('project_city', 'project city', 'trim|required|min_length[2]');
if ($this->form_validation->run() == FALSE) {
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
if ($_SESSION['userlevel'] < 3) {
$this->load->view('dashboard_add_project', $data);
} else {
$data['userslist'] = $this->user_model->get_users_list();
$this->load->view('dashboard_add_project_admin', $data);
}
$this->load->view('wrapper', $data);
} else {
$Address = urlencode($this->input->post('project_address'));
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $Address . "&sensor=true";
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if ($status == "OK") {
$Lat = $xml->result->geometry->location->lat;
$Lon = $xml->result->geometry->location->lng;
$LatLng = "$Lat,$Lon";
}
//pass validation
$data = array(
'project_name' => $this->input->post('project_name'),
'project_address' => $this->input->post('project_address'),
'project_description' => $this->input->post('project_description'),
'project_city' => $this->input->post('project_city'),
'project_finished' => $this->input->post('project_finished'),
'lat' => $Lat,
'lng' => $Lon,
);
//$this->db->insert('tbl_user', $data);
if ($this->user_model->create_project($data, $userid, $this->input->post('project_name'))) {
if ($_SESSION['userlevel'] > 1) {
$data['projectlist'] = $this->user_model->get_project_list();
$data['uncompleted_projects'] = $this->user_model->get_uncompleted_projects();
$data['completed_projects'] = $this->user_model->get_completed_projects();
} else {
$data['projectlist'] = $this->user_model->get_project_list_userid($userid);
}
$data['userdata'] = $this->session->userdata;
$this->load->view('header', $data);
$this->load->view('dashboard_projects', $data);
$this->load->view('wrapper', $data);
} else {
$data->error = 'There was a problem creating your new employee. Please try again.';
$data['userdata'] = $this->session->userdata;
// send error to the view
$this->load->view('header', $data);
$this->load->view('dashboard_add_project', $data);
$this->load->view('wrapper', $data);
}
}
}
}
My second controller, the project controller. This is the controller I want to use know to paste the createproject code so this gives more structure. But when I paste it here and adapt my views it gives the error. here's my project_controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* File Name: employee.php
*/
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url'));
$this->load->model('user_model');
$this->load->model('employee_model');
$this->load->model('customer_model');
}
public function createProject() {
//same as usercontroller
My User/login code in User controller :
public function login() {
$loggedin = $this->session->userdata('logged_in');
if (!$loggedin) {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('user_mail', 'user mail', 'required');
$this->form_validation->set_rules('user_password', 'user password', 'required');
if ($this->form_validation->run() == false) {
$data = new stdClass();
$data->error = 'Check your user and password';
$this->load->view('dashboard_login', $data);
} else {
$usermail = $this->input->post('user_mail');
$password = $this->input->post('user_password');
if ($this->user_model->resolve_user_login($usermail, $password)) {
$user_id = $this->user_model->get_user_id_from_mail($usermail);
$user = $this->user_model->get_user($user_id);
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = (string) $user->user_name;
$_SESSION['logged_in'] = (bool) true;
$_SESSION['user_gsm'] = (string) $user->user_gsm;
$_SESSION['user_address'] = (string) $user->user_address;
$_SESSION['user_city'] = (string) $user->user_city;
$_SESSION['userlevel'] = $this->user_model->get_user_level((int) $user->user_id);
$_SESSION['user_mail'] = $usermail;
$data['userdata'] = $this->session->userdata;
if ($_SESSION['userlevel'] == "3") {
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
}
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
} else {
$data = new stdClass();
// login failed
$data->error = 'Wrong username or password.';
// send error to the view
$this->load->view('dashboard_login', $data);
}
}
} else {
$data['userdata'] = $this->session->userdata;
$data['employeetotal'] = $this->user_model->get_amount_employees();
$data['customertotal'] = $this->user_model->get_amount_customers();
$data['projectstotal'] = $this->user_model->get_amount_projects();
$this->load->view('header', $data);
$this->load->view('dashboard_index', $data);
$this->load->view('wrapper', $data);
}
}
In config/autoload I've got following:
$autoload['libraries'] = array('database','session');
The session library should be automatically loaded for you. There's no reason for you to include it in each of your constructor functions.
Remove it from both controllers, and it'll work fine.
If it does not work, check in your config/autoload.php file to make sure it is being loaded there correctly.
This is a common problem , notice that the error is on line 99 on your controller Project.php (you should have only 76 lines if I am correct). I bet your controller doesn't have 99 lines ... delete the extra lines and the error will vanish.
or if you have an echo on line 99 comment it
hope that helps!
I am creating an application and handling common things in MY_Controller. I am using Message library to display common messages.
Here is MY_Controller.php:
<?php
class MY_Controller extends CI_Controller {
public $data = array();
public $view = TRUE;
public $theme = FALSE;
public $layout = 'default';
protected $redirect;
protected $models = array();
protected $controller_model;
protected $controller_class;
protected $controller_library;
protected $controller_name;
protected $partials = array(
'meta' => 'partials/meta',
'header' => 'partials/header',
'navigation' => 'partials/navigation',
'content' => 'partials/content',
'footer' => 'partials/footer'
);
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(true);
$this->load->helper('inflector');
$this->load->helper('url');
$this->controller_class = $this->router->class;
if(count($this->models)>0)
{
foreach ($this->models as $model)
{
if (file_exists(APPPATH . 'models/' . $model . '.php'))
{
$this->controller_model = $model;
$this->load->model($model);
}
}
}else{
if (file_exists(APPPATH . 'models/' . $this->controller_model . '.php'))
{
$this->load->model($this->controller_model);
}
}
$this->controller_name = $this->router->fetch_class();
$this->action_name = $this->router->fetch_method();
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
if($this->theme === TRUE OR $this->theme === '')
{
$this->theme = 'default';
$this->template->set_theme($this->theme);
}else if(strlen($this->theme) > 0){
$this->template->set_theme($this->theme);
}else{
}
if($this->layout === TRUE OR $this->layout === '')
{
$this->layout = 'default';
$this->template->set_layout($this->layout);
}else if(strlen($this->layout) > 0){
$this->template->set_layout($this->layout);
}else{
}
if(isset($this->partials))
{
foreach($this->partials as $key => $value)
{
$this->template->set_partial($key,$value);
}
}
if(isset($this->data) AND count($this->data)>0)
{
foreach($this->data as $key => $value)
{
if(!is_object($value))
{
$this->template->set($key,$value);
}
}
}
if($this->view === TRUE OR $this->view === '')
{
if($this->parse == TRUE)
{
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$this->_call_content($this->router->method);
$this->template->build($this->router->method,array());
}
}else if(strlen($this->view) > 0){
if($this->parse == TRUE){
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$view = $this->view;
$this->_call_content($view);
$this->template->build($view,array());
}
}else{
$checkpoint = $this->session->flashdata('exit');
if($checkpoint){
exit();
}else{
$this->session->set_flashdata('exit',TRUE);
}
$this->redirect();
}
}
public function _call_content($view)
{
$value = $this->load->view($view,$this->data,TRUE);
$this->template->set('content',$value);
}
/* Common Controller Functions */
public function index()
{
$data[$this->controller_model] = $this->{$this->controller_model}->get_all();
$this->data = $data;
$this->view = TRUE;
if($this->input->is_ajax_request() || $this->session->flashdata('ajax')){
$this->layout = FALSE;
}else{
$this->layout = TRUE;
}
}
public function form()
{
if($this->input->is_ajax_request() OR !$this->input->is_ajax_request())
{
$this->load->helper('inflector');
$id = $this->uri->segment(4,0);
if($data = $this->input->post()){
$result = $this->{$this->controller_model}->validate($data);
if($result){
if($id > 0){
}else{
$this->{$this->controller_model}->insert($data);
}
$this->message->set('message','The page has been added successfully.');
$this->view = FALSE;
$this->layout = FALSE;
$this->redirect = "index";
}else{
$this->message->set('message','The Red fields are required');
}
}
$row = $this->{$this->controller_model}->where($id)->get();
$this->data[$module_name]= $row;
}
}
public function delete()
{
$id = $this->uri->segment(3,0);
if($id != 0){
$this->{$this->controller_model}->delete($id);
}
$this->view = FALSE;
$this->layout = FALSE;
}
public function redirect()
{
redirect($this->redirect);
}
public function call_post($data)
{
foreach($data as $key => $row){
$_POST[$key] = $row;
}
}
public function query()
{
echo $this->db->last_query();
}
public function go($data = '')
{
if(isset($data)){
echo '<pre>';
print_r($data);
}else{
echo '<pre>';
print_r($this->data);
}
}
}
/**/
As you can see i am using Phil Sturgeon's template library and i am handling the layout with Jamierumbelow's techniques.
When i set a message on form insertion failure its fine. I can display it in the _remap like this
echo $this->message->display();
In the controller its working finebut when i call it in the partial navigation it does not display the message. What can possibly be the problem. I have tried on the different places in the My_Controller. Its working fine but not in the partial or even i have tried it in the failed form i am loading again.
This is the message library i am using
https://github.com/jeroenvdgulik/codeigniter-message
Here i s my navigation partial
<nav>
<div id = "navigation">
<ul id="menubar">
<li>Home</li>
<li>Downloads</li>
<li>About Us</li>
</ul>
</div>
<div id="breadcrumb">
<div class="breadcrumbs">
<!-- Here i will pull breadcrumbs dynamically-->
</div>
<!--<h3>Dashboard</h3>-->
</div>
<br clear = "all"/>
<div id="message">
<?php
$data['message'] = $message ;
$this->load->view('messages/success',$data);?>
</div>
</nav>
The message library is using session might be flashdata so i think its loosing session data somehow. Although i am using sessions correctly autoloading it.
I have found the issue. It was very simple. I was using the base url in config file as empty
$config['base_url'] = '';
I have to change it like this
$config['base_url'] = 'http://localhost/myproject/';