I'm working on a CodeIgniter system with the Grocery_CRUD extension. I am trying to integrate a login system with a CRUD application.
Currently I am getting this error when the user logs in, where the CRUD app should be displayed:
Severity: Notice
Message: Undefined variable: output
Filename: views/members_area.php
Line Number: 13
This is the line the error is occuring on: <?php echo $output; ?>
I realise that $output is not being set but I can't see why!! Any ideas?
This is my entire members_area.php file: View
<?php
echo '<span class="italic">'.date("l, F d, Y " ,time()).'</span>';
echo '<h3>Welcome: '.$this->session->userdata('username').' '. anchor('logout/signout', 'logout').'</h3>';
?>
<a href='<?php echo site_url('site/dishes_management')?>'>Dishes</a> |
<a href='<?php echo site_url('site/orders_management')?>'>Orders</a>
<?php echo $output; ?>
This is my site.php file: Controller
<?php
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
$this->load->database();
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
function members_area($output = null){
$this->load->view('members_area', $output);
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'You need to login to access this page. Login';
die();
}
function dishes_management()
{
try{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('dishes');
$crud->set_subject('Dish');
$crud->required_fields('dish_name');
$crud->columns('dish_name','dish_desc','dish_price', 'dish_cat');
$output = $crud->render();
$this->members_area($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
function orders_management()
{
$crud = new grocery_CRUD();
$crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}');
$crud->display_as('customerNumber','Customer');
$crud->set_table('orders');
$crud->set_subject('Order');
$crud->unset_add();
$crud->unset_delete();
$output = $crud->render();
$this->members_area($output);
}
}
}
?>
By default I would like the members_area.php to have output set to display items from the dishes table. This does not seem to be working.
Any help, as always is much appreciated!
have you tried enclosing the output inside an array ?
$this->load->view('members_area', array('output'=>$output));
or
$output['output'] = $output;
$this->load->view('members_area', $output);
You must send it on the views as two dimensional array, codeigniter is trying to find an output key from the given data but cannot find it since you have not supplied the proper data for the view to search from.
Related
I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}
I received an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: controllers/c_verifylogin.php
Line Number: 17
This is the filename that has an error:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_verifyLogin extends CI_Controller {
function __construct() {
parent::__construct();
//load session and connect to database
$this->load->model('m_login','login',TRUE);
$this->load->helper(array('form', 'url','html'));
$this->load->library(array('form_validation','session'));
}
function index() {
$this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login');
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
function check_database($password) {
//Field validation succeeded. Validate against database
$studentid = $this->input->post('studentid');
//query the database
$result = $this->login->login($studentid, $password);
if($result) {
$sess_array = array();
foreach($result as $row) {
//create the session
$sess_array = array('studentid' => $row->studentid);
//set session with value from database
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
} else {
//if form validate false
$this->form_validation->set_message('check_database', 'Invalid username or password');
return FALSE;
}
}
}
/* End of file c_verifylogin.php */
/* Location: ./application/controllers/c_verifylogin.php */
and this is the view which the data is passed
<!DOCTYPE html>
<html>
<head>
<title>Simple Login with CodeIgniter</title>
</head>
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('c_verifylogin/index');
echo form_label("StudentID: ");
echo form_input("studentid");
echo br();
echo form_label("Password: ");
echo form_password("password");
echo br();
echo form_submit("","Login");
echo form_close();
?>
</body>
</html>
I've been searching for the problem for hours, and I still don't know what's wrong with it. Can someone help?
Here in your code clearly show that you didn't define $data variable.
So you have 2 options either define $data as array like
$data = array();
or simply load the view without passing any variable like
$this->load->view('v_login');
I hope it will help you.
$data variable is not defined here..define it with some value of if you want to pass it else don't pass
$data to view call...
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login',$data);
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
Either you have to define $data
$data = array('something');
$this->load->view('v_login',$data);
or
remove $data
$this->load->view('v_login');
I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.
I am working on a basic application using the CI framework.
I have the following error:
404 Page Not Found
The page you requested was not found.
Posted below are my code files.
My Controller code:
class Contact extends CI_Controller{
function _Contact(){
parent::CI_Controller();
}
/*function main(){
$this->load->model('contact_model');
$data = $this->books_model->general();
$this->load->view('books_main',$data);
}*/
function input(){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('contact_model');
if($this->input->post('mysubmit')==true){
$this->contact_model->entry_insert();
}
$data = $this->contact_model->general();
$this->load->view('contact_input',$data);
}
}
Then in Model I have the following code:
class contact_model extends CI_Model{
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
function entry_insert(){
$this->load->database();
$data = array(
'name'=>$this->input->post('title'),
'address'=>$this->input->post('author'),
'year'=>$this->input->post('year'),
);
$this->db->insert('contact',$data);
}
function general(){
$data['base'] = $this->config->item('base_url');
$data['name'] = 'Name';
$data['address'] = 'Address';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['forminput'] = 'Student Registration';
$data['fname'] = array('name'=>'name',
'size'=>30
);
$data['faddress'] = array('name'=>'address',
'size'=>30
);
return $data;
}
}
Finally, my View:
<html>
<head>
</head>
<body>
<div id="header">
<?php $this->load->view('contact_header'); ?>
</div>
<?php echo heading($forminput,3) ?>
<?php echo form_open('books/input'); ?>
<?php echo $name .' : '.
form_input($fname).br(); ?>
<?php echo $address .' : '.
form_input($faddress).br(); ?>
<?php echo $year .' : '.
form_dropdown('year',$years).br(); ?>
<?php echo form_submit('mysubmit','Submit!'); ?>
<?php echo form_close(); ?>
<div id="footer">
<?php $this->load->view('contact_footer'); ?>
</div>
</body>
</html>
Can any one please help me?
Remove this in your Contact Controller:
function _Contact(){
parent::CI_Controller();
}
Replace with this:
function __construct(){
parent::__construct();
}
And in your Contact Model remove this:
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
Replace it with this:
function __construct(){
parent::__construct();
$this->load->helper('url');
}
Hey,
I often had similar Problems. Usually I check the following:
Check the Config file if the correct siteroot is set. I often had live server stuff in there.
Check in the .htaccess if the RewriteBase is set to the correct directory.
to make sure its reading the correct value an echo base_url(); (if this works its usually the .htaccess rewrite base).
Hope it helps.
r n8m
[enter link description here][1]
[1]: http://ellislab.com/forums/viewthread/105880/
hmc
If you have developed your application on Windows, you might have not set first character of Controllers and Models name as a capital character.
like /controllers/home.php to /controllers/Home.php
On Linux file names are case sensitive.
Note:- This is a solution to a possible problem. There may be issues of mis-configuration, server and path variables.
Possible Duplicate: CodeIgniter "The page you requested was not found." error?
I created a simple login form using Cake Php.When i click on the Login button one Warning appear
Illegal offset type [CORE\Cake\Model\Model.php, line 2734
I have use link as a reference http://bakery.cakephp.org/articles/SeanCallan/2007/04/17/simple-form-authentication-in-1-2-x-x
Code of Login.ctp
Login
<?php echo $this->Form->create('User', array('action' => 'login'));?>
<?php echo $this->Form->input('username');?>
<?php echo $this->Form->input('password');?>
<?php echo $this->Form->submit('Login');?>
<?php echo $this->Form->end(); ?>
2. modal file
find(array('username' => $data['username'], 'password' => md5($data['password'])), array('id', 'username'));
if(empty($user) == false)
return $user['User'];
return false;
}
}
?>
3.controller file
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController
{
var $name = "Users";
var $helpers = array('Html', 'Form');
var $components = array("Auth");
function index()
{
}
function beforeFilter()
{
$this->__validateLoginStatus();
}
function login()
{
if(empty($this->data) == false)
{
if(($user = $this->User->validateLogin($this->data['User'])) == true)
{
$this->Session->write('User', $user);
$this->Session->setFlash('You\'ve successfully logged in.');
$this->redirect('index');
exit();
}
else
{
$this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
exit();
}
}
}
function logout()
{
$this->Session->destroy('user');
$this->Session->setFlash('You\'ve successfully logged out.');
$this->redirect('login');
}
function __validateLoginStatus()
{
if($this->action != 'login' && $this->action != 'logout')
{
if($this->Session->check('User') == false)
{
$this->redirect('login');
$this->Session->setFlash('The URL you\'ve followed requires you login.');
}
}
}
}
?>
Why that happened.i am new in this.Some developers in PHP chat room suggest me to not use that Cake PHP. Any help is appreciated
Illegal offset type means that you tried to use an object or an array to index an array:
$x = stdClass;
$a = array(1,2,3,4);
$a[$x]; // illegal offset type
Check your code for possible places where you've taken user input (which could be an array of values even though you thought it's just one value) and used it in some function that expected just a value.
If that Cake internal function expected a value and tried to use it as the offset of an array, this message would appear.
Obs
The only place I notice in your code that passes a parameter (and not literal string values) is here:
$this->User->validateLogin($this->data['User']))
Do a var_dump on $this->data['User'] and see what's in it perhaps it's an array and you should've extracted just $this->data['User']['id'] I don't know, i haven't played that much with Cake.