1

I'm finding this hard to explain to know how to search for an answer for this. With the help of you lovely people yesterday I cut down ~1200 MySQl queries to just 2, but now I'm having trouble doing anything with the results. This is the array I'm getting back (snippet)

Array 
( 
[0] => Array ( [total] => 7 [closedby] => Adam_Howard [priority] => P3 [withinfix] => 0 ) 
[1] => Array ( [total] => 20 [closedby] => Adam_Howard [priority] => P3 [withinfix] => 1 ) 
[2] => Array ( [total] => 3 [closedby] => Adam_Howard [priority] => P4 [withinfix] => 0 ) 
[3] => Array ( [total] => 5 [closedby] => Adam_Howard [priority] => P4 [withinfix] => 1 ) 
[4] => Array ( [total] => 3 [closedby] => Adam_Jones [priority] => P3 [withinfix] => 0 ) 
[5] => Array ( [total] => 2 [closedby] => Adam_Jones [priority] => P3 [withinfix] => 1 ) 
[6] => Array ( [total] => 11 [closedby] => Adrian_Dimmock [priority] => P2 [withinfix] => 0 ) 
[7] => Array ( [total] => 39 [closedby] => Adrian_Dimmock [priority] => P2 [withinfix] => 1 ) 
[8] => Array ( [total] => 20 [closedby] => Adrian_Dimmock [priority] => P3 [withinfix] => 0 ) 
[9] => Array ( [total] => 301 [closedby] => Adrian_Dimmock [priority] => P3 [withinfix] => 1 ) 
[10] => Array ( [total] => 2 [closedby] => Adrian_Dimmock [priority] => P4 [withinfix] => 0 ) 
[11] => Array ( [total] => 33 [closedby] => Adrian_Dimmock [priority] => P4 [withinfix] => 1 ) 
[12] => Array ( [total] => 37 [closedby] => Adrian_Hull [priority] => P2 [withinfix] => 0 ) 
[13] => Array ( [total] => 1211 [closedby] => Adrian_Hull [priority] => P2 [withinfix] => 1 ) 
[14] => Array ( [total] => 4 [closedby] => Adrian_Hull [priority] => P3 [withinfix] => 0 ) 
[15] => Array ( [total] => 771 [closedby] => Adrian_Hull [priority] => P3 [withinfix] => 1 ) 
[16] => Array ( [total] => 4 [closedby] => Adrian_Hull [priority] => P4 [withinfix] => 1 )
)

The final output I'm working towards is the total number, per person, per priority, per withinfix, so for example, the last 2 lines could give a result like:

$AdrianHullP3Fix1 = 771;

$AdrianHullP4Fix1 = 4;

But I have no idea how to go about this from the results I have, can anyone recommend a solution or just the correct php method/function for me to investigate?

If it helps, this is the code I'm using to create the array from the query:

while($row = mysql_fetch_assoc($Query)){
$results[] = $row;
}
2
  • What is this: $AdrianHullP3Fix1?. is it any variable name or you want it as specific you have shown? Commented Mar 11, 2015 at 9:19
  • it's a variable I want to make from the array (a mix of closedby, priority and withinfix) and I want to give it a value of the total Commented Mar 11, 2015 at 9:37

3 Answers 3

1

Hope this will help you.

// this will be the word that you want to attached to a variable
define('WORD_TO_ADD','Fix');

while($row = mysql_fetch_assoc($Query)){
$variableName = str_replace("_","",$row['closedby']).$row['priority'].WORD_TO_ADD.$row['withinfix'];
    $$variableName = $row['total'];
}
echo $AdrianHullP3Fix1; // o/p 771 
echo $AdrianHullP4Fix1; // o/p 4 
Sign up to request clarification or add additional context in comments.

Comments

1
while($row = mysql_fetch_assoc($Query)){
    ${$row['closedby'].$row['closedbypriority'].$row['closedbywithinfix']}  = $row['total'];
}

${} will create new dynamic variables

Hope this help

1 Comment

mysql_fetch_assoc will return associative array you will not able to access array value via -> operator.
0

May not be graceful, but I've figured it out using implodes and explodes

while($row = mysql_fetch_assoc($Query)){
$rowString = implode ("_", $row); //make the row into a string
$minusTotalParts = explode("_", $rowString); // explode it by the underscores
$minusTotal = $minusTotalParts[0]."_".$minusTotalParts[1].$minusTotalParts[2]."Fix".$minusTotalParts[3]; //create the variable string
eval('return $'.$minusTotal.' = '.$minusTotalParts[4].';'); //create the variable of $firstname_surnameP2Fix0 where the SLA and withinfix changes each time, it then give it the total value
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.