Error when calling procedures in code igniter - codeigniter

I am calling a procedure in code igniter and when I try to call another procedure I get the following error:
Error Number: 2014
Commands out of sync; you can't run this command now
call get_post_info($id)
Filename: C:/Apache24/htdocs/application/models/Posts_model.php
Line Number: 18
I think I need to clear the result set... not sure.
Here is my model:
<?php class Posts_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_post_ids($acc_id){
$query = $this->db->query('call get_post_ids('.$this->db->escape($acc_id).')');
return $query->row_array();
}
public function get_post($id)
{
$query = $this->db->query('call get_post_info($id)');
return $query->row_array();
}
}
In my controller:
$post_ids = $this->posts_model->get_post_ids(1);
foreach ($post_ids as $id){
array_push($data, $this->posts_model->get_post($id));
}

I didn't actually run these solutions, but I found some info that might be what you look for. First of all, check this post about the cause of the problem.
I suppose $query->free_result() should solve the problem:
public function get_post_ids($acc_id){
$query = $this->db->query('call get_post_ids('.$this->db->escape($acc_id).')');
$result = $query->row_array();
$query->free_result();
return $result;
}
And in case that doesn't work, this this may be a good solution for CodeIgniter.

Related

Filtering and searching in codeigniter

I am doing a filtering and searching in codeigniter but no idea! how to do that?
but i try my best but fail!!!
here i show the list which is shown by ajax that sucess
MY DATA BASE ARE
MY CONTROLLER IS
public function get_quick_search()
{
$sepcli= $this->input->post('spec');
$distct= $this->input->post('dist');
$locat= $this->input->post('locat');
$data['list'] = $this->Doctor_model->search_listing();
$data['quck_search'] = $this->search_model->get_quick_list($sepcli,$distct,$locat);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$this->load->helper(array('form', 'url'));
$this->load->view('customer/header');
$this->load->view('customer/side_view',$data);
$this->load->view('customer/quick_search',$data);
$this->load->view('customer/footer');
}
MY MODEL LIKE
public function get_quick_list($locat,$distct,$sepcli)
{
$this->db->select('*');
$this->db->from('tbl_doctor');
$this->db->join("tbl_specialisation", "tbl_specialisation.spec_id = tbl_doctor.spec_id",'left');
$this->db->where("(district LIKE '$distct' AND place LIKE '$locat' AND spec_specialise LIKE '$sepcli')");
$query=$this->db->get()->result_array();
return $query;
}
HERE NO ERROR SHOW BUT THE RETURN QUERY IS NULL SHOW LIKE THIS (array(0) { })
you might try like this ,Your Controller function code
public function get_quick_search()
{
$s_data['sepcli'] = $this->input->post('spec');
$s_data['distct'] = $this->input->post('dist');
$s_data['locat'] = $this->input->post('locat');
$data['quck_search'] = $this->search_model->get_quick_list($s_data);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$this->load->helper(array('form', 'url'));
$this->load->view('customer/header');
$this->load->view('customer/side_view',$data);
$this->load->view('customer/quick_search',$data);
$this->load->view('customer/footer');
}
your model function code
public function get_quick_list($s_data)
{
$this->db->select('td.*, ts.*')
$this->db->from('tbl_doctor as td');
$this->db->join('tbl_specialisation as ts', 'ts.spec_id = td.spec_id','left');
if($s_data['sepcli'] !="")
$this->db->like('ts.spec_specialise',$s_data['sepcli'],'both');
if($s_data['distct'] !="")
$this->db->like('td.district',$s_data['distct'],'both');
if($s_data['locat'] !="")
$this->db->like('td.place', $s_data['locat'], 'both');
$query=$this->db->get()->result_array();
return $query;
}
sure it will helps , you should try it !!!

CodeIgniter invoke variable from another function in the same controller

