We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36523
    • 3 Posts
    Hi,

    I am new to MODX and PHP and I am trying to get a phone number to change based on the page a user loads (local vs. national; defaulting to national when not specified).

    I've created a snippet and placed in this code:

    <?php
    $homepage = "/";
    $ohio = "/states/oh";
    $illinois = "/states/il";
    $michigan = "/states/mi";
    $currentpage = $_SERVER['REQUEST_URI'];
    
    	if ($ohio == $currentpage)  {
    	  echo "555-555-5555";
    	} elseif ($homepage == $currentpage) {
    	  echo "1 (800) 555-5555";
    	} elseif ($illinois == $currentpage) {
    	  echo "555-555-5555";
    	} elseif ($michigan == $currentpage) {
    	  echo "555-555-5555";  
    	};


    I've gone through a few iterations and I cannot seem to get it to work. All I get is the national number on every page.

    Any help would be appreciated.

    Home page url: www.example.com
    state page example: www.example.com/states/oh


    Thanks
      • 22303 MODX Staff
      • 10,725 Posts
      How are you calling the Snippet? It must be uncached to return different results. Also, REQUEST_URI is not portable; consider developing in a subdirectory. Instead, you might try this without including the initial slash:

      <?php
      $homepage = "";
      $ohio = "states/oh";
      $illinois = "states/il";
      $michigan = "states/mi";
      $currentPage = $_REQUEST[$modx->getOption('request_param_alias')];
      


      However, consider this is not cacheable and there might be more efficient ways to accomplish this. One thing that comes to mind is having a TemplateVar with the state initial which is simply fed to the Snippet call, e.g.

      [[GetPhone? &state=`[[*state]]`]]


      Then your code would be cacheable on each Resource and would not be dependent on the URI's.

      <?php
      $output = "";
      $state = empty($state) ? "" : $state;
      switch ($state) {
          case 'oh':
                  $output = "555-555-5555";
                  break;
          case 'il':
                  $output = "555-555-5556";
                  break;
          case 'mi':
                  $output = "555-555-5557";
                  break;
          default:
              if ($modx->resource->get('id') == $modx->getOption('site_start')) {
                  $output = "1 (800) 555-5555";
              }
              break;
      }
      return $output;
      
      [ed. note: opengeek last edited this post 15 years, 2 months ago.]
        • 33968
        • 863 Posts
        It looks like you could even do that without needing the tv, seeing as your alias seems to contain the state code already (oh, il, mi, etc):
        [[GetPhone? &state=`[[*alias]]`]]
        

        And... (sorry Jason!) there's a small typo in the switch above, should be:
        switch ($state) {
         ... instead of ...
        switch ($currentpage) {
        
          • 22303 MODX Staff
          • 10,725 Posts
          Fixed the typo, thanks...
            • 36523
            • 3 Posts
            Thanks for responding!, I will take a look and let you know how I do with it.
              • 36523
              • 3 Posts
              Thanks guys, got a chance to test it -- works beautifully.

              Using, [[GetPhone? &state=`[[*alias]]`]]