We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29201
    • 239 Posts
    I’m sure this has been covered and I’ve seen a related thread on "Passing a TV into a Snippet"....but I’m unable to figure out how to handle passing a TV to a Snippet and outputting the proper data I need.....this is probably due to my lack of solid programming skills.

    I’ve got a table in my MODx database that holds real estate (MLS) data. I’ve pulled the MLS numbers from this table into a TV which is a Listbox (multiple-select). Once the user selects a MLS number(s) from this TV it should then query my table and return the rows where the MLS numbers are equal.

    I would then need to output all the values onto the page to build my list of properties with price, #bedroom, #bath, etc.)

    Can someone help my thick skull figure out how to do this? Could I be going about this wrong?


    Thanks!
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      The simple way to pass values to your snippet is via the input parameters (sorry to be pedantic, but hey, it’s a forum post so even if it’s stuff you already know, maybe someone else is watching):

      Call your snippet and pass it a value something like this:
      [[!mySnippet? &my_argument=`[[*my_tv]]` ]]


      But you could also calculate what the TV value is just by knowing page ID by using the xPDO API. I know Bob has some examples of this on his site if you need them (sorry, I’m being lazy here).

      I was having some trouble using TV filtering in the getResources snippet, but you should be able to do searches by TV variables using establish Snippets.

      Thirdly, if you are rolling your own Snippet, I’ve found the getCollectionGraph method to be extremely useful for this type of thing... it lets you join on other tables and search them using the normal xPDO where condition. I have a problem on a site where I need to join on TV values programatically and I’ll post my methods in the official docs once I figure them out...

      Wow... I must be tired... my response went from MODx 101 to some advanced dev stuff... not sure what level of response you’re after. Can you clarify more of what direction you’re proceeding in so the forum dudes can offer more info?
        • 29201
        • 239 Posts
        To clarify my listbox is pulling in data that is in a non-MODx table. I need to take each of these unique values and put them in a query, return a row set, and output the results.

        I’m not an advanced developer by any stretch so a very basic example would be the best thing I could hope for. Thanks for taking the time to read this and post.
          • 33968
          • 863 Posts
          Have a look at this for starters: http://wiki.modxcms.com/index.php/DB_query_and_Placeholders

          You can pass the MLS number to the snippet exactly as shown by Everett, then pass that to the db query within the snippet code.

          But first, are you using Evo or Revo? And are you able to build the db query yourself? (eg in phpMyAdmin) If so, post it here and I’ll help you build the snippet.

            • 29201
            • 239 Posts
            Vadim wrote this for me which works like a charm

            <?php
            $features = $modx->getTemplateVar('GetProperties','',1);
            $features = $features['value'];
            
            $features_arr = explode("||",$features);
            
            $ids = join(',',$features_arr);  
            
            $query = "SELECT * FROM mls_listings where C_MLSNumber IN($ids) ORDER BY C_Price DESC";
            $result = mysql_query($query);
            
            
            
            while ($row = mysql_fetch_assoc($result)){
                  
                  $price = (int)$row['C_Price'];
                  $price_str =  '';
                  $i=0;
                  while ((int)$price){
                     if ((int) $price && $i % 3 == 0 && $i) $price_str = ','.  $price_str;
                     $i++;
                     $price_str = ($price % 10) .  $price_str;
                     $price /=10;
                   }     
                   
                   $query2 = "SELECT * FROM mls_listings_images where C_Matrix_Unique_ID = '{$row["C_Matrix_Unique_ID"]}' ORDER BY C_INDEX LIMIT 1";
                   $result2 = mysql_query($query2);
                   $row2 = mysql_fetch_assoc($result2);
            
                   $src = $row2['C_PATH'];
                   $src = str_replace ('/home8/fairvif3/public_html/oakvillerealestate4sale/' , '' , $src);
                 
            
                  $params['C_HHouseNumberNumeric']=$row['C_HHouseNumberNumeric'];
                  $params['C_StreetName']=$row['C_StreetName'];
                  $params['C_StreetType']=$row['C_StreetType'];
                  $params['C_Price']= $price_str;
                  $params['C_NumbBedsAboveGround']=$row['C_NumbBedsAboveGround'];
                  $params['C_NumbBathrooms']=$row['C_NumbFullBathrooms']+$row['C_NumbPartBathrooms'];
                  $params['C_Postal']=$row['C_Postal'];
                  $params['C_CityTown']=$row['C_CityTown'];
                  $params['ListingID']=$row['ListingID'];
                  $params['C_Remarks']=$row['C_Remarks'];
                  $params['image_src']=$src;
            //      $params['']=$row[''];
            //     $params['']=$row[''];
            //      $params['']=$row[''];
                 $output.=$modx->parseChunk('FeaturedPropertiesTpl', $params, '[+', '+]');
            }
            echo $output;
            ?>