Hello Everybody i have problem with my program i need check multi number in my database but when i test it just show only one result my code :
/*in mt View*/
$data = array(
'name' => 'search_id',
'id' => 'search_id',
'placeholder' => 'numbers_test',
'autofocus' =>"autofocus",
'rows' => '20'
);
echo form_textarea($data,set_value('search_id'));
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
return $this->db->get();
i waiting your help for this problem
If you are getting input as comma separated ids like in string 1,5,4,8 etc from $this->input->post('search_id') then update your code like this
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
// Explode string into array to make where_in clause work
$this->db->where_in('p_id', explode(',', $this->input->post('search_id')));
return $this->db->get();
as official docs suggest you need to provide array of options in IN clause
You have to return the result of the query. Make changes,
/* In my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
$query = $this->db->get();
return $query->result_array(); // You've to return the result of the query
Also as #Saqueib said in comments, try some debugging when in doubt,
echo $this->db->last_query(); exit;
OR
echo '<pre>'; print_r($query->result_array()); exit;
Related
This question already has an answer here:
Codeigniter model with multiple update conditions using manual where statement
(1 answer)
Closed 5 years ago.
$this->db->where('column_field1',$value1);
$this->db->where('column_field2',$value2);
For above two query we can write a single query as:
$arr = array('column_field1'=>'value1', 'columne_field2'=>'value2');
function select($arr){
.. .. ..
... .. . ..
$this->db->where($arr);
}
is there any solution for the where_in query:
function($value1, $value2){
$this->db->where_in('column_field1',$value1);
$this->db->where_in('column_field2',$value2);
}
as I have tried but didn't work:
arr = array('column_field1'=>$arr,'column_field2'=>arr2);
function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where_in($arr);
$query = $this->db->get();
return $query;
}
I want to combine the where_in condition so that i can store multiple column_field and array for it.
$this->db->where_in($arr);
where $arr contains pair of column_filed and array_of_value:
$arr = $array('column_field'=>$arr_of_value);
function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where($arr); // change here
$query = $this->db->get();
return $query;
}
If you want multiple where In then you need to write it twice....It's not possible in single statement.
$this->db->where_in('field1',$cond1);
$this->db->where_in('field2' , $cond2);
Note: Where_in is similar to where id IN (1,2,3...)but in your case you are doing multiple where condition.
Creating custom method is a reasonable solution for this, you can either extend database's active record class or write custom function which takes array and call where_in like below
function multiple_where_in($array){
foreach($array as $key => $data){
$this->db->where_in($key, $data);
}
}
And call like below
function select_in($arr)
{
$this->db->select('*');
$this->db->from('your_table');
$this->multiple_where_in($arr); // call local function created above
$query = $this->db->get();
return $query;
}
// create array like below, fieldname and field_values inside array
$arr = array(
'column_field1'=>array('value1','value2'),
'columne_field2'=>array('value2','value3')
);
// call your select method
$this->select_in($arr);
You can try this solution for your problem.
$this->db->select('*');
$this->db->from('table');
if(!empty($cond1) && !empty($cond2)){
$this->db->where("field1 IN (".$cond1.") AND field2 IN (".$cond2.") ",null,false);
}
$query = $this->db->get();
return $query;
I hope this will helps you. Thanks!
This is a headache issue for long time. I have the following code in codeigniter php active record.
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_detail', 'order_detail.order_id = orders.o_id');
$this->db->join('profiles','profiles.id = orders.buyer_id','left');
$this->db->where('orders.o_id', $order_id);
$this->db->group_by('orders.o_id');
$query = $this->db->get();
$order_details_by_order_id_result = (object)$query->result();
result
var_dump($order_details_by_order_id_result);exit; //see below
object(stdClass)[34]
public 0 =>
object(stdClass)[37]
public 'xx_id' => string '13' (length=2)
public 'yy_iud' => string '22' (length=10)
public 'order_total' => string '25.00' (length=5)
public 'shipto_fname' => string 'dan' (length=3)
public 'shipto_lname' => string 'theman' (length=6
on my controller i called the above function as follow:
$order_details = $this->orderdetails->get_orderdetail_of_buyer($oid);
$data['order_details'] = $order_details; //pass this to the view
$this->load->view('dashboard/order_detail_view',$data);
and i want to send the result to the view (/order_detail_view.php)
Order #o_id ;?>
$order_details->o_id; //why on earth this expression show me error
i guess the problem is
object(stdClass)[34]
public 0 =>
How could i ever solve this because i fetch only one order at a time. Thanks
row() method will return a single row for you query.
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->xx_id;
}
In CodeIgniter, $query->result() returns an array of objects, each element represents the row in the result set. No need to convert it into object; use...
$result = $query->result();
if ($result) {
$order_details_by_order_id_result = $result[0];
}
... instead.
For your original code, there's actually no way to get the content object: PHP objects that are results of conversion from array do not play nicely with numeric keys (you can read more about this here).
I am new to CodeIgniter .Can some one please help me by telling how to pass $row['main_products_cat_id']; in model to 'id'=> 'my id here', in controller. I tried so many ways but I failed.
Thanks!!!
This is my model
function cart_product_records($id){
$this->db->from('tbl_sub_products');
$this->db->where('tbl_sub_products.sub_product_id', $id);
$query = $this->db->get();
foreach ($query->result_array() as $row){
echo $row['main_products_cat_id'];
echo $row['sub_product_id'];
echo $row['sub_product_name'];
echo $row['sub_product_description'];
echo $row['sub_product_image'];
echo $row['sub_product_price'];
}
return $query->result();
}
this is my controller
function shopping_cart(){
$id = $this->input->get('id');
if($query = $this->mod_products->cart_product_records($id)){
$data['result'] = array(
'id' => 'my id here',
'qty' => my qty here,
'price' => my price here,
'name' => 'my product name here'
);
}
}
First - you should not echo data in your model - you should really only echo data in your view file.
The way that your data is flowing is first you are passing $id from your controller to your cart_product_records function in the mod_products model. The model then runs a query and gets data from your database. This data is represented by $query->result_array() or $query->result() (you're using both of these but you should only use one, once).
So you then have return $query->result() which sends your data back to your controller. The data will be stored in the $query variable, because you have $query = $this->mod_products->cart_product_records($id).
If I were you I would add an !empty() check to the if statement, so it would look like this:
if( !empty($query = $this->mod_products->cart_product_records($id)) ){
Then, depending on what data you want to get from $query, you're either going to want to construct a loop (which would enable you to extract multiple ids, prices, etc), or you could retrieve specific values from $query, like $query[0][main_products_cat_id] (assuming $query is an array) or $query[0]->main_products_cat_id (if it's an object).
I hope that helps!
Your $query holds the database results object. So, you'll do this in your $data array in your controller.
$data['result'] = array(
'id' => $query->main_products_cat_id,
// carry on...
);
I dosen't make any sense to write
echo $row['main_products_cat_id'];
....
In your loop create array as
foreach ($query->result_array() as $row){
$return_array[] = $row;
}
return $return_array
and in controller, you can access as
$query['your_id'];
I'm new to codeIgniter and unit test, and i want to test this query that return a multidimentionnal array
public function get_ficheFrais($selected_visiteur_np) {
$query = $this->db->select('ff.mois')
->from('fichefrais ff')
->join('utilisateur u', 'u.id = ff.idVisiteur', 'inner')
->where('ff.idVisiteur', $selected_visiteur_np)
->where('ff.idEtat', 'VA')
->get();
return $query->result();
}
this is the var_dump of the query:
array (size=1)
0 =>
object(stdClass)[42]
public 'mois' => string '201211' (length=6)
but i can't find out how to write it in the test function. i've tried this:
$this->visiteur_model->get_ficheFrais('a131');
$data['ficheFrais'] = $this->visiteur_model->get_ficheFrais('a55');
$this->unit->run($data['ficheFrais'], (array ('mois'=>'201211')), "testing get_ficheFrais function");
$this->load->view('visiteur/v_tests');
can anyone help or has a better solution on how to test queries that return arrays?
I think were you are going wrong is you are expecting your get_ficheFrais function to return an array but its actually returning an object. If you want it to return an array you want to change it from
return $query->result();
to
return $query->result_array();
So bearing in mind it returns an object at the moment you want to change your unit test to
$this->unit->run($data['ficheFrais']->mois, 201211, "testing get_ficheFrais function");
I'm trying to create pagination in codeigniter and I have it working, but I have a small issue. It seems to be loading all the entries in my database and not the selected ones I want.
public function original_count() {
$this->db->where('type', 'Original');
return $this->db->count_all("story_tbl");
}
I know that whats happening is that the last line is overrighting my previous statements. I can't seem to find a way around it though. I tried just a staight sql statement and then returning it, but I could not get that to work either.
this was my statement...
SELECT COUNT(*) FROM story_tbl where type = 'Original';
Some help would much appreciated! :)
CI has inbuilt count method
count_all_results()
Permits you to determine the number of rows in a particular Active Record query. Queries will accept Active Record restrictors such as where(), or_where(), like(), or_like(), etc. Example:
https://www.codeigniter.com/userguide2/database/active_record.html
$total_count = $this->db->count_all_results('story_tbl', array('type' =>'Original'));
You could also use the built-in num_rows() function...
$query = $this->db->where('type', 'original')->get('story_tbl');
return $query->num_rows();
First Try this one.
$query = $this->db->where('tbl_field', 'value')
->get('your_table');
return $query->num_rows();
Besides this Codeigniter has it own function like the following.
$this->db->where('tbl_field', 'value')
->get('your_table');
return $this->db->count_all_results();
Or use this.
return $this->db->count_all('your_table');
Where wont work on count_all condition.. you can use below method to find out total number of rows..
public function count_all() {
$this->db->select ( 'COUNT(*) AS `numrows`' );
$this->db->where ( array (
'type' => 'Original'
) );
$query = $this->db->get ( 'story_tbl' );
return $query->row ()->numrows;
}