We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29794
    • 3 Posts
    Please bear with me while I try to explain smiley

    I currently have a PHP page that I’ve added as a snippet and it works perfectly when called from the page, but I’d like to make it work from the one snippet on many areas of my site. I have a football (Soccer) site which is separated into seasons, I have stats of league table, home and away form and more etc. in each season, currently I have identical pages in each season with only the years changed so they pick up the .csv data used to generate the pages.
    i.e.:
    $handle = fopen("assets/includes/2006-2007/stats/leaguetable.csv", "r");
    $handle = fopen("assets/includes/2007-2008/stats/leaguetable.csv", "r");
    

    etc.

    Rather than create snippets for each season I’d like to get the snippet to pick up the parent folder name (i.e. 2006-2007) and add it to the link listed above so that no matter which ’folder’ the snippet was being called from it would select the correct data file. So as an example the link would be in the format of
    $handle = fopen("assets/includes/(InsertParentFolderName)/stats/leaguetable.csv", "r");
    


    I am no PHP programmer and have trawled the forums for hours, going cross eyed, without anything slapping me in the face saying ’do this dumbass’ so would like to call on help for ideas/answers/slaps. grin

    Also is it possible to do something like this in the Long Title field when creating a document so I can then create all the docs for one season and then duplicate as many seasons as required to populate the site.
    i.e. League Table (InsertParentFolderName)

    Many thanks for any help offered smiley
      • 21257 MODX Staff
      • 730 Posts
      Quote from: thetribe at Jan 12, 2008, 06:39 PM

      Also is it possible to do something like this in the Long Title field when creating a document so I can then create all the docs for one season and then duplicate as many seasons as required to populate the site.
      i.e. League Table (InsertParentFolderName)

      Once you’ve followed ganeshXL’s advice and have your snippet picking up the parent folder’s name, create another snippet who’s only job it is to return the parent’s name. Then you can make your Long Title: League Table {{ParentFolderName}}
      That assumes you name your snippet ParentFolderName.

      One of the things that may have you going cross-eyed in trying to figure this out is that there’s no *right* way to do what you want. I may have been inclined to create a template variable (TV) to hold the value of the season name, so that you didn’t have to tighly bind the folder name with the name of the CSV file. The value you set in the parent’s TV could be inherited in any child documents using @INHERIT.. but anyway.. enough of that.. the point is that MODx’s flexibility creates some additional getting-used-to, but you will eventually settle on some techniques that work for you and the way you think.
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 29794
        • 3 Posts
        Thanks for the replies, I’ve managed to get the Long Title version working by installing the GetField snippet and creating a TV with a value of
        [!GetField? &parent=`1`!]

        and using the format League Table [*parentName*] in the Long Title field.

        I just can’t get my head around getting it to work in a snippet. One such snippet is
        // outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.
        
        $filename = '/assets/includes/2006-2007/stats/leaguetable.csv';
        if (file_exists($filename)) {
            echo "<span class=\"lastupdate\">Information Correct As Of:- " . date ("F d Y H:i:s.", filemtime($filename)),"</span>";
        }
        


        Can someone shed a little light on how I’d use GetField (or anything else they can now think of) to replace 2006-2007 with the name of the parent folder.

        Thanks again for any advice (and your patience smiley )
          • 7231
          • 4,205 Posts
          To run a snippet in another snippet you need to use the runSnippet api function. Try this untested code bellow, it may work or could be a starting point grin

          $parent_name = $modx->runSnippet("GetField", array("parent" => "1", "field" => $title ));
          $filename = '/assets/includes/'.$parent_name.'/stats/leaguetable.csv';


          MOre on runSnippet: http://wiki.modxcms.com/index.php/API:runSnippet
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 29794
            • 3 Posts
            Quote from: dev_cw at Jan 14, 2008, 06:19 AM

            To run a snippet in another snippet you need to use the runSnippet api function. Try this untested code bellow, it may work or could be a starting point grin

            $parent_name = $modx->runSnippet("GetField", array("parent" => "1", "field" => $title ));
            $filename = '/assets/includes/'.$parent_name.'/stats/leaguetable.csv';


            MOre on runSnippet: http://wiki.modxcms.com/index.php/API:runSnippet

            You are a star! I had attempted to use the runSnippet command but didn’t get any joy with it, but that was because I wasn’t using it correctly, many thanks for the help and that example has helped me understand a little more about the runSnippet command. grin