PHP not showing all data in array from loop - php

Outputting data to an array
public function GetProfitByMonth($d1, $d2){
$sql=odbc_exec($this->connections, "select con.ID, con.ShortName as NameAK,
ROUND(ISNULL(SUM(tik.TotalSub)-SUM(tik.TotalAG), 0), 2) as Profit,
MONTH(tik.DEALDATE) as month_num
from Counteragent as con
left join Tickets as tik on tik.AgentID=con.ID
where con.ValueType in (2,3) and con.Active='1' and (DEALDATE between '".$d1."' and '".$d2."')
group by con.ID, con.ShortName, MONTH(tik.DEALDATE)
order by con.ShortName, MONTH(tik.DEALDATE) ;");
$tblResult=array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[]=$row;
}
odbc_free_result($sql);
Array
(
[0] => Array
(
[Profit] => 11218.30
[month_num] => 8
)
[1] => Array
(
[Profit] => 1152.15
[month_num] => 8
)
....
[4] => Array
(
[Profit] => 119837.81
[month_num] => 8
)
)
I need to display only month_num => Profi from the array
did this way
$tblResult = array();
while ($row = odbc_fetch_array($sql)) {
$tblResult[$row['mon_num']] = $row['Profit'];
}
odbc_free_result($sql);
return $tblResult;
but as a result in the array shows only the last value:
Array
(
[8] => 119837.81
)
How to make an array show all the data?

This is because month_num = 8 for all entries in your original array.
Your while loops first two iterations effectively evaluate to:
$tblResult[8] = 11218.30
$tblResult[8] = 1152.15
To get the result in the form you want, you need month_num to be unique.

Related

How to group multidimensional array by more keys

this is my array source.
//array for all reception
$reception = array(
array('code'=>'AB202','order'=>'111111','qty'=>'50.000'),
array('code'=>'AB202','order'=>'111111','qty'=>'50.000'),
array('code'=>'AB202','order'=>'222222','qty'=>'50.000'),
array('code'=>'AB300','order'=>'666666','qty'=>'100.000'),
array('code'=>'AB300','order'=>'666666','qty'=>'120.000'),
array('code'=>'AB300','order'=>'777777','qty'=>'120.000')
);
the code AB202 equals order 111111 AND 222222
the order 111111 equals qty=>50, qty=>50
the order 222222 equals qty=>50
the code AB300 equals order 666666 AND 777777
the order 666666 equals qty=>100, qty=>120
the order 777777 equals qty=>120
I want group by code and order like this
Array
(
[AB202] => Array //array code
(
[111111] => Array //array order
(
[0] => 50.000 // qty
[1] => 50.000
)
[222222] => Array
(
[0] => 50.000
)
)
[AB300] => Array
(
[666666] => Array
(
[0] => 100.000
[1] => 120.000
)
[777777] => Array
(
[0] => 120.000
)
)
)
loop thru all receptions, then check if code key or order key exists, make one if not, then append each qty
$newArr = array();
foreach ($reception as $code) {
if (!array_key_exists($code['code'], $newArr)){
$newArr[$code['code']] = array();
}
if (!array_key_exists($code['order'], $newArr[$code['code']])) {
$newArr[$code['code']][$code['order']] = array();
}
array_push($newArr[$code['code']][$code['order']], $code['qty']);
}

Specific value in an array does not work

I have a question about my array. How can I get only data of milestone in my milestone array?
I have a query which is a prefixed array. I get data of milestones and milestonefases (this is milestone parts).
This is my code:
$stones_fases = array();
while ($row = $db->fetchassoc($result)){
$milestonefase = array();
$milestone = array();
foreach ($row as $mkey => $mvalue){
$milestone[$mkey] = $mvalue;
foreach ($row as $fkey => $fvalue){
$milestonefase[$fkey] = $fvalue;
}
}
if (!isset($stones_fases[$milestone['milestone_id']])){
$stones_fases[$milestone['milestone_id']] = $milestone; //['client']['milestone_verkocht_id']
}
$stones_fases[$milestone['milestone_id']][$milestonefase['milestonefase_id']] = $milestonefase['milestonefase_titel'];
}
I get this:
Array
(
[int] => Array
(
[milestone_id] => int
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => 99
[milestonefase_id] => 10
[milestonefase_titel] => string
[milestonefase_milestone_id] => 6
[10] => string
[11] => string
)
)
But I want this:
Array
(
[int] => Array
(
[milestone_titel] => string
[client] => string
[milestone_verkocht_id] => int
[10] => string
[11] => string
)
)
My query is this:
$project = $_COOKIE['project'];
$query = " SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '{$project}' ";
$result= $db->query($dbh, $query);
I solved my own problem!
If i do this:
while ($row = $db->fetchassoc($result)){
$stones_fases[$row['milestone_id']]['milestone_titel'] = $row['milestone_titel'];
$stones_fases[$row['milestone_id']]['milestone_client'] = $row['client'];
$stones_fases[$row['milestone_id']]['milestonesfases'][$row['milestonefase_id']] = $row['milestonefase_titel'];
}
I get what i expected:
Array
(
[2] => Array
(
[milestone_titel] => Beheer opleveren
[milestone_client] => stackoverflow
[milestonesfases] => Array
(
[1] => Menu bouwen
[2] => Pagina beheer CMS
[3] => Projecten CMS
[4] => Portfolio
[5] => Footer inbouwen
)
)
)
Look at that bunch of code what I wrote a day ago.
Do you see that nonsens of code what I wrote?
Take a look at my code which I wrote 1 minute ago!
I was struggling this for 3 days and now I solved my own problem!

PHP - Array does not turn into two-dimensional array

I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases

Grouping Array by Value

I am looking to group my array into sub arrays when they have the same Supplier ID. I have looked at many similar questions on here but I cannot get any of them to work for me. I have tried foreach loops and even the PHP merge function. I have to use MySQLi for this so I cannot group using PDO.
This is what I have:
Array
(
[supplierID] => 1
[drugID] => 1
[costPrice] => 9.98
[reorderQTY] => 50
)
Array
(
[supplierID] => 1
[drugID] => 2
[costPrice] => 9.98
[reorderQTY] => 50
)
Array
(
[supplierID] => 2
[drugID] => 3
[costPrice] => 9.98
[reorderQTY] => 50
)
This is what I need:
Array
(
[supplierID] => 1
Array
(
[drugID] => 1
[costPrice] => 9.98
[reorderQTY] => 50
)
[drugID] => 2
[costPrice] => 9.98
[reorderQTY] => 50
)
)
Array
(
[supplierID] => 2
Array
(
[drugID] => 3
[costPrice] => 9.98
[reorderQTY] => 50
)
)
This is the PHP where I am outputting the array to get the above result:
if(isset($_POST["automatic"])){
$sql = "SELECT supplierID,drugID,costPrice,reorderQTY FROM drugs WHERE quantityInStock <= reorderLevel";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<pre>";
print_r($row);// I need to sort this array into sub array groups by supplierID key
echo "</pre>";
}
}else{
echo " <tr><td>0 results </td></tr>";
}
mysqli_close($conn);
}
Something like this will do it,
$out = [];
while($row = mysqli_fetch_assoc($result)) {
$out[$row->supplierID][] = $row;
}
Basically I use the supplier id to create an array key, then assign an empty array. Then add all rows with that supplier id to that array.
This will create a multidimensional array.

php mysqli result into an array not working as expected

I have a php /mysqli query and I want to populate an array with the results:
$query3 ="SELECT * FROM conditions";
$results = array();
if ($result = mysqli_query($conn, $query3)){
while($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
}
print_r($results);
Something is wrong here -its making arrays within arrays I think. (to be honest I am confused by this result)
How do I do this correctly!
Array (
[0] => Array ( [condition_id] => 1 [condition_name] => Epilepsy )
[1] => Array ( [condition_id] => 2 [condition_name] => ASD )
[2] => Array ( [condition_id] => 3 [condition_name] => BESD )
[3] => Array ( [condition_id] => 4 [condition_name] => HI )
[4] => Array ( [condition_id] => 5 [condition_name] => Medical )
[5] => Array ( ...
Thanks for all the help - now how should I create what I actually want which is one array with key=>value like this:
array (1=>epilepsy, 2=>ASd...) - the numbers refer to the primary key.
How do I populate an array from this query please?
Change your code as below :
while($row = mysqli_fetch_assoc($result))
{
$results[$row['condition_id']] = $row['condition_name'];
}
Move to PDO, Luke.
$results = $pdo->query("SELECT FROM conditions")->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($results);
Whoops! Is that all the code?

Categories