how to combine mysql query in CodeIgniter - mysql

I need to combine those two array into a single array.
1st query result
Array ( [0] => stdClass Object ( [p_type] => Bank [tsales] => 131000 [tdues] => 55000 [tpaid] => 75000 ) [1] => stdClass Object ( [p_type] => Cash [tsales] => 104000 [tdues] => 50000 [tpaid] => 54000 ) [2] => stdClass Object ( [p_type] => bKash [tsales] => 25000 [tdues] => 10000 [tpaid] => 5000 ) [3] => stdClass Object ( [p_type] => Pause [tsales] => 4000 [tdues] => 1000 [tpaid] => 2000 ) )
2nd query result
Array ( [0] => stdClass Object ( [dpayment] => 1000 ) [1] => stdClass Object ( [dpayment] => 5000 ) [2] => stdClass Object ( [dpayment] => 1000 ) [3] => stdClass Object ( [dpayment] => 5000 ) )

try to use array_merge. That should be very easy.

Your query result shows that you using $query->result() to get the value,
Instead of using result(), use $query->row_array();
then you get single dimensional array for both queries
then use array_merge() that would be easy

Related

mysql cant save my big decimal number

I have a number like this
34.59704151614417
and I put datatype on decimal(16,14) but it has saved as 34.59704151614400
it means mysql save just 12 digit decimal
this is my array
Array
(
[geometry] => Array
(
[type] => Polygon
[coordinates] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
[1] => Array
(
[0] => 16.875
[1] => 34.597041516144
)
[2] => Array
(
[0] => 43.2421875
[1] => 31.653381399664
)
[3] => Array
(
[0] => 64.3359375
[1] => 40.97989806962
)
[4] => Array
(
[0] => 32.34375
[1] => 48.690960390925
)
)
)
)
)
I understand
after I do json_decode of my array it reduce it's digit to 12 number
Set precision directive:
ini_set('precision', 16);
It is currently 14 and that's why the decimals get reduced. Check this link about more information

Combine 3 arrays into One

I have 3 Queries
$this->db->select('count(id) as Lead_Current_Month')->from('admin_lead_distribution');
$result1=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level1_lead_distribution');
$result2=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level2_lead_distribution');
$result3=$this->db->get()->result();
When I print it gives
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 12
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 38
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 10
)
)
How to combine 3 arrays into one?
I want the result to be:
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 60
)
)
[Lead_Current_Month] => 60(60 is sum all the 3 array values)
You can just sum up the values of the results, and I don't know why you want to store the information in any array/object, just store it in a variable.
$sum_result = $result1[0]->Lead_Current_Month + $result2[0]->Lead_Current_Month + $result3[0]->Lead_Current_Month;
echo $sum_result; //will echo 60

Return multileve array from MySql

I'm trying to return some data from a query in this format (or at least very similar)
Array
(
[0] => Array
(
[id] => 1
[name] => Gym Opening Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
[1] => Array
(
[id] => 1
[name] => Resteruant Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
)
I have this query so far, however, it throws an error Error executing PDO query: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SELECT
otg.groupName,
otg.id,
(SELECT
ot.id,
ot.label,
ot.time
FROM
opening_times ot
WHERE
ot.groupId = otg.id) as times
FROM
opening_time_groups as otg
WHERE
otg.venueId = $venueId
AND otg.active = 1
What have I gotten wrong? Or is this something I simply can't do in 1 query?
You sub query is returning more than one row. A sub query in the SELECT must only return a single row.
However it would seem that you could do this simple with a normal join, then sort the array out in the script that calls it:-
SELECT
otg.groupName,
otg.id,
ot.id,
ot.label,
ot.time
FROM opening_time_groups as otg
LEFT OUTER JOIN opening_times ot
ON ot.groupId = otg.id) as times
WHERE otg.venueId = $venueId
AND otg.active = 1

Sort array (stdClass Object)

Getting this result after array_merge of 2 arrays:
Array ( [0] => stdClass Object ( [tid] => 3954 [name] => Alleman )
[1] => stdClass Object ( [tid] => 3958 [name] => Jack )
[2] => stdClass Object ( [tid] => 3963 [name] => Aquin Catholic ));
I want to sort the array by [name]. I have been reading and searching for hours but cannot figure out how to sort passed the stdClass Object to [name].
Have tried sort, ksort, uasort, etc. wont sort passed the stdClass Object to [name].
Example
function compareItems($a, $b)
{
if ( $a->name < $b->name ) return -1;
if ( $a->name > $b->name ) return 1;
return 0; // equality
}
uasort($players, "compareItems");

How to order sql results by counting a field in CakePHP 2.0

When I perform the following:
$popular_posts = $this->Blog->find('all', array('limit' => 5));
I get the following:
Array
(
[0] => Array
(
[Blog] => Array
(
[id] => 4fcfb37d-3eb0-4ec2-a744-175c987a2b72
[title] => This is a post example2
[short_description] => You've stumbled across our blog! Welcome! Here
[created] => 2012-06-06 21:46:05
[modified] => 2012-06-07 16:01:24
)
[Reply] => Array
(
[0] => Array
(
[id] => 4fcfb305-0c58-421b-9149-175c987a2b72
)
[1] => Array
(
[id] => 4fd0ae9e-dca0-4afe-862c-1258987a2b72
)
)
),
[1] ...
[2] ...
)
How can I order the results by the number of Reply ??? (desc)?
Try this
$data = $this->Blog->find('all',array('group' =>array('Reply.id'),
'order' => array('COUNT(Reply.id) DESC'),
'limit'=> 5));