Undefined variable: query in codeigniter - 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);

Related

`CI` return wrong value

I have no idea why my CI return wrong value from phpMyAdmin
Model :
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
return $this->db->get('post');
}
}
?>
And
Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Postc extends CI_Controller {
function index()
{
$this->load->model('post');
$posts=$this->post->getallpost();
echo '<pre>';
print_r($posts);
}
}
?>
enter image description here
You code is not giving you any error, it is doing exactly what you are asking it to do. Perhaps you need to be specific about the information you want to get. You see in your model
function getallpost()
{
return $this->db->get('post');
}
The above function will return you an object. If you want to get an array you need to write
return $this->db->get('post')->result_array();
And make sure you have some data in your post table to print For learning more about query database you should read Codeigniter's Query Builder Class
Change your Model Code
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
$query = $this->db->get('post');
if($query->num_rows() > 0)
{
return $query->result();
}else{
return false;
}
}
}
If you want to get return your result as as array then use
return $this->db->get('post')->result_array();
And if you want to get return your result as object then use
return $this->db->get('post')->result();

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

Fatal error: Call to a member function get_tab() on a non-object in D:\wamp\www\relation\application\controllers\c_relation.php on line 7

I got this problem, I researched more but didn't find any solution .
I am creating a Database relation logic For Crud .
Model code is :
<?php
class M_relation extends CI_Model{
public function __construct()
{
$this->load->database();
}
public function get_tab(){
$query = $this->db->get('user_id');
return $query->result_array() ;
}
}
And Controller Code is :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_relation extends CI_Controller {
public function index(){
$this->load->model('m_relation');
$data['user_id'] = $this->M_relation->get_tab();
echo var_dump($data['id']);
$this->load->view('v_relation');
}
public function __construct()
{
parent::__construct();
$this->load->model('m_relation');
}
}
?>
Please :)
$query = $this->db->get('user');
Instead
$query = $this->db->get('user_id');
The mistake was that i set the get() to user_id its a table field .
instead of table name .
please replace
$this->load->model('m_relation');
to
$this->load->model('M_relation');
You load model as m_relation but when you access function using M_relation.

why is this error showing

I am using CI for my web-site. While programming CMS for the same I faced no problem but when I am programming the same for user-end I am getting error as:
"404 Page Not Found
The page you requested was not found."
What am i doing wrong?? Any help/suggestions is warmly welcome.Thank you.
In Controller(model.phpl):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->load->model('common');
$this->load->model('home_model');
$this->load->model('page_model');
}
function _remap($method , $params = array())
{
$this->menu = $this->common->createMenuArr();
$this->index();
}
function index()
{
$data['sliderArr'] = $this->user_model->sliders();
$this->load->view('index', $data);
}
}
In Model(user_model.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model{
function __construct()
{
parent::__construct();
//$this->load->database();
}
function sliders()
{
$query = $this->db->query("SELECT slider FROM tbl_sliders ORDER BY slider_id DESC")->result_array();
return $query;
}
}
and finally in view(index.php):
<div id="slideContainer">
<div id="slideShim">
<?php
if(!empty($sliderArr))
{
foreach($sliderArr as $slider)
{ ?>
<img src="<?php echo base_url();?>images/sliders/<?php echo $slider['slider'];?>"
<?php }
}
?>
</div>
</div>
Go to Application -> Config -> Routes.
And Set $route['default_controller'] = "index"; //whatever your controller name is on which you want your application to route by default
Hope this helps.

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