We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17422
    • 225 Posts
    Hi,

    i’ll like with a snippet create a json array.

    i send to my snippet a id of resource parent and in the snippet, i will like generate a array json with all childs resources.

    exemple :

    $collection = $modx->getCollection(’modResource’, $criteria);
    $ArrayJson=array();
    foreach ($collection as $item) {
    $ArrayJson.=array_merge(
    $ArrayJson
    ,array(
    ’pagtitle’ => $item["pagetitle"]
    ,’content’ => $item["content"]
    ,’menuindex’=>$item["content"]
    )
    }

    $output=json_encode($ArrayJson);
    return $output;



    Thanks for your help.

    Laurent
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      What exactly do you need help with? The more clear you are with asking questions, the easier we can help you.


      Some things I noticed:

      As you’re using an object ($modx->getCollection returns an array of objects that represent the resources), you’ll need to use:
      $item->get('pagetitle');

      instead of
      $item['pagetitle'];

      Content is available with $item->getContent();

      The function $modx->toJSON($array); uses on all php configurations, even in those versions (think it’s < PHP 5, but not sure) that don’;t have json_encode yet.

      Instead of array_merge to add new things to the array I would simply call it like this:

      $arrayJson[] = array(
        ..
        ..
      );


      Also you put $criteria in the $modx->getCollection but you didn’t make (or show) that array with where clauses.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 17422
        • 225 Posts
        thanks for your response.

        I understand that I must further develop my questions but I am holding back my English smiley

        explication :

        i use ajax for load a resource, with a jquery class, i want load, in a json array, all the child of parent resource.
        for that, my snippet recover the html in all resources with this
        $url = $modx->makeUrl($IdApres, '', '', 'full');
            if($http= fopen($url,"r")){
            while(!feof($http)){
            $data .=fgets($http,1024);        
            }
            fclose($http);
            }
            preg_match_all('/(<!--DebutContenuArtiste-->)(\W.*)(<!--FinContenuArtiste-->)/imsU',$data,$result);
            foreach($result[2] as $ContentRessource){
            $ContentRessource;
            }
        


        create a json array

        [
        {"idressource1":[
            {
            "contentHTML":" here, the code html of my ressource1"
            }
        ]},
        {"idressource2":[
            {
            "contentHTML":" here, the code html of my ressource 2"
            }
        ]},
        {"idressource3":[
            {
            "contentHTML":" here, the code html of my ressource 3"
            }
        ]}
        ]
        




        and in my class jquery. I’ll look in the table json the content that I want to display
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          It’s no problem if your mother language isn’t English, just makes it harder for us to answer, hehe.


          Maybe it’s an idea to try to get support in the French Community so you can simply ask & explain in French? (I noticed the french comments in your code).
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.