Loops within loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdalts3759
Forum Newbie
Posts: 4
Joined: Wed Mar 07, 2012 4:27 am

Loops within loops

Post by kdalts3759 »

Hi Folks... my first post here as a novice developer....

I'm trying to build a line chart with mulitple lines using a very neat product. The concept is simple; partners (Dispatched_TPM), months (Calendar_Month) and values (Completed). The number of partners could vary month to month and the dates simple grow as more data is added so this cannot be hard-coded. Here is what I have so far....

Code: Select all

$query = "SELECT Dispatched_TPM, Calendar_Month, " . // row 0
	"COUNT(Dispatched_TPM) AS NumDispatches, " . //total number of dispatches row 1
	"SUM(IF(STATUSTEXT = 'Completed',1,0)) AS Completed, " . //completed status  row 2
    "SUM(IF(STATUSTEXT = 'Cancel',1,0)) AS Cancel, " . //Cancel status  row 3
	"partners.shortname " . //row 4
	"FROM dispatches INNER JOIN partners ON (partner_name = Dispatched_TPM AND GEO = '$CUST_ADX_GEO')" .
	"WHERE CUST_ADX_GEO = '$CUST_ADX_GEO' " .
	"GROUP BY Dispatched_TPM, STR_TO_DATE(Calendar_Month, '%d-%M-%y') ";
	"ORDER BY STR_TO_DATE(Calendar_Month, '%d-%M-%y') ";
		
$results = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_assoc($results);
    if ($results) {
    
        $labels = array();
        $data   = array();
        while ($row = mysql_fetch_assoc($results)) {
            $labels[] = $row["Dispatched_TPM"];
            $data[]   = $row["Completed"];
        }

        // Now you can aggregate all the data into one string
        $data_string = "[" . join(", ", $data) . "]";
        $labels_string = "['" . join("', '", $labels) . "']";
    } else {
        print('MySQL query failed with error: ' . mysql_error());
    }
echo $data_string;
?>

My output string is
[text][2, 6, 2, 3, 4, 8, 5, 4, 8, 123, 112, 86, 67, 60, 477, 507, 487, 448, 520, 60, 54, 42, 56, 52, 11, 8, 14, 12, 12, 151, 148, 134, 141, 172, 10, 5, 6, 5, 7, 393, 421, 390, 411, 504][/text]
Now this works fine but I need to break the array into smaller arrays, 1 for each partner each with 4 values (1 for each month) like this...
[text][2, 2, 6, 2]
[4, 8, 5, 4]
[123, 112, 86, 67]
[477, 507, 487, 448]
[60, 54, 42, 56]
[11, 8, 14, 12]
[151, 148, 134, 141]
[10, 5, 6, 5]
[393,421,390,411]
[/text]

I can then concatanate the arrays to create my final string variable to look like this -
[text]
[[2, 2, 6, 2], [4, 8, 5, 4], [123, 112, 86, 67], [477, 507, 487, 448], [60, 54, 42, 56], [11, 8, 14, 12], [151, 148, 134, 141], [10, 5, 6, 5], [393,421,390,411]]
[/text]

I also need to create 2 seperate arrays, 1 for the date (Calendar_Month) and 1 for the Partner (Dispatched_Partner)

Any help here would be most welcome... many thanks in advance...
kon
Forum Newbie
Posts: 19
Joined: Sat Mar 03, 2012 5:43 am

Re: Loops within loops

Post by kon »

This is not actually on what you are asking, but have you thought turning this in object oriented programming. As I see it the “smaller arrays” are objects and you could have an indexed objects list to do the job for you. Again I am not saying that this answer your question but just have in mind that going OOP way may seem harder at beginning but could save you time afterwards in such problems.
kdalts3759
Forum Newbie
Posts: 4
Joined: Wed Mar 07, 2012 4:27 am

Re: Loops within loops

Post by kdalts3759 »

@kon

Actually you completely lost me on that... all I know right now is simple PhP.
kon
Forum Newbie
Posts: 19
Joined: Sat Mar 03, 2012 5:43 am

Re: Loops within loops

Post by kon »

I just told you what you should consider in the future. Now you know simple PHP but the time and effort learning object oriented PHP will pay of. In your question here is one way of doing that… but again to treat arrays like objects isn’t a good way…

Code: Select all

$originalArray = array(2, 6, 2, 3, 4, 8, 5, 4, 8, 123, 112, 86, 67, 60, 477, 507, 487, 448, 520, 60, 54, 42, 56, 52, 11, 8, 14, 12, 12, 151, 148, 134, 141, 172, 10, 5, 6, 5, 7, 393, 421, 390, 411, 504);
$newArray = array(); 
$row = 0; 
$column = 0; 
for($i=0; $i<count($originalArray); $i++)
{
  if($column == 4)
  {
    $column = 0; 
    $row ++;  
  }
  $newArray[$row][$column] = $originalArray[$i];
  $column ++;
}
var_dump($newArray);
kdalts3759
Forum Newbie
Posts: 4
Joined: Wed Mar 07, 2012 4:27 am

Re: Loops within loops

Post by kdalts3759 »

Hi Kon

So this is great and I figured out wht you code does - thanks very much

Now I have ...

Code: Select all

array
  0 => 
    array
      0 => string '2' (length=1)
      1 => string '2' (length=1)
      2 => string '6' (length=1)
      3 => string '2' (length=1)
      4 => string '3' (length=1)
  1 => 
    array
      0 => string '4' (length=1)
      1 => string '8' (length=1)
      2 => string '5' (length=1)
      3 => string '4' (length=1)
      4 => string '8' (length=1)
  2 => 
    array
      0 => string '123' (length=3)
      1 => string '112' (length=3)
      2 => string '86' (length=2)
      3 => string '67' (length=2)
      4 => string '60' (length=2)
  3 => 
    array
      0 => string '477' (length=3)
      1 => string '507' (length=3)
      2 => string '487' (length=3)
      3 => string '448' (length=3)
      4 => string '520' (length=3)
  4 => 
    array
      0 => string '60' (length=2)
      1 => string '54' (length=2)
      2 => string '42' (length=2)
      3 => string '56' (length=2)
      4 => string '52' (length=2)
  5 => 
    array
      0 => string '11' (length=2)
      1 => string '8' (length=1)
      2 => string '14' (length=2)
      3 => string '12' (length=2)
      4 => string '12' (length=2)
  6 => 
    array
      0 => string '151' (length=3)
      1 => string '148' (length=3)
      2 => string '134' (length=3)
      3 => string '141' (length=3)
      4 => string '172' (length=3)
  7 => 
    array
      0 => string '10' (length=2)
      1 => string '5' (length=1)
      2 => string '6' (length=1)
      3 => string '5' (length=1)
      4 => string '7' (length=1)
  8 => 
    array
      0 => string '393' (length=3)
      1 => string '421' (length=3)
      2 => string '390' (length=3)
      3 => string '411' (length=3)
      4 => string '504' (length=3)
How do I now create the following variable string please?
[[2, 2, 6, 2], [4, 8, 5, 4], [123, 112, 86, 67],[477, 507, 487, 448],[60, 54, 42, 56],[11, 8, 14, 12],etc...]]
Post Reply