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.
Related
I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. Once I fix this, things will be much smoother.
He is the model function:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1($numstudent)
{
$query = $this->db->query("SELECT * FROM Student WHERE Student.JRNOption ='1'");
$numstudent = $query->num_rows()/25;
if($query->num_rows() > 0){
return $numstudent;
}
return null;
}
?>
Here is the controller.
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1($numstudent);
}
public function view($page = 'Statsview')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
//$data['title'] = ucfirst($page);
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
?>
Here is the view
<html>
<h1> Various Statistics for the Journalism department</h1>
<?php
echo "Percent of students that are undecided:";
echo $Test; ?>
</html>
What am I doing wrong? I been trying to get it right for over an hour and I just want to output the single result from the database query. Any help you can provide would be great.. please be respectful. Thanks!
Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.
Update Model:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1()
{
$query = $this->db->query("SELECT (SELECT COUNT(s2.student_id)
FROM Student s2
WHERE s2.JRNOption='1')*100/COUNT(s1.student_id) AS percentage
FROM Student s1");
$result = $query->row_array();
return $result['percentage'];
}
?>
Updated controller:
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1();
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
}
?>
Hope this will help you.
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; ?>
so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
What is the best way to create my model so I can call it from my controller like this?
$this->model_model->function->dbname
I have the following model but its rubbish:
Model:
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
Update:
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
Why would you want to? Why not call it like this?:
$this->model_model->functionname('dbname');
A complete skeleton of a model is like this:
<?php
class Mymodel extends Model
{
function get_some_entries($number_rows)
{
return $result = $this -> db -> query("SELECT id, name
FROM tablename
LIMIT $number_rows");
}
}
?>
Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,
class Custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
...
...
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
}
<?php
class CustomController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('custom_model', 'fubar');
}
public function index()
{
$result = $this->fubar->systemName('dbname');
print_r ($result);
}
}
?>