We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8592
    • 22 Posts
    I have a snippet which is receiving two parameters, according to the first parameter (type) it goes the a specific table in the database and regarding to th second parameter (id) it will go to the specific record, fetch the record and display the details as result. but the problem is when I call this snippet multiple times in a page, if the parameters are identical in different calls, there is no problem, but if I provide them different parameter values only the first snippet will execute.
    like this:

    [[Item?type=1&id=2]]
    [[Item?type=2&id=3]]


    result will be only the result of first one! undecided huh........... >:( >:( >:(
      I love computer programming...
      • 10449
      • 956 Posts
      It’s good practise to always unset() all your variables at the begin or at the end of your snippets. Also, try to run the snippets uncached: [!Item?etc!]
        • 8592
        • 22 Posts
        I tried both versions (cached and uncached)!

        Do you mean in the body of the snippet which is receiving parameters type, and id i should write lik this:

        <?php
        unset($type);
        unset($id);
        ...
        ...
        ...
        unset($type);
        unset($id);

        ?>
          I love computer programming...
          • 7231
          • 4,205 Posts
          Check out the Creating Snippets post in the wiki, there are some good guidelines and tips:

          http://wiki.modxcms.com/index.php/Creating_Snippets

          In your case they say:
          Wrap functions inside !function_exists conditional if the resource is needed to run more than once on a page

          If you call a snippet twice on a page, it will throw up a parsing error saying something like "Cannot redeclare functionName()". So, if you need it to be possible to run your snippet multiple times on a page, you need to wrap it inside a !function_exists conditional block.

          function rnCountRows() { ... } // bad
           
          if(!function_exists(rnCountRows)) { // good
              function rnCountRows() { ... } 
          
          }

          Also always unset() the vars, since I set the vars in the beginning I only unset at the end but to use it in both cases is not a bad idea.
            [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]
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: dev_cw at Oct 28, 2007, 09:06 AM

            Also always unset() the vars, since I set the vars in the beginning I only unset at the end but to use it in both cases is not a bad idea.
            You can’t unset the vars at the beginning -- that will wipe out the variables being set by the snippet parameters. It’s much better to write reusable functions or classes you can create objects from for each snippet instance. Otherwise, you are executing code in the global scope, and all global variables will be considered and used, including any set of previous executions of the same snippet.
              • 7231
              • 4,205 Posts
              You can’t unset the vars at the beginning -- that will wipe out the variables being set by the snippet parameters.
              Of course, good point. rolleyes
                [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]
                • 8592
                • 22 Posts
                I know I should not unset them at the beginning! wink

                I solved the problem but I don’t know what the reason was!!!

                I’ve already faced with all the problems you friends described but this one was really different!!! shocked

                My snippet used to contain only 3 lines of code, one include_once , one condition and second include_once which was including the main file which was containing the code of the snippet!!!

                And the problem was when I was calling the snippet twice in a page, the first one was working fine but in the second one parameters were available in the snippet itself but not in the included file!!! which was containing the actual logic.
                So I copy and paste the content of that file into the snippet area and the problem solved! but what is the reason??? huh huh huh huh huh huh huh huh
                  I love computer programming...
                  • 22303 MODX Staff
                  • 10,725 Posts
                  If you used include_once(), that is the reason -- it would never be included after the first execution, and unless it was a class definition or function that could be reused again later, it was executed only when included. The second time would simply say, hey that file is included already, don’t include it again.