getwhere() error in codeigniter? - codeigniter-2

How i call solve the error?
Fatal error: Call to undefined method CI_DB_mysql_driver::getwhere() in C:\xampp\htdocs\ci_learn\application\models\employee_model.php on line 18
<?php
class Employee_model extends CI_Model {
function Employee_model()
{
parent::__construct();
}
function employee_getall()
{
$this->load->database();
$query = $this->db->get('employee');
return $query->result();
}
function employee_get()
{
$this->load->database();
$query = $this->db->getwhere('employee', array('id' => 1));
return $query->row_array();
}
} ?>

According to the docs, getwhere() was changed to get_where(). Looking back at the CodeIgniter Git log, it looks like it was removed on August 6, 2010 so any releases after that don't have it.

<?php
class Employee_model extends CI_Model {
function Employee_model()
{
parent::__construct();
}
function employee_getall()
{
$this->load->database();
$query = $this->db->get('employee');
return $query->result();
}
function employee_get()
{
$this->load->database();
$query = $this->db->get_where('employee', array('id' => 1));
return $query->row_array();
}
} ?>
Looking back at the [CodeIgniter Gitlog][1], it looks like it was removed on August 6, 2010 so any releases after that don't have it.According to the docs, getwhere() was changed to get_where().

Related

Getting remarks of teacher and principal in view

I'm trying to get the comments of teacher and principal to show up in view, all to no avail.
This is a school web app where parents get to view the results of their respective children. Just below the scores is the teacher's and principal comments.
Now, these comments show up in students' view but I'm having a hard time making it show up on the parents' view.
This is the model:
Comment_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Comment_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->current_session = $this->setting_model->getCurrentSession();
$this->current_session_name = $this->setting_model->getCurrentSessionName();
$this->start_month = $this->setting_model->getStartMonth();
}
public function TeacherComment($data)
{
$this->db->insert('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function UpdateTeacherComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetTeacherComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('teacher_comments')->row();
}
public function PrincipalComment($data)
{
$this->db->insert('principal_comments', $data);
return $this->db->affected_rows();
}
public function UpdatePrincipalComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('principal_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetPrincipalComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('principal_comments')->row();
}
}
The Controller I'm battling with:
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
How do I properly put it in function?
The view:
<span> CLASS TEACHER'S REMARK:
<u style="text-transform: uppercase;"><?php echo $teacher_comment->teacher_comment; ?> </u>
</span><br>
<br>
<span>PRINCIPAL REMARK:
<u style="text-transform: uppercase;"><?php echo $principal_comment->principal_comment; ?></u>
</span>
Let suppose this is your controller file.
class customview extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("Comment_model");
}
public function yourViewFunction()
{
// get your $id and $student_session_id
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
$this->load->view('your_html_view',$data); //this is the view file name without php
}
}
And now in your html view you can call like this:
print_r($teacher_comment); //you can view the array or object get from model
print_r($principal_comment); //you can view the array or object get from model

num_rows return ZERO all the time in CodeIgniter

I have a simple code in CI Model for Login BUT it always return 0.
$this->db->select('*');
$this->db->from('cms_users');
$this->db->where('username', "admin");
$this->db->where('password', "admin01");
$query = $this->db->get();
echo $query->num_rows();exit;
It always print 0.
When i try echo $this->db->get_compiled_select();exit; and run in phpmyadmin it return one value.
I would think because you are echoing instead of return
And you have exit on there as well I would not use exit;.
Model: User_model.php
<?php
class User_model extends CI_Model {
public function count() {
$this->db->where('username', "admin");
$this->db->where('password', "admin01");
$query = $this->db->get($this->db->dbprefix . 'cms_users');
return $query->num_rows();
}
}
Controller: Weclome.php
<?php
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index() {
$data['user_total'] = $this->user_model->count();
$this->load->view('welcome_message', $data);
}
}

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.

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
}

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