codeigniter news section tutorial not working - php

hey guys im going thru the news section tutorial on codeigniter and i get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$db
Filename: core/Model.php
Line Number: 77
Backtrace:
File: /Applications/MAMP/htdocs/CodeIgniter/application/models/News_model.php
Line: 13
Function: __get
File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/News.php
Line: 10
Function: get_news
File: /Applications/MAMP/htdocs/CodeIgniter/index.php
Line: 292
Function: require_once
All i did was simply copy what was provided in the tutorial, here are my files
news_model.php file
<?php
class News_model extends CI_Model {
public function __constrcut() {
$this->load->database();
}
public function get_news($slug = FALSE) {
if ($slug === FALSE ) {
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
news.php
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
public function view($slug) {
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header',$data);
$this->load->view('news/view',$data);
$this->load->view('templates/footer');
}
}
and here is a picture of my file structure
im trying my best to learn and debug but i dont know what it could be.

Maybe, database library is not loaded. you have to load database library.
application / config / autoload.php
$autoload['libraries'] = array('database');

Related

How to solve page blank problem with codeigniter "news" section?

I'm following the Codeigniter tutorial: http://localhost:8080/myproject/user_guide/tutorial/news_section.html
When i point my browser to http://localhost:8080/myproject/news as the tutorial indicates at the final of the section, "Point your browser to your document root, followed by index.php/news and watch your news page." appears a blank page.
I tried to point to localhost:8080/index.php/news or /myproject/news but occurs the same problem.
Also i tried to set in autoload.php the next: $autoload['libraries'] = array('database');
as is indicated in codeigniter news section tutorial not working but not solve, so i leave it like this: $autoload['libraries'] = array('');
This is the routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The News.php:
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
the view.php:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
and the News_model.php:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' =>
$slug));
return $query->row_array();
}
}
I expect to watch the news page

How to fix "Undefined property: Mahasiswa::$form_validation"?

Undefined property: Mahasiswa::$form_validation
I already load the Form helper and Form Validation library.
I'm using CodeIgniter 3.1.10.
class Mahasiswa extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Mahasiswa_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$data['judul'] = 'Data Mahasiswa';
$data['mahasiswa'] = $this->Mahasiswa_model->getAllMahasiswa();
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa/index');
$this->load->view('templates/footer');
}
public function tambah()
{
$data['judul'] = "Form Tambah Data Mahasiswa";
$this->form_validation->set_rules('nama', 'Nama', 'required');
if ( $this->form_validation->run() == FALSE )
{
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa/tambah');
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa');
$this->load->view('templates/footer');
}
}
}
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Mahasiswa::$form_validation
Filename: controllers/Mahasiswa.php
Line Number: 26
You should add the form_validation library in autoload.php in the config folder. because I also had the same issue and it worked for me
$autoload['libraries'] = array('form_validation');
First you should load libraries in autoload.php in the config folder. Like this
$autoload['libraries'] = array('form_validation);
$autoload['helper'] = array('url', 'file');

Currently Using CodeIgniter Framework i have an Error

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$U_Model
Filename: controllers/Login.php
Line Number: 24
Backtrace:
File: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php
Line: 24
Function: _error_handler
File: C:\xampp\htdocs\Cmsproject\index.php
Line: 315
Function: require_once
$data=$this->U_Model->loginf($loginf['username'], $loginf['password']);
An uncaught Exception was encountered
Type: Error
Message: Call to a member function loginf() on null
Filename: C:\xampp\htdocs\Cmsproject\application\controllers\Login.php
Line Number: 24
Backtrace:
File: C:\xampp\htdocs\Cmsproject\index.php
Line: 315
Function: require_once
Model File Is Here:
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass){
$this->db->select('*');
$this->db->from('users');
$this->db->where('username',$username);
$this->db->where('password',$pass);
if($query=$this->db->get()){
return $query->row_array();
}else{
return false;
}
}
}
?>
Hope this will help you :
Your controller should be like this :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
//$this->load->model('User');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->database();
$this->load->model('U_Model');
}
public function index(){
$this->load->view('header');
$this->load->view('login');
$this->load->view('footer');
}
/*added public before loginp*/
public function loginp(){
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
/*md5 not recommended*/
$data = $this->U_Model->loginf($username, $password);
if( ! empty($data))
{
$this->session->set_userdata('sid',$data['id']);
$this->session->set_userdata('sname',$data['name']);
$this->session->set_userdata('username',$data['username']);
redirect('login/home');
}else{
$this->session->set_flashdata('emsg','Please Enter Your Correct Name And Password');
redirect('login/home');
}
}
public function home(){
$this->load->view('home');
}
}?>
Change your model like this
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass)
{
$this->db->where('username',$username);
$this->db->where('password',$pass);
$query = $this->db->get('users')
if($query->num_rows() > 0){
return $query->row_array();
}else{
return false;
}
}
}
?>
<?php
class U_Model extends CI_Model
{
public function loginf($username,$pass)
{
$this->db->where('username',$username);
$this->db->where('password',$pass);
$query = $this->db->get('users')
if($query->num_rows() > 0){
return $query->row_array();
}else{
return false;
}
}
}
?>

Message: Undefined property: News::$news_model in Codeigniter

I am learning Codeigniter from Codeigniter mannual step by step. Therefore i used as it is code from the manual.
The Controller class is :
<?php
class News extends CI_Controller{
public function _construct()
{
parent::_construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
?>
The Model Class is:
class News_model extends CI_Model{
public function _construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
?>
Please help me to fix this error. I've Tried every possible solution on internet but couldn't find any mistake.
CodeIgniter uses __construct with two _.
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
The above code should work.
As a side note, you can call your models like this:
$this->load->model('news_model', 'news');
and then you can call it like this:
$this->news->get_news();
But your method works fine, just makes it a bit easier.

Call to a member function get_news() on a non-object in C:\xampp\htdocs\CodeIgniter_Practice\application\controllers\news.php on line 11

I wrote a small application from CodeIgnitor user guide but when I run it, display the given message
Fatal error: Call to a member function get_news() on a non-object in C:\xampp\htdocs\CodeIgniter_Practice\application\controllers\news.php on line 11
The code is
class News extends CI_Controller{
public function _construct()
{
parent::_construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
}
Line 11 is :
$data['news'] = $this->news_model->get_news();
By looking at your code, i could see that you've missed one '_'(underscore) while defining your construct.
It has to be as below:
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
You can call your model by getting your Instance as well:
class News extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->CI = & get_instance();
$this->CI->load->model('news_model');
}
public function index()
{
$data['news'] = $this->CI->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
}

Categories