I have an sql query which returns values. I would want to add new values with keys so that I can use it to my next controller which is these values doesn't come from the query result.
My Model:
$paymentDetails = $this->db->query($sql);
$payments = $paymentDetails->result();
//these are the values I wanted to add to the result for the $payments
$amountDue = 'Sample';
$change = 'Sample';
$result = array_merge($payments, array(
"AmountDue" => $amountDue,
"Change" => $change
));
if($result){
return $result;
}else{
return false;
}
I wonder why array_merge() doesn't work. In my view, values fetched from the SQL Query did returned, but the added (AmountDue and Change) is not found.
Please help me. Thank you so much! Ya'll be a big help for my project. :)
because result() function returns the query result as an array of objects, or an empty array on failure. It is just an alias of result_object().
You have to use result_array() which returns the query result as a pure array.
I hope this will help you.
Related
I am currently using the code below in Prestashop to retrieve a cart id.
public function hookDisplayPDFInvoice($params) {
$order_invoice = $params['object'];
$id_order = (int)$order_invoice->id_order;
$sql = 'SELECT id_cart FROM '._DB_PREFIX_.'orders WHERE id_order="'.$id_order.'"';
//example id_cart
$id_cart = Db::getInstance()->execute($sql);
return $id_cart;
In database, there are id_cart and id_timeslot. Table is called ps_cart_timeslot.
I am quite stuck as I am baffled as to why the data return is 1 for any data I am retrieving.
$id_order is fine, it is returning the right value. Any data select I am querying will return 1.
Am I missing anything? Pardon me if this is a silly mistake.
Thank you.
For selects use Db::getInstance()->executeS($sql); or to get single value use Db::getInstance()->getValue($sql);
However since PS 1.6 you should be using query builder.
$query = new DbQuery();
$query->select('id_cart')
->from('orders')
->where('id_order = ' . (int)$id_order);
Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
// Or array of values
Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
execute() method returns only true or false while executeS() will return an array of select results (method can only be used for select queries) and getValue() will return the first value found in result.
I am trying to write a CSV in Codeignter for a result set fetched from db.
I am already using the solution mentioned here. Reports in Codeigniter
But the issue is that it writes the whole table. Now I want to fetch and write specific records. So here is my model code.
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get()
->result_array();
return $result;
It gives me this error.
You must submit a valid result object
If I change the model code to this
return $query = $this->db->get('hangtag_request');
It works perfectly. Is there any way I can make my model code to return the results in the form of DB object? Coz it seems thats the form we need.
Thanks.
If you need result object then you shouldn't use the result_array()
$this->db->get();
The above statement runs the selection query and returns the result.It does return in form that can be used for presentation.
result_array()
This function returns the query result as an array, or an empty array when no result is produced.
Its good to use return $query = $this->db->get('hangtag_request'); for obtaining result object.
I got it solved.
Just had to remove result_array() so the correct code becomes
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get();
return $result;
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;
}
I have a query which im trying to return the exact entry for, but instead it keeps returning the 'Array' ... how do i extract just the one 'name' in looking for?
My current query looks like so
$query = $this->db->query('SELECT name FROM '.$this->table_name.' WHERE id =1');
return $query->result();
Basically, i just want to return the actual name, not an array
You need to dig into the objects returned by the query. row() returns the first row in a result set:
$row = $query->row();
return $row->name;
result() returns an array of objects representing the rows in the result set. You need to get the object itself, then get it's 'name' property.
It's also worth checking $query->num_rows() to make sure you have results:
if ($query->num_rows() > 0) {...}
Say you construct an activerecord query that will always just return a single value, how do you just address that single value instead of getting an array in return? For instance I am using an ActiveRecord query to return the SUM of a single column, it will only return this one single SUM, instead of having to parse the array is there a way to assign the value as a function return equal to that value instead of getting an array?
$query = $this->db->get(); // your ActiveRecord query that will return a col called 'sum'
echo $query->row('sum');
$output = $this->db->query("YOUR QUERY")->row()->fieldname;
OR
$output = $this->db->get("TABLE NAME")->row()->fieldname;
Where fieldname is the name of the field you want the output of in your query.
this isn't the prettiest way. i can't find a way to do it with the active record api. but here's a one-liner to get it.
$result = array(
array('sum' => '23')
);
echo current( current( $result ) );