We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    I’m beginning to understand how to create custom snippets in modx, and am really excited each time I figure out part of the API. But as always when learning, you get stuck somewhere...

    I’m trying to create a snippet that executes the mySql query contained in the code below. It basically compares values across the whole document tree for several TVs (48,50,26), filters them, then returns the lowest ’price’ value from the results.

    The filter values (28, 24) are static until I can get it working correctly. The query itself is valid SQL:

    <?php
    function getPrice() {
    global $modx;
    $output = '';
    
    $result = $modx->db->query( '
    
               SELECT t3.value
    
               FROM         `hlp_site_tmplvar_contentvalues` t 
               INNER JOIN   (`hlp_site_tmplvar_contentvalues` t2,  
                                    `hlp_site_tmplvar_contentvalues` t3)
    
               ON           t.contentid = t2.contentid
               AND          t2.contentid = t3.contentid
               AND          t.tmplvarid = '48' 
               AND          t2.tmplvarid = '50'
               AND          t3.tmplvarid = '26'
    
               WHERE      t.value >= '28' AND t2.value >= '24'
    
               ORDER BY t3.value ASC 
               LIMIT 1
               ');
    
    if( $modx->db->getRecordCount( $result ) >= 1 ) {
          while( $row = $modx->db->getRowArray( $result ) ) {
                $output .= 'Value: ' . $row['t3.value'];
                }
    }
    
    else {
        $output = 'No records to show' ;
    }
    return $output;
    }
    ?>
    


    Unfortunately I can’t get anything output. I’ve tried even with a basic sql query and I can’t get anything to pop out, so presumably the issue is with my php.

    Is anyone able to help me out?

    Cheers, Luke
      • 16278
      • 928 Posts
      You’re using single quotes in the query, so you need to use double quotes to wrap the whole thing for the function’s argument, not singles.
      laugh KP
        • 33968
        • 863 Posts
        You mean like this?

        $result = $modx->db->query( " SELECT ... etc ........  " );
        
        


        That doesn’t work either unfortunately!

        I’ve been trying to strip down the query to it’s most basic form and then build it back up to see where it is going wrong. But not getting anywhere so far.
          • 20413
          • 2,877 Posts
          Try this
          if(!function_exists('getPrice')) {
             function getPrice() {
             ...
             }
          }
          
            @hawproductions | http://mrhaw.com/

            Infograph: MODX Advanced Install in 7 steps:
            http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

            Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
            http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
            • 33968
            • 863 Posts
            Thanks for that, but still not working.

            For now I’ve simplified the Sql query to:

            	           SELECT value
            	
            	           FROM   `hlp_site_tmplvar_contentvalues`
            	
            	           WHERE  tmplvarid = '26'
            	
            	           ORDER BY value ASC 
            	           LIMIT 10
            


            But I’m sure the problem is with the php...
              • 4172
              • 5,888 Posts
              you can try:

                    while( $row = $modx->db->getRow($result) ) {
                          $output .= 'Value: ' . $row['value'];
                          }


              or

              $rows=$modx->db->makeArray($result);
              
              if (count($rows)>0){
                  //echo'<pre>'.print_r($rows,1).'</pre>';
                  foreach ($rows as $row){
                      $output .= 'Value: ' . $row['value'];
                  }
              }
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 33968
                • 863 Posts
                Hi Bruno, thanks for that! Both worked... as did the initial code, but only after I removed anything to do with function getPrice().

                So now it starts simply at ’global $modx;’ and I get data from the DB displayed on the page as intended.

                However, would removing the function present any problems, for example if I wanted to use the snippet several times on one page?
                Or if I wanted to use the results or code in another snippet called on that page?

                I really appreciate everyone’s help here smiley
                  • 18913
                  • 654 Posts
                  I think the reason it’s tipping over is that you have single quotes within the query definition, when that query string is itself surrounded by single quotes. Try surrounding it with double quotes.
                  Right, someone already suggested that. One other thing : while the function is defined, shouldn’t it actually be invoked? I.e.

                  echo getPrice();

                  Matt
                    • 33968
                    • 863 Posts
                    Yep, the single quotes were definitely an error, and the function seems to be where I am going wrong.

                    That makes sense that it needs to be invoked somewhere but I just can’t get it to work. However the code seems to work fine without it.

                    I’m not a php developer but having some small successes with modx snippets is really encouraging me to read up... frustrating but enjoyable at the same time!
                      • 18913
                      • 654 Posts
                      At the end of your snippet - before the ?> - put something like
                      echo getPrice();
                      or
                      return getPrice();

                      Call the snippet in a document and the output should show up.

                      Matt