I have a problem. I want to access to a variable from another function. let me explain.
Controller
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$plataforma = 'BaD';
$data['records'] = $this->usuarios_model->obtener_grupos($plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}
Model
function obtener_grupos($plataforma, $grupo){
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where('Plataforma', $plataforma);
$this->db->where('grupos', $grupo);
$query = $this->db->get();
return $query->result();
}
I want to access to the variable platforma in the function obtener_oplataforma_usuarios from the function obtener_grupos I tried different things and I still have problems.
Can you help me to solve this issue. and I want to do that because I want to make a query with two validations one is group and the other platforma. My problem is the second one. I recibe the information of plataforma fine from a from this is in the function obtener_oplataforma_usuarios and is fine but I do not how to read this variable from the other function.
you lose minuses (-) in pointers :)
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($plataforma);
must be:
$data['records'] = $this->usuarios_model->obtener_plataformas_usuarios($plataforma)
You can use a private variable in your class.
Create the variable above your __construct function, private $plataforma
Set the default value in your __construct function
Set and get in your public function using $this->plataforma
Implemented in your code:
private $plataforma;
public function __construct() {
parent::__construct();
$this->plataforma = "";
}
public function obtener_oplataforma_usuarios() {
$this->load->helper('form');
$this->load->model('usuarios_model');
$this->plataforma = $this->input->post('id_plataforma');
$data['records'] = $this>usuarios_model>obtener_plataformas_usuarios($this->plataforma);
$this->load->view('vista_plataforma_usuarios', $data);
}
function grupos($grupo){
$this->load->helper('form');
$this->load->model('usuarios_model');
$data['records'] = $this->usuarios_model->obtener_grupos($this->plataforma, $grupo);
$this->load->view('vista_plataforma_usuarios', $data);
}

Fetching “id” from a view and pass to another view in Grocery Crud CodeIgniter

I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>

codeigniter passing count_all_results to view

I'm using the count_all_results() function to return a user's number of languages spoken. But when I try to pass the number to the view, I keep getting a php undefined variable (for $lang_cnt). Below is my code:
Model
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Controller
function showLangCount() {
$data['lang_cnt'] = $this->language_model->countLanguages($id);
$this->load->view('lang_view', $data);
}
View
<p>This user speaks <?php echo $lang_cnt; ?> languages.</p>
One problem is that your model function takes two arguments:
function countLanguages($id, $cnt_languages)
But when you call it you are only passing one argument:
$this->language_model->countLanguages($cnt_languages);
And an even bigger problem, as Rocket points out, is that countLanguages doesn't return anything. Try this:
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Always check your model functions if they return value or not. Try this:
function showLangCount() {
if($this->language_model->countLanguages($id))
{
$data['lang_cnt'] = $this->language_model->countLanguages($id);
}
else
{
$data['lang_cnt'] = NULL;
}
$this->load->view('lang_view', $data);
}
Its better to use:
return $query->num_rows();
to return the number of rows effected...

Cannot load a model in codeigniter

I failed to load a model from my controller
This is the controller file, article.php:
<?php
class Article extends CI_Controller {
function show($id) { //id'ye gore getir
$this->load->model('articles_model');
$parameter = $this->articles_model>getarticle($id);
$this->my_template->build('article_view', $parameter);
}
}
?>
This is the model file, articles_model.php:
<?php
class Articles_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function Getarticle($id) {
parent::Model();
$query = $this->db->get_where('articles', array('id' => $id));
$data['articles'] = $query->result();
return $data;
$query->free_result();
}
}
?>
just to add, i even tried to load it from autoloader, still no chance, i assume something is wrong with the model, or the whole system broke.
up: the models loads without problems, if i put echo in __construct function, it works, however, i cannot call the getarticle function. geez
UP: I did it! according to http://grasshopperpebbles.com/codeigniter/codeigniter-call-to-a-member-function-on-a-non-object/
i used
$CI =& get_instance();
and called the function
$CI->articles_model->getarticle($id) and it called the function
It should be,
$CI =&get_instance();
$CI->load->model('articles_model');
$parameter = $CI->articles_model>getarticle($id);
There's a parse error in the following line:
$parameter = $this->articles_model>getarticle($id);
It should be:
$parameter = $this->articles_model->getarticle($id);
Does that fix your problem? If not, what error message are you seeing?
Leif's answer is the right one. Just to add one thing: you don't have to use the long variable name like $this->articles_model over and over, by using the second parameter:
$this->load->model('articles_model','artm');
$parameter = $this->artm->getarticle($id);
Just a little faster to type, and can reduce typos like the one in your sample.

Resources