how to use model function result in controller by codeigniter - php

I have a function in Codeignite model that return just a row. This bellow code is my function in Codeigniter model:
public function Load_Home_Page_Theme(){
$CMD = "call load_home_page_theme()";
$query = $this->db->query($CMD);
return $query->result();
}
Now I want to use a field value that this function returned in a controller function like this:
public function Home(){
$this->load->model("Admin/Admin_getdata");
$data["HomePageTheme"] = $this->Admin_getdata->Load_Home_Page_Theme();
$this->load->view("User/Header", $data);
$data["AdsData"] = $this->Admin_getdata->Load_Ads_Data($data["HomePageTheme"]["home_ads_quantity"]);
$this->load->view("User/Home", $data);
$this->load->view("User/Footer");
}
But when I run this code, this error will shown:
Message: Undefined index: home_ads_quantity
How I can solve it?
Please guide me.

Try it:::
Model
public function Load_Home_Page_Theme(){
$CMD = "call load_home_page_theme()";
$query = $this->db->query($CMD);
return $query->row();
}
Controller:
public function Home(){
$this->load->model("Admin/Admin_getdata");
$HomePageTheme = $this->Admin_getdata->Load_Home_Page_Theme();
$data['HomePageTheme']=$HomePageTheme;
$this->load->view("User/Header", $data);
$data["AdsData"] = $this->Admin_getdata->Load_Ads_Data($HomePageTheme->home_ads_quantity);
$this->load->view("User/Home", $data);
$this->load->view("User/Footer");
}

Related

How to call 2 model function in one function controller in codeigniter

I am new to codeigniter, so please help me to solve my problem.
I want to call my two model function in one function controller. Heres my two model function:
public function getposts($postid){
$this->db->select('*');
$this->db->from('tbl_endangered');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
//$this->db->join('tbl_images');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
And here is my function controller:
public function viewAnimals($postid){
if(!$this->session->userdata("id") )
return redirect("AuthCon/login");
$this->load->model('Show');
$posts = $this->Show->getSinglePost($postid);
//$posts = $this->Show->getposts($postid);
$this->load->view('Showimage',['posts'=>$posts]);
}
In that code above I always get the first one that I declared. I want to fetch the getposts and getSinglePost inside the function viewAnimals controller.
Hopeyou can help me. Thanks in advance
I think using same variable name is creating a mess.
you can try assigning to posts array with output in it.
$this->load->model('Show');
$posts['getSinglePostData'] = $this->Show->getSinglePost($postid);
$posts['getPostsData'] = $this->Show->getposts($postid);
$this->load->view('Showimage',$posts);
Do like this in the controller
public function viewAnimals($postid){
if(!$this->session->userdata("id"))
return redirect("AuthCon/login");
$this->load->model('Show');
$data['images'] = $this->Show->getSinglePost($postid);
$data['endangered'] = $this->Show->getposts($postid);
$this->load->view('Showimage', $data);
}
Another way is to use join() and only one model function
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
$this->db->join('tbl_endangered', 'tbl_endangered.endangeredid = tbl_images.endangeredid');
$this->db->where('tbl_images.endangeredid', $postid);
return $this->db->get()->result();
}

can't pass data to view

I'm learning codeigniter framework. I've managed to make login page work, now I'm trying to pass data from controller to my view. I'm getting errors I have looked and looked here and did as suggested still getting the errors/
Model
class Dashboard_Model extends CI_Model{
function __construct(){
parent::__construct();
}
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result();
}
}
Controller
class Dashboard extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('admin/dashboard_model');
}
public function index(){
if(!$this->session->logged_in){
redirect('user_login');
}else{
$data['title'] = $this->session->fullname. " | Dashboard";
$this->load->view('admin/common/header',$data);
$data['total'] = $this->dashboard_model->getTasks();
$this->load->view('admin/dashboard',$data);
$this->load->view('admin/common/footer');
}
}
}
View
<h2 class="text-white"><span data-plugin="counterup"><?php echo $total?></span> <small><i class="mdi mdi-arrow-up text-success"></i></small></h2>
Error :
Severity: Notice
Message: Array to string conversion
Filename: admin/dashboard.php
Line Number: 40
What I am doing wrong?
result() This method returns the query result as an array of objects, or an empty array on failure update getTasks like below
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->result()[0]->totaltasks;
}
You can use row() to get single row instead of result() when you already know that you will get one row as a result
public function getTasks(){
$this->db->Select("COUNT(taskID) as totaltasks");
$this->db->from('tasks');
$query = $this->db->get();
return $query->row()->totaltasks;
}

codeigniter how to send query->num_rows from model to controller to view

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>

How to get and display database table value in codeigniter error was coming

I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}

model in codeigniter won't recognize id number

So here is the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: idnum
Filename: models/model_teacher.php
Line Number: 8
this is my controller:
public function teacher(){
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard();
$this->load->view('teacher/teacher', $data);
}
and my model:
class Model_teacher extends CI_Model {
public function scoreboard() {
$this->db->where('login_id', $this->input->post('idnum'));
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
}
and the view:
<?php
foreach ($result as $row) {
echo $row->login_id."<br>";
echo $row->lname."<br>";
}
?>
and don't know what is wrong with this code. please bear with me, i'm still a newbie at using codeigniter. thanks guys.
Change as following:
In Controller
public function teacher(){
$this->load->model('model_teacher');
$idNum = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($idNum);
$this->load->view('teacher/teacher', $data);
}
In Model
public function scoreboard($idNum) {
$this->db->where('login_id', $idNum);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}
you can't get directly in model you have to first get in controller
Controller
public function teacher(){
$id = $this->input->post('idnum');
$this->load->model('model_teacher');
$data['result'] = $this->model_teacher->scoreboard($id);
$this->load->view('teacher/teacher', $data);
}
Model`
public function scoreboard($idnum) {
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$idnum."'");
return $query->result();
}`
This is the code you can use
Would be better if you pass it from controller to model
$id = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($id);
and model:-
public function scoreboard($id) {
//$this->db->where('login_id', $id);
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$id."'");
return $query->result();
}
Also you haven't define anywhere $idnum so notices comes up

Categories