I am a newbie with CodeIgniter (2.03), and I have the following problem:
Here is my main template (view):
<?php $this->load->view('backOffice/bo_header_in'); ?>
<?php $this->load->view($bo_main_content); ?>
<?php $this->load->view('backOffice/bo_footer_in'); ?>
Here is my model:
<?php
class Back_office_users extends CI_Model
{
public function getAllUsers ()
{
$query = $this->db->query("SELECT * FROM users");
if ($query->num_rows() > 0) {
foreach ($query->result() as $rows) {
$users[] = $rows;
}
return $users;
}
}
}
And here is my controler:
<?php
class Dashboard extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->is_logged_in();
}
public function index ()
{
$this->load->model('back_office_users');
$users['rows'] = $this->back_office_users->getAllUsers();
$data['bo_main_content'] = "backOffice/dashboard";
$this->load->view('backOffice/bo_template_in', $data, $users);
// if I pass the variable like this it works just fine...
//$this->load->view('backOffice/users', $users);
}
public function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || ($is_logged_in != true)) {
$this->accessdenied();
}
}
public function accessdenied ()
{
$data['bo_main_content'] = 'backOffice/accessdenied';
$this->load->view('backOffice/bo_template', $data);
}
public function logout ()
{
$this->session->sess_destroy();
redirect('backOffice/index');
}
}
And the dashboard view is like this:
<?php
print_r($users);
?>
I am getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: users
Filename: backOffice/dashboard.php
Line Number: 9
Can anyone shed some light how can I resolve this? I create another view without use of the template, and it print the array.
You're not passing the $users variable to the second (nested) view.
I'd suggest adding $users to the $data array, and then in the first view pass the $users array to the embedded view. So, in your controller:
public function index () {
/* stuff... */
$data['users']['rows'] = $this->back_office_users->getAllUsers();
$data['bo_main_content'] = "backOffice/dashboard";
/* stuff... */
$this->load->view('backOffice/bo_template_in', $data);
}
Then in the main view:
<?php $this->load->view($bo_main_content, $users); ?>
Then in the dashboard view:
<?php
print_r($rows);
?>
This is because in the main view, as you know, CodeIgniter transforms all elements of $data in to variables, so we'll end up with the $users variables. $users is an array containing rows, so when we pass $users to the second view, the second view transforms all elements of $users in to view variables, hence we now have access to $row.
Related
I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
This is my controller : Admin
class Admin extends CI_Controller {
public function index()
{
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
$this->load->view('Admin_view',$data);
}
}
This my model : Dash_model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
This my view : Admin_view
<?php echo $jumlah_instansi; ?>
Please help me, sorry newbie..Thank you..
Thats show error
Message: Undefined property: Admin::$Dash_model
Filename: controllers/Admin.php
and
Message: Call to a member function jml_instansi() on a non-object
You must first load a model before accessing it, e.g.
public function index()
{
$this->load->model('Dash_model');
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi()->result();
// ...
}
See Loading a Model for more.
Try like this.You are returning integer value of count so no need to use result() result set in controller.
Controller:
class Admin extends CI_Controller {
public function index()
{
$this->load->model('Dash_model');//load model
$data['jumlah_instansi'] = $this->Dash_model->jml_instansi();
$this->load->view('Admin_view',$data);
}
}
Model
public function jml_instansi()
{
$query = $this->db->query("SELECT * FROM instansi");
return $query->num_rows();
}}
View:
<?php echo $jumlah_instansi; ?>
whenever I run this code i get the error of undefined variable $d, so i noticed i have to pass the variable from the controller to the view. I am new to MVC, hence why i would be grateful if someone helps me with this issue.
This is my implementation so far:
**
Controller
**
class ControllerModuleGetstoreproducts extends Controller{
public function index() {
$object = new ModelGetstoreproducts();
$d = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
Model
<?php
class ModelGetstoreproducts{
function fetch(){
$sql = 'SELECT count(*) FROM oc_product';
$req = #mysql_query($sql);
return #mysql_fetch_object($req);
//return $req;
}
}
?>
View
<p>The total is <?php var_dump($d) ?></p>
Try this update to your files:
Controller:
class ControllerModuleGetstoreproducts extends Controller{
public $data;
public function index() {
$object = new ModelGetstoreproducts();
$this->data['products'] = $object->fetch();
require_once "getstoreproducts.php";
}
function returndata(){
$this->db->select("count(*)");
$this->db->from("oc_product");
$query = $this->db->get();
return $query->result();
}
}
View:
<p>The total is <?php var_dump($this->data['products']) ?></p>
Pass your id from View to Controller and get it into Model.Apply this rule You will not get any kind of error.
I'm working on simple application consisting of simple form. My code really works fine in local environment. But when i uploaded it on a live server gives an error, unable to fetch database fields and i think there is an error in my model.
Here is my model class
class Model_get extends CI_Model
{
function getData($page) {
$query = $this->db->get_where('ci_tbl', array('page' => $page));
print_r($query->result());
return $query->result();
}
}
Here goes my view
<div id="content">
<?php
foreach ($results as $row) {
$title = $row->title;
$para1 = $row->para1;
$para2 = $row->para2;
}
echo heading($title, 1);
?>
<p><?php echo $title;?></p>
<p><?php echo $para1;?></p>
<p><?php echo $para2;?></p>
</div>
My controller goes as
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('home');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('main_content',$data);
$this->load->view('footer');
}
public function about() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('about');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('about_page',$data);
$this->load->view('footer');
}
Make sure you actually return the results of the query:
//print_r($query->result());
return $query->result();
At the moment you've commented out return $query->result(); and you're just printing the result. The controller calling the model isn't going to get that information and therefore isn't passing through to the view.
Also check that the database connection is correct if moving from local to a public environment.
Actually you had an error
foreach ($results as $row) {
$title=$ row->title;
^^^^
It should be
$title = $row->title;
^^^
You need to update your query within model as
class Model_get extends CI_Model {
function getData($page) {
$query = $this - > db - > get_where('ci_tbl', array('page' => $page));
return $query->result_array();
}
}