Errors with foreach loop on the CodeIgniter tutorial - codeigniter

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.

Related

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

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

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');

Codeigniter model not loading

I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}

MY_Controller extend cant show data ( Codeigniter )

I have changed my application core/MY_Controller.php extendable. Before I could get data from mysql but Now I get Message: Undefined variable: records. How can i get data again ?
MY_Controller.php :
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
My home controller :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$this->data);
}
Services_model :
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
This is the where i get error in home.php view :
<ul class="blog-medium">
<?php foreach($records->result() as $row): ?>
<li><h1><?php echo $row->title; ?></h1></li>
<?php endforeach ?>
The error message shows : Message: Undefined variable: records
In your controller it looks like you are setting $data['records'] when you should be setting $this->data['records']
I found a solution. In my Home controller, I was using the view as :
$this->render_page('pages/'.$page,$this->data);
But I have to use it as here :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
I dont know why but I tried this before. It did not make but it is working now.

passing data in model trough controller in codeigniter

I've heard about codeigniter couple of times so i was curious and thought why not. I took some tutorials and was very delighted to see how the framework worked.
Now i've the following problem i want to passing the data i've made in my model trough my controller and showing this in my view but i always run to the folowing error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\codeigniterTest\application\models\leden_model.php on line 9. The funny thing about this error is, when i google on this issue a lot of forum topics is about this issue but nowhere i get the right answer. my code looks like this.
codegniter version 2.03
class Leden extends CI_Controller {
function __construct(){
parent::__construct();
}
function index()
{
$this->load->model('leden_model');
$ledenModel = new Leden_model();
$data = $ledenModel->allLeden();
$this->load->view('leden_overzicht',$data);
}
}
<?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden(){
$query = $this->db->query("SELECT * FROM leden");
foreach ($query->result_array() as $row)
{
echo $row['Naam'];
echo $row['Achternaam'];
echo $row['Email'];
}
return $query;
}
}
?>
When i'm doing the query in my controller then i'm getting the results i want, why not in my model?
my question is what am i doing wrong?
Leden_model.php
?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden()
{
$data = array();
$this->db->select();
$query = $this->db->get('leden');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Leden_controller.php
?php
class Leden extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('leden_model');
}
function index() {
$data['leden_data'] = $this->ledenModel->allLeden();
$this->load->view('leden_overzicht',$data); }
}
leden_overzicht.php
?php
if (count($leden_data))
{
foreach ($leden_data as $key => $list)
{
echo $list['Naam'] . " " . $list['Achternaam'] . " " . $list['Email'] . "";
} }
else {
echo "No data."; }
did you load database? example:
$this->load->database();

Resources