We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9572
    • 56 Posts
    I have a PHP script that echos an array and I would like to specify the location to a specific part of the array in the snippet call
    (e.g. {{snippetname &array_name="sample_array" &array_location="2"}} which would print the data located in sample_array[2] which is created in the snippet.

    Any ideas as to how to how I could take the snipppet call and integrate it in my currently existing PHP script?
      • 29181
      • 480 Posts
      In your current PHP snippet you could do something like this:

      $snippetOutput=$modx->runSnippet(
      	'snippetname',
      	array(
      		array_name=>"sample_array",
                       array_location=>2)
      	); 


      which will retrieve the results of snippetname and place them in the variable $snippetOutput

      Hope taht is what you are after.
        Adrian Lawley: www.adrianlawley.com
        • 9572
        • 56 Posts
        So even though its in the same snippet, I have to runSnippet?

        In order to call the title element in the first cell of the array, I have to do the following;
        echo $xmlData->SONGHISTORY->SONG[0]->TITLE;


        How would I format that call to specify the cell in SONG to call from? I’ll always be calling TITLE so I dont necessarily need to pass a variable to specify that, its just the location in the SONG array that I need to pass through a snippet call.

        Thanks for your help!
          • 3749
          • 24,544 Posts
          Quote from: fegul at Sep 16, 2007, 02:10 PM

          I have a PHP script that echos an array and I would like to specify the location to a specific part of the array in the snippet call
          (e.g. {{snippetname &array_name="sample_array" &array_location="2"}} which would print the data located in sample_array[2] which is created in the snippet.

          Any ideas as to how to how I could take the snipppet call and integrate it in my currently existing PHP script?

          If I’m understanding you correctly, the typical way to do it is to put all your PHP code in the snippet (i.e., your code IS the snippet).

          If you add array_name and array_location as a parameters in the snippet, they will be available in the snippet as PHP variables. See some of the existing snippets (e.g. ditto) to see how to set this up and access the parameter values in the snippet. Your snippet code would then create the output that would appear on the screen where the snippet call is placed in the document with the snippet call.

          BTW, snippets are called with [!SnippetName!] rather than {{xxx}}
          {{}} is for chunks, which usually contain straight HTML, not PHP code.

          [!SnippetName!] will call the snippet uncached, which I think you’ll want to do since the snippet will produce dynamic results.

          Also, be sure your parameters are enclosed in backticks, not single or double quotes, and put a question mark after the snippet name:

          [!snippetname? &array_name=`sample_array` &array_location=`2`!]

          The quick and dirty method to do what you want would be to put the code that presents the data inside your snippet.

          Later, you may want to make your snippet more of an official MODx snippet by creating placeholders in the snippet and using those placeholders in presentation code that’s contained in the document with the snippet call. That separates the presentation layer from the code behind it. It also makes your snippet much more flexible so that it can be used, unchanged, for different purposes.

          Bob
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 29181
            • 480 Posts
            Bob is correct, my apologies...I miss understood your predicament.

            Taff
              Adrian Lawley: www.adrianlawley.com
              • 9572
              • 56 Posts
              Wow, the more I edit this post to ask another question, I end up fixing it in some way! I’m going to try using place holders now
                • 9572
                • 56 Posts
                I got everything setup and working properly but sending the array location as a variable is still giving me a tough time. This is the line I have in my snippet for bringing in a parameter in the snippet call;
                $input = isset($input) ? $input : 0;


                My Snippet call looks like this;
                [!Shoutcast? &input=`2`!]


                The line in which I use the $input variable is here;
                return $xmlData->SONGHISTORY->SONG[$input]->TITLE;



                Still not working though...
                  • 3749
                  • 24,544 Posts
                  Two things to check.

                  1. Spelling - snippet names are case-sensitive

                  2. Try harcoding the value of input:

                  return $xmlData->SONGHISTORY->SONG[2]->TITLE;

                  to see if that line works to begin with.

                  Hope this helps,

                  Bob
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 9572
                    • 56 Posts
                    It works when the array location is hardcoded and it works when I manually set $input to a number

                    It just doesnt seem to work as a parameter in the snippet call for some reason
                      • 33372
                      • 1,611 Posts
                      I think we need to see your entire snippet code at this point, since it looks to me as if the problem is in there somewhere.
                        "Things are not what they appear to be; nor are they otherwise." - Buddha

                        "Well, gee, Buddha - that wasn't very helpful..." - ZAP

                        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options