Hi, I am new to Codeigniter. A PHP Error was encountered - codeigniter

A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$News_model
Filename: controllers/News.php
Line Number: 14
Backtrace:
File: /Applications/MAMP/htdocs/workshop10/application/controllers/News.php
Line: 14
Function: _error_handler
File: /Applications/MAMP/htdocs/workshop10/index.php
Line: 315
Function: require_once
my controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class News extends CI_Controller {
public function index()
{
$data['theNews'] = array("All your base are belong to us!",
"Autotune this news",
"Numa Numa!!",
$this->News_model->get_new_news()
);
$this->load->view('templates/news_header');
$this->load->view('new_items',$data);
$this->load->view('templates/news_footer');
}
}
my model:
class News_model extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
public function __construct()
{
//call the Model constuctor
parent::__construct();
}
public function get_new_news()
{
return "Something Kool";
}
}
my view:
<?php
foreach ($theNews as $news_item) { ?>
<div><?php echo $news_item; ?></div>
<?php } ?>

In your controller.Use the following code.
public function index()
{
$this->load->model('News_model');//loads your model
$return = $this->News_model->get_new_news();
$data['theNews'] = array("All your base are belong to us!",
"Autotune this news",
"Numa Numa!!",
$return
);
$this->load->view('templates/news_header');
$this->load->view('new_items',$data);
$this->load->view('templates/news_footer');
}

u didnt load your model file on your controller
use this$this->load->model('News_model');

Related

Writing to Database when executing controller's index method - error

I call the index() method of the collaborating controller, it automatically inserts 6 records into the database, and the action of logging into the database is not in the cadastrarquestao() method of the class, and not in the index method . I'm using a template library.
Controller Colaborador:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Colaborador extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->helper('cookie');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados['teste'] = 1;
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function logout() {
session_unset();
redirect ('/entrar');
}
public function cadastrarquestao() {
$this->simulado->envioquestao(1);
$dados['mensagem'] = "dados cadastrados com sucesso!";
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function materia() {
}
}
part of the code
public function envioquestao($dados) {
$data = [
'Id_usuario' => $this->session->userdata('id_usuario'),
];
$this->db->insert('teste',$data);
}
librarie Template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
View template:
<html lang="pt-br">
<?php
$this->load->view('app/header');
?>
<body>
<div class="wrapper">
<?php
$this->load->view('app/topo');
?>
<?php
$this->load->view('app/menu');
?>
<?php echo $contents ?>
<footer class="footer-container"><span>© </span></footer>
</div>
<?php
$this->load->view('app/footer');
?>
</body>
</html>
the correct one would be to insert it in the database after clicking on 'register' which is in the cadastrarquestao() method

PHP Message: Undefined property: Update_form::$update_model

Please help me on this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Update_form::$update_model
Filename: controllers/update_form.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\306\application\controllers\update_form.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\306\index.php
Line: 315
Function: require_once
An uncaught Exception was encountered
Type: Error
Message: Call to a member function rest() on null
Filename: C:\xampp\htdocs\306\application\controllers\update_form.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\306\index.php
Line: 315
Function: require_once
here is the code:
update.php
<?php
class Update extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('html','form','url'));
$this->load->library('table');
$this->load->model('update_model');
}
function index()
{
$this->table->set_heading('Books Name','Author','Edit Records');
$tstyle= array(
'table_open' => '<table border="1" align="center" cellpadding="4" cellspacing ="0">'
);
$this->table->set_template($tstyle);
$answer = $this->update_model->select();
foreach ($answer as $row)
{
$link = anchor(base_url().'update_form/update_function/'.$row->id,'Edit');
$this->table->add_row($row->name,$row->author,$link);
}
echo $this->table->generate();
}
}?>
update_model.php
<?php
/**
*
*/
class Update_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function select()
{
$this->db->select();
$this->db->from('books');
$query = $this->db->get();
return $query->result();
}
function rest($id)
{
$this->db->select();
$this->db->from('books');
$this->db->where('id',$id);
$query1= $this->db->get();
if($query1->num_rows() ==1)
{
return $query1->result();
}
}
}?>
update_form.php
<?php
class Update_form extends CI_Controller
{
function ___construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->model('update_model');
}
function update_function($id)
{
$answer = $this->update_model->rest($id);
foreach ($answer as $row)
{
echo form_open();
echo form_label('Book','book');
echo form_input('book', $row->name);
echo form_label('Author','author');
echo form_input('author', $row->author);
echo form_submit('submit','Update');
echo form_close();
}
}
}?>
You need to check line by line code which i am to add below
Controller :
Add below line on the top of the page of controller
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
**check you controller class name uppercase first letter.
**
$this->load->model('update_model');
Now update_model.php
Add : $this->db->select('*')
Try to load the model on the method or you can try to check autoload.php files..
It is showing line no 15 on update_form.php :
Create your function public in the controller.
Check or mark accepted if it works

