We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12379
    • 460 Posts
    I have a function inside a snippet but it’s not displaying - have read a few bits here and there without making progress. Code within function works fine when removed from within the function...
      Mostly harmless.
      • 18913
      • 654 Posts
      Can you post your snippet? This, for example, correctly prints the value "8" :
      <?php
      function multnum($n1, $n2) {
        $result = $n1 * $n2;
        return($result);
      }
      
      $output = multnum(2,4);
      
      return $output;
      ?>
      
        • 4041
        • 788 Posts
        You may have to add global $modx; inside the function? </guess> smiley
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 12379
          • 460 Posts
          Here’s part of the function:

          
          <?php
          
            function paginate()
            {
                       if ($total_pages > 1) {
                        $previous = $page_number - 1;
                        $next = $page_number + 1;
                        
                        // Previous link
                        if ($previous <= 1) {
                            $output = '<a href="' . $thisurl . '?page_number=1&s=' . $search . '"><</a> ';
                        } else {
                            $output = '<a href="' . $thisurl . '?page_number=' . $previous . '&s=' . $search . '"><</a> ';
                        }
                        echo $output;
          
             //etc, etc
                        }
            }
          
          ?>
          
          


          It’s called twice in the snippet, before and after the search results with:

          paginate();


          Like I said, when i replace the function call with the code itself, it works. I’ve also run the snippet thru Taco Edit’s php syntax checker without error.
            Mostly harmless.
            • 3749
            • 24,544 Posts
            Putting global $modx inside a function fails for me from time to time and I never have figured out what causes it. Try sending $modx as an argument to the function (and delete the global $modx).

            BTW, using echo is considered a bad practice in MODx (and could possibly be contributing to your problem). It also can make your output appear in the wrong places. It’s better to build the $output variable with = and .= so it contains all the output from the snippet and return it when you’re all done processing. That will also make your code infinitely easier to maintain.
              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
              • 12379
              • 460 Posts
              Try sending $modx as an argument to the function

              How do I do this?

              Yep, knew the echos were a no no. I’ll fix ’em up once I got the basics working - unless you think echos inside functions inside snippets could be the problemo?
                Mostly harmless.
                • 3749
                • 24,544 Posts
                Quote from: hotdiggity at Jun 10, 2010, 11:08 PM

                Try sending $modx as an argument to the function

                How do I do this?

                Yep, knew the echos were a no no. I’ll fix ’em up once I got the basics working - unless you think echos inside functions inside snippets could be the problemo?

                Try this first, I don’t think it’s your problem, but let’s rule it out.

                when you call the function:

                paginate($modx);


                For the function itself:

                function paginate ($modx) {
                }


                Be sure to remove the global $modx so the argument will be used.

                [update] Just took another look, you should send all the other variables from outside the function that it needs as arguments too (e.g. $total_pages, $page_number, etc.). That’s why you’re not getting any output, the function is creating new local instances of those variables and ignoring the ones you’ve created elsewhere. You need to either make them all global or (the preferred method) send them as arguments.
                  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
                  • 12379
                  • 460 Posts
                  You nailed it with the last one - thanks!
                    Mostly harmless.
                    • 12379
                    • 460 Posts
                    It’s better to build the $output variable with = and .= so it contains all the output from the snippet and return it when you’re all done processing.

                    Have just made the above changes, but while the rest of the snippet works, the function calls aren’t appearing now. It’s like the $output for the paginate function isn’t added to the global $output

                    <?php
                    function paginate ($total_pages,$page_number,$search,$page_range,$this_url,$output)
                      {
                       //etc
                      }
                    ?>



                    paginate($total_pages,$page_number,$search,$page_range,$this_url,$output);
                      Mostly harmless.
                      • 26903
                      • 1,336 Posts
                      You need the $output variable to be passed by reference so you can change it in your paginate function, like this :-
                      function paginate(&$output) {........
                      $output .= 'fred';
                      }
                      


                      Then call it as normal ’paginate($myoutput)’ and $myoutput will have ’fred’ appended.

                      I’d pass $modx by reference as well BTW.
                        Use MODx, or the cat gets it!