Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a set of data from mysql query as per below

hub       | month | frequency 
GALAXY    | 10    | 1
GALAXY    | 11    | 2 
GALAXY    | 12    | 1 
LEVERAGES | 10    | 3 
LEVERAGES | 12    | 2 

and i would like to populate the data to json format using json_encode as this :

[{"name":"GALAXY","total":"4","articles":[["10","1"],["11","2"],["12","1"]]},{"name":"LEVERAGES","total":"5","articles":[["10","3"],["12","2"]]}]

But i couldn't get the right json. Below is my code:

$root = array();
$aColumns = array('hub', 'month', 'frequency');
$tangos = $this->Report_Model->getMonthHubTango();


    foreach($tangos->result_array() as $aRow)
                    {
                        $row = array();
                        $total = 0;

                        foreach($aColumns as $col)
                        {
                            $row[] = $aRow[$col];
                            $total += $aRow['frequency'];
                                                    $hub = $aRow['hub'];

                        }
                        $main['name'] = $hub;
                        $main['total'] = $total;
                        $main['articles'][] = $row;                 

                    }

                    $root[] = $main;
echo json_encode($root);

Anyone? Thanks in advance..

share|improve this question
 
Where is $hub defined in your code? –  kingkero Jan 4 at 2:09
 
It looks like $main['name'] and other two $main are being overwritten with every foreach iteration, so you will get only last iteration results to your $root[] –  Nikolay Jan 4 at 2:11
add comment

2 Answers

I think, you should add $root[] = $main; into foreach block, so every $main array will be put into $root matrix:

foreach($tangos->result_array() as $aRow)

{
    $row = array();
    $total = 0;

    foreach($aColumns as $col)
    {
        $row[] = $aRow[$col];
        $total += $aRow['frequency'];

    }
    $main['name'] = $hub;
    $main['total'] = $total;
    $main['articles'][] = $row;                 
    $root[] = $main;
}

Plus, you have $hub undefined in loop. So, $main['name'] will probably be undefined.

share|improve this answer
1  
Or $root[]['name'] and so forth. –  Nikolay Jan 4 at 2:14
 
@Nikolay Yes, and get rid of $root completely –  user4035 Jan 4 at 2:15
add comment
$root = array();
$tangos = $this->Report_Model->getMonthHubTango();


    foreach($tangos->result_array() as $aRow)
                    {
                        $row = array();
                        $total = 0;

                        foreach($aColumns as $col)
                        {
                            $row[] = $aRow[$col];
                            $total += $aRow['frequency'];

                        }
                        $main['name'] = $hub;
                        $main['total'] = $total;
                        $main['articles'][] = $row;                 
                        $root[] = $main;
                    }


echo json_encode($root);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.