Errors with foreach loop on the CodeIgniter tutorial

I am trying to work through the CodeIgniter tutorial and my news pages won't output data from the foreach loop. I get the following messages:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: pages/index.php
Line Number: 1
and
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/index.php
Line Number: 1
This is my model class:
<?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();
}
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');
}
}
And my controller:
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();
}
public function view($slug)
{
$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');
}
}
And the first view:
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
and the second:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
I know there are other questions about the tutorial but none seemed to help me.
Thanks.
in model you have closed your class after constructor. Should be closed after all function.
Also view() is initialized twice.

Undefined variable: query in codeigniter

I'm getting an error Undefined variable: query. I've been searching through the codeigniter forums and here for a solution but nothing seemed to work. If you can find what I'm doing wrong here I would greatly appreciate it.
Message: Undefined variable: query
Filename: views/display.php
Line Number: 3
Controller
function index() {
$this->load->model('mdl_tasks');
$data['query'] = $this->mdl_tasks->get('priority');
$this->load->view('display');
}
}
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mdl_tasks extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get($order_by) {
$this->db->order_by($order_by);
$query = $this->db->get('tasks');
return $query;
}
}
View
<h1>Your tasks</h1>
<?php
foreach ($query->result() as $row) {
echo "<h2>".$row->title."</h2";
}
?>
you need to include $data here:
$this->load->view('display', $data);

codeigniter help causing error while loading model

I am not sure whats wrong with my code.......it causing an error while loading model.......
please help...........
my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Exams extends CI_Controller {
public function index(){
$ob = new exam_class();
$ob->set_exam(0,'Html exam','1','20');
$ob->create_exam();
echo 'success';
}
}
class exam_class{
private $id;
private $title;
private $catagory;
private $timeLength;
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->load->model('examModel');
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ExamModel extends CI_Model {
public function create_exams($title,$catagory,$timeLength){
$data = array(
'title' => $title ,
'catagory' => $catagory ,
'timeLength' => $timeLength
);
$this->db->insert('exams', $data);
}
}
error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: exam_class::$load
Filename: controllers/exams.php
Line Number: 26
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\exam\application\controllers\exams.php on line 26
You shouldn't put more than one class in a file. Controller should be something like this.
class Exams extends CI_Controller {
private $id;
private $title;
private $catagory;
private $timeLength;
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index(){
$this->set_exam(0,'Html exam','1','20');
$this->create_exam();
echo 'success';
}
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
Along with #shin i want to include that go to this file
....\testproject\application\config\autoload.php
and edit this to add your models
$autoload['model'] = array('modelName1','modelName2');
and to load the models from any time from any controller . This will automatically load your models.No need to add
$this->load->model('modelName');
Tip : Keep it simple
In your controller :
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index()
{
$exam_data = $this->process_exam_data(0,'Html exam','1','20');
$insert_status = $this->examModel->create_exams($exam_data);
if($insert_status===TRUE){
echo "Exam Insert Successful!";
}
else{
echo "Exam Insert Failed";
}
}
public function process_exam_data($id, $title, $category, $timelength)
{
// Do whatever you want with the data, calculations etc.
// Prepare your data array same as to be inserted into db
$final_data = array(
'title' => $processed_title,
'catagory' => $processed_category,
'timeLength' => $processed_time
);
return $final_data;
}
And in your model :
public function create_exams($data)
{
$result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
return $result;
}
Your index function is the main function which does the calling, whereas all the processing work is done in the process_exam_data function.
Have a nice day :)

Resources