0

heres the function: http://pastebin.com/tQLjzzbH

I want it to return this:

{"years":[
    {
        "yName":"2011",
        "yAlbums:[
            {
                auID:"1234",
                aID:"456",
                etc..
            },
            {
                auID:"12345",
                aID:"4567",
                etc..
            }
    },
    {
        "yName":"2010",
        "yAlbums:[
            {
                auID:"2234",
                aID:"556",
                etc..
            }
    }
]}

but its only returning this:

{"years":[
{"yName":"2011"},
{"yName":"2010"}]}

Been trying to get this for ages, am totally lost now. Some help would be appreciated.

Thanks.

1 Answer 1

1

You have read your result to the very end by

while($data2 = mysql_fetch_assoc($result))
{
    $yearsArrayRaw[] = $data2['album_year'];
}

Add mysql_data_seek($result,0) before inner while, to line 36. Also, I would suggest rewriting this function to have single loop, using something like

$year_albums= array();
while ($data = mysql_fetch_assoc($result)){
    if (empty($years[$data['album_year']])){
        $year_albums[$data['album_year'] = array(
            'yName'=>$data['album_year'],
            'yAlbums'=>array()
        );
     }
     //...album creation logic here...//
     $year_albums[$data['album_year']['yAlbums'][] = $album;
}
//Converting arrays into objects
$years = array();
foreach ($year_albums as $year){
    $years[] = (object)$year;
}
json_encode($years);
Sign up to request clarification or add additional context in comments.

1 Comment

mysql_data_seek fixed it :), true the function does need rewriting, but doing it along the lines you suggested will not exactly work given you don't know the input data. But Thanks a lot all the same :)

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.