We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36573
    • 173 Posts
    I'm attempting to use PHP code that finds zipcodes in a radius of another zipcode and create a custom snippet for formit. I've uploaded a table to the modx db with the zipcodes and lat/lon info (plus city and state). I have a file called "clsZipCodeRange.php" that does all the calculating in an includes folder.

    This is my form:
    <div class="panel">
    		<div class="panel-header"><h1>US Zipcode distance search</h1></div>
    		<div class="panel-content">
    			[[!FormIt?
    			&hooks=`zipsearch, successMessage`
    			&successMessage=`this was a success`
    			]]
    			
    			<form action="[[~[[*id]]]]" method='POST'>
    				<label for='zipcode'>Enter the zipcode (US)</label> [[%zipsearch.zipcode]]
                                     <input type='text' name='zipcode' id='zipcode' >
    				<div class="helptext">eg. 20007,20306</div>
    				<label for='radius'>Radius</label>
    				<select name='radius' id='radius' > [[%zipsearch.radius]]
    					<option value='5'>5 miles</option>
    					<option value='10'>10 miles</option>
    					<option value='15'>15 miles</option>
    					<option value='20'>20 miles</option>
    					<option value='25'>25 miles</option>
    					<option value='30'>30 miles</option>
    					<option value='35'>35 miles</option>
    					<option value='40'>40 miles</option>
    				</select>
    				<div id="actions">
    					<input class="button" type='submit' value='Search' class='clear'>
    				</div>
    			</form>	
    		</div>
    		[[$zipresult]]
    		[[+fi.successMessage]]
    	</div>


    This is my snippet "zipsearch"
    		$modx->getOption('site_url').'includes/clsZipCodeRange.php';
    		
    		$modx->setPlaceholder('zipcode',$sSearchZipCode);
    		$modx->setPlaceholder('radius',$iRadius);
    		
    		$sQuery = $modx->db->query('SELECT zipcode FROM `zipcodes`');
    		$objZipCodeRange = new ZipCodesRange($sSearchZipCode, $iRadius);
    	
    		$myChunk = "zipresult";
    
    		if(isset($objZipCodeRange)){
    			$totalZipcodesFound = count($objZipCodeRange->zipCodes);
    			if($totalZipcodesFound > 0){
    				foreach ($objZipCodeRange->zipCodes as $ZipData){
    				$values = array($ZipData['ZIPCODE'], ',');
    				}
    				$output = $modx->parseChunk($zipresult, $values, '[+', '+]');
     
    				return $output;
    			}}


    PHP is not really my thing so I could be way off. I'm trying to push my knowledge. I'm unable to get it to work. It just kicks to a blank screen. [ed. note: crecorn last edited this post 11 years, 11 months ago.]
      Everything I know I learned on the internet. Saved me thousands in College tuition,
      • 3749
      • 24,544 Posts
      This code almost certainly won't work, but it may push you in the right direction. I can't give you more specific advice without knowing what's in the $ZipData variable and what you're trying to do with the DB (if anything).

      /* This needs to be a path, not a URL. 
         Might need MODX_CORE_PATH instead, 
         depending on where the file is */
      $path = MODX_BASE_PATH . 'includes/clsZipCodeRange.php';
      if (file_exists($path)) {
          include $path;
      } else {
          return 'File Not found at: ' . $path;
      }
      
      /* Get the submitted values */
      $sSearchZipCode = $hook->getValue('zipcode');
      $iRadius = $hook->getValue('radius');
      
      /* I'm assuming that these placeholders are in the Tpl chunk */
      $modx->setPlaceholder('zipcode', $sSearchZipCode);
      $modx->setPlaceholder('radius', $iRadius);
      
      
      $objZipCodeRange = new ZipCodesRange($sSearchZipCode, $iRadius);
      
      if (empty($objZipCodeRange)) {
          return 'objZipCodeRange is empty';
      }
      
      $myChunk = "zipresult";
      
      /* next line is incorrect, but the variable $sQuery is never used */
      // $sQuery = $modx->db->query('SELECT zipcode FROM `zipcodes`');
      
      $totalZipcodesFound = count($objZipCodeRange->zipCodes);
      if ($totalZipcodesFound > 0) {
          foreach ($objZipCodeRange->zipCodes as $ZipData) {
              
              /* $values needs to be an associative array of keys and values, e.g.,  */
              /*
              $values = array(
                  'placeholder1' => $value1,
                  'placeholder2' => $value2,
              );
              */
              
              /* Guessing here */
              $values = array();
              foreach ($ZipData as $key=>$value) {
                 $values[$key] = $value;
              }
              /*$values = array(
                  $ZipData['ZIPCODE'],
                  ','
              );*/
          }
          /* If this is revolution, the placeholders in the chunk should be in
             the form [[+placeholderName]] */
          $output .= $modx->getChunk($myChunk, $values);
      
          return $output;
      }


        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
        • 36573
        • 173 Posts
        Thanks Bob. This will defiantly help. I started taking a PHP course on lynda.com. I'm a designer, but I enjoy coding also. I have a few projects that need a good amount of coding would you be interested in being paid to help out?
          Everything I know I learned on the internet. Saved me thousands in College tuition,
          • 3749
          • 24,544 Posts
          I sometimes take on small projects, but I'm pretty swamped and I'll be on the road until at least Nov 1. You can contact me after that If you're still interested.

            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
            • 36573
            • 173 Posts
            I may do that, thanks for the help. I played with it for awhile and ended running across zipcodeapi.com that offers a jquery solution. I was able to get it to return the zips, but I'm having trouble making it work with my snippet. I started a new post if you'd like to view it https://forums.modx.com/thread/94644/ajax-with-pdouser-snippet#dis-post-512568
              Everything I know I learned on the internet. Saved me thousands in College tuition,