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

Answered Redirector help

    • 8168
    • 1,118 Posts
    OK, quick update...

    The code pasted below, is now working out what the url should be that the user should be redirected to as I can see it rendered in the output. However, 1 more thing... It is not including the #[[+id]] element that I need added to the end of the URL to redirect to - so instead of it outputing http://www.example.com/here/ I need it to be http://www.example.com/here/#67 if the page id of the resource was 67.

    Any ideas?? Thanks Bob!

    $thisId = $modx->resource->get('id');
    $ids = array_reverse($modx->getParentIds($thisId));
    $ids[] = $thisId;
       
    $i = 1;
    $level = $modx->resource->getTVValue('levelsUp');
     
     
    $output = '<p>In snippet id = ' . $thisId . '</p>';
     
    foreach ($ids as $id => $value) {
        $docIds[$i] = $value;
        if ($value == $thisId) {
            $target = $docIds[$i - $level];
            if (!empty($target)) {
                $url = $modx->makeUrl($target, "", "", "full");
                if (!empty($url)) {
                    $output .= '<p>URL: ' . $url;
                    // $modx->sendRedirect($url);
                } else {
                    $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                }
                     
            } else {
                    $output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
            }
        }
        $i++;
    }
      
    $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
    return $output;
     
    return $thisId;
    
    • discuss.answer
      • 8168
      • 1,118 Posts
      FIXED! Bob - you are a leg-end - worked out the final few bits wink Code that works pasted below for reference - not it adds a ? before the #[[+id]] element - but still seems to work in a browser?

      $thisId = $modx->resource->get('id');
      $ids = array_reverse($modx->getParentIds($thisId));
      $ids[] = $thisId;
         
      $i = 1;
      $level = $modx->resource->getTVValue('levelsUp');
       
       
      $output = '<p>In snippet id = ' . $thisId . '</p>';
       
      foreach ($ids as $id => $value) {
          $docIds[$i] = $value;
          if ($value == $thisId) {
              $target = $docIds[$i - $level];
              if (!empty($target)) {
                  $url = $modx->makeUrl($target, "", "#$thisId", "full");
                  if (!empty($url)) {
                      $output .= '<p>URL: ' . $url;
                      $modx->sendRedirect($url);
                  } else {
                      $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
                  }
                       
              } else {
                      $output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>';
              }
          }
          $i++;
      }
        
      $output .= '<br><pre>' . print_r($ids, true) . '</pre>';
      return $output;
       
      return $thisId;
      
        • 3749
        • 24,544 Posts

        Try it this way:

        $url = $modx->makeUrl($target, "", "", "full") . '#" . $thisId;


          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
          • 3749
          • 24,544 Posts
          BTW, if the TV is empty or contains a 0, you could end up reloading the current page indefinitely. This (as line 7) should prevent that:

          if (empty($level)) {
              return '';
          }
            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
            • 8168
            • 1,118 Posts
            Quote from: BobRay at Dec 16, 2014, 09:06 PM

            Try it this way:

            $url = $modx->makeUrl($target, "", "", "full") . '#" . $thisId;



            Works with
            $url = $modx->makeUrl($target, "", "", "full") . '#' . $thisId;
            - you had a " instead of a ' after the # wink
              • 8168
              • 1,118 Posts
              Quote from: BobRay at Dec 16, 2014, 09:09 PM
              BTW, if the TV is empty or contains a 0, you could end up reloading the current page indefinitely. This (as line 7) should prevent that:

              if (empty($level)) {
                  return '';
              }

              Thanks Bob - I have a default value set on the TV so will never be set to 0 wink
                • 8168
                • 1,118 Posts
                To summarise - Bob, this works a dream! Thanks for guiding me on the solution. We got there in the end! smiley
                  • 3749
                  • 24,544 Posts
                  Glad I could help. smiley
                    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