what is the model code to fetch multiple result set from store procedure in codeigniter? - codeigniter

My Model code is given below.
public function getattendence($data)
{
$sql="CALL `sp_AttendanceDetails`(?,?,?);";
$query=$this->db->query($sql,$data);
return $query->result_array();
}
But it return one result set.The second result set i not get..

Pass data like this
public function getattendence($data)
{
# example of your $data array
$id = $data['id'];
$name = $data['name'];
$date = $data['date'];
$sql="CALL sp_AttendanceDetails($id,'$name',$date);";
$query=$this->db->query($sql);
return $query->result_array();
}

Use return $this->_execute_multi_query($sql);
refer http://php.net/manual/en/mysqli.multi-query.php

Related

Running same query in 2 different views: What's the proper way?

Just getting started into laravel and need to run the same db query for 2 different views, I know I could just create 2 controllers and perform the same query in both passing it to each view.
But, is that the proper way? Or is there a way without repeating my code?
Thanks!
Edit:
I have the following function inside a controller:
protected function getLocation($url)
{
$match = ['url' => $url];
$location = DB::table('location')->where($match)->first();
if (!$location) {
abort(404);
}
return $location;
}
the controller is returning that data to a view:
public function showsubcatandlocation($service)
{
$category = $this->getCat($service);
$location = $this->getLocation($category);
$id = $category->id;
$locID = $location->id;
$profiles = $this->getProfileswLoc($id, $locID);
return view('category', ['profile' => $profiles]);
}
What's the proper way to reuse the getLocation function? Or should I just copy it in the new controller? I just need to use it in those 2 views.
maybe scope Query helps?
class Location extends Model
{
public function scopeMatch($query, $url)
{
return $query->where('url', $url);
}
}
And then your two controllel methods will be
$location = Task::match($url)->first();
if (!$location) {
abort(404);
}
return $location;
$category = $this->getCat($service);
$location = Task::match($category['url'])->first();
$id = $category->id;
$locID = $location->id;
$profiles = $this->getProfileswLoc($id, $locID);
return view('category', ['profile' => $profiles]);

How to check name already exist from first result array

i want to check cpt code is already exist in db or not in update form..
so,First i'm checking all cpt codes present in database except post one now i'm getting result without post cpt code....now I want check cpt code from first query result.
my function in controller
public function updatedDxCodeExist()
{
$this->load->model('MedicineModel');
$Id = $this->input->post("Id");
$dxCode = $this->input->post("dxCode");
if(empty($Id))
{
$result = $this->MedicineModel->updatedDxCodeExist($dxCode);
} else {
$result = $this->MedicineModel->updatedDxCodeExist($dxCode, $Id);
}
if(empty($result))
{
echo("true");
}
else
{
echo("false");
}
}
my function in model
public function updatedDxCodeExist($dxCode, $Id =0)
{
$this->db->select("dx_code");
$this->db->from("dx_codes");
$this->db->where('dx_code !=',$dxCode);
$query = $this->db->get();
return $query->result();
}
Actually you can use like this:
if($result->num_row() <= 0 )
{
echo("true");
}
else
{
echo("false");
}
You can use array_search function in codeigniter

Codeigniter : array to string conversion error

Here is my model:
public function count_diabetic(){
$query = $this->db->query("Select count(diabetic) as count_diabetic from member where diabetic is not NULL");
return $query->result_array();
}
public function count_hypertensive(){
$query = $this->db->query("Select count(hypertensive) as count_hypertensive from member where hypertensive is not NULL");
return $query->result();
}
here is my controller:
public function home(){
$this->load->model('Jsv_model');
$data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata($data);
$this->load->view('home');
}
Here is my view with in php tag:
echo $this->session->userdata('count_diabetic');
But Here it shows error that array to string conversion error..
please help me
Inside count_diabetic() you should change $query->result_array()
into
$query->row()->count_diabetic,
it would return number of count only not array.
Do it to count_hypertensive() too.
In PHP, you can display arrays by using the print_r function instead of echo.
For example, you have to change your code to:
print_r($this->session->userdata['count_diabetic']);
You are doing it wrong in this line.
echo $this->session->userdata('count_diabetic');
What should you do?
// To Set a Data in Session
$session_data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata('user_data', $session_data);
// To get the session data
$session_data = $this->session->userdata('user_data');
$user_name = $session_data['username'];
$count_diabetic = $session_data['count_diabetic'];
$count_hypertensive = $session_data['count_hypertensive'];

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

Getting data from model to controller

I do have a question : I cannot pass the data from model to controller
You can see some of my codes, please help me.
It does not work!
this is my model "mymodel.php"
....
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
if($query){
foreach ($query->result() as $row);
}
$t = "EXAMPLE/{$row->code}";
function wandad() {
return $t;
}
.....
and this is my controller mycont.php
...
$this->load->model('mymodel');
$data['new'] = $this->Mymodel->wandad();
$this->load->view('myview',$data);
...
and this is my view myview.php
....
echo $new;
.....
Clearly you The model is not written properly and to corrent this simple do this
1.) I put a default value on $t
2.) I put the query >> loop on the inside function
wandad
so it can be executed once called from controller
function wandad() {
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
$t = "";
if($query){
foreach ($query->result() as $row){
$t = "EXAMPLE/{$row->code}".'<br>';
}
}
return $t;
}
Here are several issues into your model
Your Foreach function doesn't do anything
$t is not in the same namespace than wandad()
Function wandad is not defined into your model class
I'm not sure of what you wanna get with wandad() function but here's a pattern.
function yourFunction()
{
/* This will return the full query as an array */
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1")->result_array();
/* Store variable in the same class */
$this->t = "EXAMPLE/".$query[0]['code'];
/* Close yourFunction() */
}
public function wandad() {
return $this->t;
}
Then into your controller, do that instead :
$this->load->model('mymodel');
$this->mymodel->yourFunction();
$data['new'] = $this->mymodel->wandad();
$this->load->view('myview',$data);
#Touki his foreach actually does something. It will set the variable $row with the last row that is returned from the query. Not the best way to do it ... But it's a way. Better would be to use a limit in the query.
There is a small mistake in your code #Ernesto.that is
foreach ($query->result() as $row){
$t. = "EXAMPLE/{$row->code}".'<br>';
}
but your code was simply nice

Resources