Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
Related
I am starting my game with CodeIgniter and I have a problem - I would like to download the ID - of the product from the database so that it will be passed to the URL address:
Example address:
http://localhost/crm-SEO/api/admin/domains/{{ID in database}}/notes
When I have other methods like:
domains/GET/6
domains/update/6
Everything works fine for me, but I can not grasp it that I have domains/{{ID}}/
My code i models:
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('domains');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('domains');
$q = $q->row();
}
return $q;
}
public function update($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->update('domains', $domain);
}
public function create($domain)
{
$this->db->insert('domains', $domain);
}
public function delete($domain)
{
$this->db->where('id', $domain['id'] );
$this->db->delete('domains');
}
My code in controller:
public function __construct()
{
parent::__construct();
$post = file_get_contents('php://input');
$_POST = json_decode($post,true);
$this->load->model('admin/domains_model');
}
public function get($id = false)
{
$result = $this->domains_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
public function update()
{
$domain = $this->input->post('domain');
$this->domains_model->update($domain);
}
public function create()
{
$domain = $this->input->post('domain');
$this->domains_model->create($domain);
}
public function delete()
{
$domain = $this->input->post('domain');
$this->domains_model->delete($domain);
}
Below is my code where I am calling three methods from three models to retrieve counts as below.
$this->load->model('orders_model');
$order_count = $this->orders_model->count_orders(array("executive_id" => $this->id));
$this->load->model('activities_model');
$activity_count = $this->activities_model->count_activities(array("users_id" => $this->id));
$this->load->model('leads_model');
$leads_count = $this->leads_model->count_leads(array("users_id" => $this->id));
And this is the query I am getting:
SELECT COUNT(*) AS numrows FROM orders, activities, leads WHERE executive_id = '5' AND users_id = '5' AND users_id = '5'
which leads to a database error
Why is this happening?
Orders_model
class Orders_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_order($order_update_data = FALSE, $order_update_condition = FALSE) {
if (!($order_update_data === FALSE && $order_update_condition === FALSE)) {
if (is_array($order_update_data) && is_array($order_update_condition)) {
return $this->db->update('orders', $order_update_data, $order_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_orders($order_custom_condition = FALSE) {
if (!$order_custom_condition === FALSE) {
if (is_array($order_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($order_custom_condition);
$query = $this->db->get('orders');
return $query->result();
}
}
}
public function get_last_ref_id() {
$query = $this->db->query('select sprx_ref_id from orders where id in (select max(id) from orders)');
foreach ($query->result() as $row) {
return $row->sprx_ref_id;
}
}
public function fetch_orders($limit, $start, $order_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($order_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_orders($order_custom_condition) {
$this->db->where($order_custom_condition);
return $this->db->count_all_results('orders', FALSE);
}
}
Activities_model
class Activities_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_activities($activity_id = FALSE) {
if ($activity_id === FALSE) {
$query = $this->db->get('activities');
return $query->result();
}
$this->db->where('id', $activity_id);
#$this->db->order_by('id','ASC');
$query = $this->db->get('activities');
return $query->result();
}
public function add_activity($activity_data = FALSE) {
if (!$activity_data === FALSE) {
if (is_array($activity_data)) {
return $this->db->insert('activities', $activity_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_activity($activity_update_data = FALSE, $activity_update_condition = FALSE) {
if (!($activity_update_data === FALSE && $activity_update_condition)) {
if (is_array($activity_update_data) && is_array($activity_update_condition)) {
return $this->db->update('activities', $activity_update_data, $activity_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_activities($activity_custom_condition = FALSE) {
if (!$activity_custom_condition === FALSE) {
if (is_array($activity_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($activity_custom_condition);
$query = $this->db->get('activities');
return $query->result();
}
}
}
public function fetch_activities($limit, $start, $custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_activities($custom_condition) {
$this->db->where($custom_condition);
return $this->db->count_all_results('activities', FALSE);
}
}
Leads_model
class Leads_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_leads($lead_id = FALSE) {
if ($lead_id === FALSE) {
$query = $this->db->get('leads');
return $query->result();
}
$this->db->where('id', $lead_id);
$query = $this->db->get('leads');
return $query->result();
}
public function add_lead($lead_data = FALSE) {
if (!$lead_data === FALSE) {
if (is_array($lead_data)) {
return $this->db->insert('leads', $lead_data);
} else {
return false;
}
} else {
return false;
}
}
public function update_lead($lead_update_data = FALSE, $lead_update_condition = FALSE) {
if (!($lead_update_data === FALSE && $lead_update_condition)) {
if (is_array($lead_update_data) && is_array($lead_update_condition)) {
return $this->db->update('leads', $lead_update_data, $lead_update_condition);
} else {
return false;
}
} else {
return false;
}
}
public function get_custom_leads($lead_custom_condition = FALSE) {
if (!$lead_custom_condition === FALSE) {
if (is_array($lead_custom_condition)) {
#echo "Yes a parameter is passed which is also an array";
$this->db->where($lead_custom_condition);
$query = $this->db->get('leads');
return $query->result();
} else {
return false;
}
} else {
return false;
}
}
public function fetch_leads($limit, $start, $lead_custom_condition) {
$this->db->limit($limit, $start);
$this->db->order_by("id", "desc");
$this->db->where($lead_custom_condition);
$query = $this->db->get();
return $query->result();
}
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads', FALSE);
}
}
As far as i understand, you are surprised why the query builder uses additional parameter from the previous query.
You've to reset your Query according to docs
which means all your "count_" functions should be like
public function count_leads($lead_custom_condition) {
$this->db->where($lead_custom_condition);
return $this->db->count_all_results('leads');
}
obviously you did set the false flag on purpose - but i'm not sure why ;)
I believe that the error message is only a symptom, not the actual cause of the issue
. The way I read your code is that the count_*() methods in each of the 3 models should return the count from their respective tables only.
However, the way you wrote your count functions results in the query builder adding the tables and conditions to the overall query, not executing them just on the single tables
$this->db->where($custom_condition); <-- this adds a new where condition using "and" operator
return $this->db->count_all_results('activities', FALSE); <-- just adds another table without resetting the others
I would add a $this->db->reset_query(); line as the 1st line in each of the 3 count_*() methods to force the query builder to start from scratch.
The problem is due to using the second argument to $this->db->count_all_results(). When you set the second argument to FALSE then $this->db will not clear any select statements from its cache. Then each successive call to count_all_results() will include the table from any prior call to the function. The solution is simple - don't use the second parameter.
Change
return $this->db->count_all_results('activities', FALSE);
to
return $this->db->count_all_results('activities');
Not related to your problem but something that will improve your code is changing this
public function get_orders($order_id = FALSE) {
if ($order_id === FALSE) {
$query = $this->db->get('orders');
return $query->result();
}
$this->db->where('id', $order_id);
$query = $this->db->get('orders');
return $query->result();
}
to
public function get_orders($order_id = NULL) {
if (!empty($order_id))
{
$this->db->where('id', $order_id);
}
$query = $this->db->get('orders');
return $query->result();
}
Changing the argument default to NULL and using !empty($order_id) helps because it protects against and empty string or empty array being given as the argument. (Ready about empty() here.)
This new logic also keeps the code DRY - your not repeating the two line of code to get and return results.
Many of your other model functions could be cleaner too. For instance
public function add_order($order_data = FALSE) {
if (!$order_data === FALSE) {
if (is_array($order_data)) {
return $this->db->insert('orders', $order_data);
} else {
return false;
}
} else {
return false;
}
}
would be cleaner written like this
public function add_order($order_data = NULL) {
if (!empty($order_data) && is_array($order_data))
{
return $this->db->insert('orders', $order_data);
}
return false;
}
Sorry for nitpicking your code - I couldn't help myself.
Please Help.. im using CODEIGNITER
I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me.
beginner in CODEIGNITER need some help here.. thanks a lot!
in my controller:
if ($this->is_validated($rules))
{
$where = array('email' => $this->input->post('txt_email'));
$data['query'] = $this->database_model->select_userlevel('userlevel', $where);
$user = $data['query'];
foreach($user->result() as $row)
{
echo $row->email;
echo $row->userlevel;
}
}
in my model:
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return true;
}
else
{
echo 'no value';
return false;
}
}
Instead of return true you need to return your query in model
Model
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query;// return query here
}
else
{
return false;
}
Controller
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach($user->result() as $row)
{
// your code here
}
UPDATED FOR better solution and proper use of MVC.
Instead of return query you need to return data from your model
MODEL
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{
return false;
}
}
CONTROLLER
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
// your code here
}
$user is not an active record object, so it does not have a reference to the result method.
All of this logic belongs in the Model and not the Controller.
Controller
public function doSomething()
{
if( !$this->is_validated($rules) )
return;
$email = $this->input->post('txt_email');
$users = $this->database_model->select_userlevel($email);
// for debugging
var_dump( $users )
die();
}
Model
public function select_userlevel($email)
{
$query = $this->db->select()
->where('email', $email)
->get('userlevel');
return ( $query->num_rows() > 0) ? $query->result() : false;
}
I was doing a query in the controller but it came to my attention that I should do it in the model.
This is the code I have in the controller
$this->db->where('page', 'your-secret-skill');
$this->data['articles'] = $this->article_m->get();
and this is the get method I have in the core model:
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
How would I go about this and do the query in the articles model and loading it with the controller?
You should inherit the CI_Model with the Articles model and in it you can write your query as:
class Articles_m extends CI_Model
{
function __construct()
{
parent::__construct();
$this->table_name = 'articles';
}
public function get()
{
$this->db->where('page', 'your-secret-skill');
$query = $this->db->get($this->table_name);
return $query->result();
}
}
and then in your controller:
$this->data['articles'] = $this->article_m->get();
Remove this line from the controller first:
$this->db->where('page', 'your-secret-skill');
Instead send your-secret-skill as a parameter to your model function:
$where = array( 'page', 'your-secret-skill' );
$this->data['articles'] = $this->article_m->get('','',$where);
In your model:
public function get($id = NULL, $single = FALSE, $where = array){
if(isset($where) && is_array($where)){
$this->db->where($where);
}
}
When I send a Request to this Page including POST-DATA ({"bot_hw_id":"2147483647"}), i get the following error:
<p>Severity: Notice</p>
<p>Message: Undefined offset: 0</p>
<p>Filename: models/prometheus_model.php</p>
<p>Line Number: 26</p>
My Code:
Controller(update_bot function):
[code]public function update_bot()
{
$bot_data = json_decode($this->input->post('bot_data'));
$to_update = array(
'bot_last_update' => time(),
'bot_ip' => $_SERVER['REMOTE_ADDR'],
'bot_port' => $_SERVER['REMOTE_PORT']
);
$bot_data = $this->prometheus_model->get_bot_data_by_hw_id(/*$bot_data->{'bot_hw_id'}*/$bot_data->{'bot_hw_id'});
//echo $bot_data['bot_id'];
print_r($bot_data);
$this->prometheus_model->update('bots', array('bot_id' => $bot_data['bot_id']), $to_update);
//var_dump($bot_data);
}
Model(prometheus_model):
<?php
class Prometheus_model extends CI_Model {
var $tables = array(
'bots' => 'bots'
);
function __construct() {
parent::__construct();
}
public function tablename($table = NULL) {
if(! isset($table)) return FALSE;
return $this->tables[$table];
}
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
if($single) {
return $result[0];
}
return $result;
}
public function update($table, $where = array(), $data) {
$this->db->update($this->tablename($table),$data,$where);
return $this->db->affected_rows();
}
public function insert($table, $data) {
$this->db->insert($this->tablename($table),$data);
return $this->db->insert_id();
}
public function delete($table, $where = array()) {
$this->db->delete($this->tablename($table),$where);
return $this->db->affected_rows();
}
public function explicit($query) {
$q = $this->db->query($query);
if(is_object($q)) {
return $q->result_array();
} else {
return $q;
}
}
public function num_rows($table, $where = NULL) {
if(isset($where)){
$this->db->where($where);
}
$q = $this->db->get($table);
return $q->num_rows();
}
public function get_bot_data_by_hw_id($bot_hw_id) {
$q = $this->get('bots', array('bot_hw_id' => $bot_hw_id), TRUE);
return $q;
}
}
?>;
How can i fix this error?
First, I should mention that it's not absolutely a good and safe idea to query user input exactly without any process (as we see in your code). It's better to have different models at least for each table.
Anyway...
This way your function will be corrected:
public function get($table, $where = array(), $single = FALSE, $order = NULL) {
$this->db->where($where);
if(isset($order)) {
$this->db->order_by($order);
}
$q = $this->db->get_where($this->tablename($table),$where);
$result = $q->result_array();
// You should use $q->num_rows() to detect the number of returned rows
if($q->num_rows() == 1) {
// Return the first row:
return $result[0];
}
return $result;
}
It returns the first row when there is only one, and brings an array when $q->num_rows() is not equal to 1.
Hope it helps