0

I will try and explain this without causing any confusion, first here is the relevant code:

$week_start_dates = array($start_of_last_week, $start_of_last_week_last_year, $start_of_previous_week);
$week_end_dates   = array($end_of_last_week, $end_of_last_week_last_year, $end_of_previous_week);
$weekname         = array('TW', 'LY', 'PW');

Basically what these arrays contain are start and end dates for the following query:

$leads = array();

for ($i = 0; $i < sizeof($week_start_dates) ; $i++)
{
        $sql = '
    SELECT
        email.country,
            email_dailytotal.summarydate,   
            sum(email_dailytotal.conversions) as leads      
        FROM email_dailytotal
            left join email on email.email_id = email_dailytotal.email_id
            left join email_type on email.type = email_type.type_id
        WHERE                
            email_dailytotal.summarydate >= "' . $week_start_dates[$i] . '" and email_dailytotal.summarydate <= "' . $week_end_dates[$i]  . '" and email.business = "' . $business .'" and email.business_division = "' . $businessdivision .'"
    GROUP BY email.country
    ';

$dbresult = mysql_query($sql);

while($results = mysql_fetch_array($dbresult)) {
        $country        = $results['country'];
        $no_leads       = $results['leads'];

        array_push($leads, array(
            $country => array(
               $weekname[$i]=>$no_leads
            )
        ));
}

The output is as follows:

[0] => Array
    (
        [uk] => Array
            (
                [TW] => 10334
            )

    )

[1] => Array
    (
        [de] => Array
            (
                [TW] => 315
            )

    )

 [2] => Array
    (
        [uk] => Array
            (
                [LY] => 18579
            )

    )

[3] => Array
    (
        [de] => Array
            (
                [LY] => 401
            )

    )

    [4] => Array
    (
        [uk] => Array
            (
                [PW] => 13410
            )

    )


etc...

When I'd love it to be:

[0] => Array
    (
        [uk] => Array
            (
                [TW] => 10334
                [LY] => 18579
                [PW] => 13410
            )

    etc...

    )
2
  • I am confused, are you using UNIX Timestamps? Commented May 25, 2012 at 13:32
  • the dates aren't an issue, it's the structuring of the array that's the problem. Commented May 25, 2012 at 13:33

2 Answers 2

1
while($results = mysql_fetch_array($dbresult)) {
        $country        = $results['country'];
        $no_leads       = $results['leads'];

        if( !isset($leads[$country]) ){
            $leads[$country] = array();
        }
        $leads[$country][$weekname[$i]] = $no_leads;
}
Sign up to request clarification or add additional context in comments.

1 Comment

that's perfect, thank you! I will accept in a few minutes when it lets me.
1

Try changing it into this:

$leads[$country] = array(
           $weekname[$i]=>$no_leads
        );

1 Comment

thanks, that changes it, seems to only iterate once - it only outputs the `PW' figures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.