We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1343 ☆ A M B ☆
    • 2,213 Posts
    Hello,

    I am using this (http://wiki.modxcms.com/index.php/DB_query_and_Placeholders) on one of my sites and I was wondering if someone could explain how to filter the results?

    I need to be able to show one record out of the DB on a page, but that record needs to be passed via url I can’t have a custom version per page.

    Thanks,
    AMDbuilder
      Patrick | Server Wrangler
      About Me: Website | Tweets |  MODX Hosting
      • 4310
      • 2,310 Posts
      Below is a slightly extended version of my original code example in the Wiki.
      If you need further explanation , just shout wink
      <?php
      $val = $_GET['querystring']; // http://your-url?&querystring=some-value
      $output = '';//create variable for holding the output
      $sql = $modx->db->query( 'SELECT * FROM `tblprojects` WHERE `some_column` = \''.$val.'\' LIMIT 0, 1000');//define the DB query, in this case the table is named 'tblprojects', and 'some_column' is your field name
      $resultArray = $modx->db->makeArray( $sql );//put it into an array
      foreach($resultArray as $item)//go through each record in the array
      {//start looping through the data array
      $params['award']=$item['Grant_Award'];//set the placeholders, the $params sets the name of the placeholder, the $item is the field name from the DB table
      $params['ref']=$item['Project_Ref'];
      $params['area']=$item['LA_Area'];
      $output.=$modx->parseChunk('ProjectTpl', $params, '[+', '+]');//define the chunk name and process the content, here it's ProjectTpl which has the placeholders for the data
      }//finish looping through the data array
      return $output;//now return the output from the processed chunk
      ?>
        • 1343 ☆ A M B ☆
        • 2,213 Posts
        I don’t have FURLs enabled at this point in time so how would I format the address to test the system until that point in time?

        Thanks
        AMDbuilder
          Patrick | Server Wrangler
          About Me: Website | Tweets |  MODX Hosting
          • 1343 ☆ A M B ☆
          • 2,213 Posts
          Thank you for confirming the address, turns out I was forgetting the output template sad

          Thanks,
          AMDbuilder
            Patrick | Server Wrangler
            About Me: Website | Tweets |  MODX Hosting
            • 4041
            • 788 Posts
            As a side note, to make the code portable I do this:

            Use the following as a snippet at the top of your template: [!Url_Seperator!]

            <?php
            /*      Url_Seperator
            
            */
            // url seperator placeholder ///////////////////////////////
            if($modx->config['friendly_urls'] =="1"){
             $seperator ="?";
            }else{
             $seperator ="&";
            }
            
            $modx->setPlaceholder('url_sep',$seperator);  // [+url_sep+]
            ?>


            You can then use [+url_sep+] in your code and it works with/without Friendly URL’s.
            ex: <a href="[~21~][+url_sep+]val=param">Link</a>
            ex: <a href="[~25~][+url_sep+]val=param1&val2=param2">Link</a>
              xforum
              http://frsbuilders.net (under construction) forum for evolution