We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    I think this is a really basic PHP question, but my brain is fried...

    I’m trying to return the ID number of a child page, based on its pagetitle. Using the getDocumentChildren API function, I’ve been able to return all child page titles, like this:

    <?php
    $currentID = $modx->documentIdentifier;
    $children = $modx->getDocumentChildren($currentID);
    
    if (!empty($children)) {
      foreach($children as $child) {
        $output .= $child['pagetitle'];
        $output .= "<br />";
    
      }
    return $output;
    }
    ?>


    That snippet will return this array of data (the pagetitles):

    Overview
    Admission
    Departments
    Courses
    Advising
    Careers
    Financing
    


    This looks good and proves I’m able to get the right data. However, what I really want to do is return the ID number of only the child page with the pagetitle "Departments". How do I go about this? I can print the entire array if that will help with troubleshooting, but I don’t want to muddy up this post unless it’s necessary.

    Thanks!
      • 32982
      • 674 Posts
      see this

      <?php
      $currentID = $modx->documentIdentifier;
      $children = $modx->getDocumentChildren($currentID);
      
      if (!empty($children)) {
        foreach($children as $child) {
          $output .= $child['pagetitle'];
          $output .= "<br />";
      if ($child['pagetitle']=="Departments"){
      $output .= $child['id'];
      }
        }
      return $output;
      }
      ?>


      hi
        Jabiertxof (formerly XYZVISUAL)
        My bussines: http://marker.es
        https://www.youtube.com/user/jabiertxof/videos
        • 813 ☆ A M B ☆
        • 326 Posts
        That worked! I knew it was fairly basic, but I couldn’t seem to get the syntax right. Thanks!
          • 813 ☆ A M B ☆
          • 326 Posts
          If you don’t mind, let me throw another challenge at you. Is it possible to use this to grab children, or technically grandchildren, from a deeper, pre-specified level (say, three levels deeper)?
            • 32982
            • 674 Posts
            see this

            <?php
            $currentID = $modx->documentIdentifier;
            $children = $modx->getDocumentChildren($currentID);

            if (!empty($children)) {
            foreach($children as $child) {
            $grandChildren = $modx->getDocumentChildren($child[’id’]);
            foreach($grandChildren as $grandChild) {
            $output .= $grandChild[’pagetitle’];
            $output .= "
            ";
            if ($grandChild[’pagetitle’]=="Departments"){
            $output .= $grandChild[’id’];
            }
            }
            }
            return $output;
            }
            ?>
              Jabiertxof (formerly XYZVISUAL)
              My bussines: http://marker.es
              https://www.youtube.com/user/jabiertxof/videos
              • 813 ☆ A M B ☆
              • 326 Posts
              Thank you, and this does seem to take me one level deeper, but what if I need to always go a certain number of levels deeper? I can’t seem to get it to work right using this:

              <?php
              $currentID = $modx->documentIdentifier;
              $children = $modx->getDocumentChildren($currentID);
              
              if (!empty($children)) {
                foreach($children as $child) {
              $grandChildren = $modx->getDocumentChildren($child['id']);
                foreach($grandChildren as $grandChild) {
                  $output .= $grandChild['pagetitle'];
              $greatgrandChildren = $modx->getDocumentChildren($grandChild['id']);
              
              if ($greatgrandChildren['pagetitle']=="Programs & Majors"){
              $output .= $greatgrandChildren['id'];
              }
              }
                }
              return $output;
              }
              ?>
              


              I think this is close, but even if this did work, is this really the simplest logic -- to manually go through each level’s child array and continue deeper?
                • 32982
                • 674 Posts
                Yes this is a way(only poor way), there is anothe way like direct sql querys.
                ¿Any one more?
                  Jabiertxof (formerly XYZVISUAL)
                  My bussines: http://marker.es
                  https://www.youtube.com/user/jabiertxof/videos
                  • 813 ☆ A M B ☆
                  • 326 Posts
                  I think I’m making progress! This is what I’ve come up with (and yes, it’s hideous, but it almost is working):

                  <?php
                  $currentID = $modx->documentIdentifier;
                  $children = $modx->getDocumentChildren($currentID);
                  
                  if (!empty($children)) {
                       foreach($children as $child) {
                            if ($child['pagetitle']=="Departments") {
                                 $output_one = $child['id'];
                                 $grandchildren = $modx->getDocumentChildren($output_one);
                                 if (!empty($grandchildren)) {
                                      foreach($grandchildren as $grandchild) {
                                           if ($grandchild['pagetitle']=="Aerospace Engineering") {
                                                $output_two = $grandchild['id'];
                                                $greatgrandchildren = $modx->getDocumentChildren($output_two);
                                                if (!empty($greatgrandchildren)) {
                                                     foreach($greatgrandchildren as $greatgrandchild) {
                                                          if ($greatgrandchild['pagetitle']=="Programs & Majors") {
                                                               $output_three = $greatgrandchild['id'];
                                                          }
                                                     }
                                                }
                                           }
                                      }
                                 }
                            }
                       }
                  return $output_three;
                  }
                  ?>
                  


                  When used in the &parents parameter of the Ditto call, it works exactly like I want by giving me the doc ID of a specific document (with pagetitle "Programs &amp; Majors") three child levels deep. However, there’s still one line that’s hardcoded:

                  if ($grandchild['pagetitle']=="Aerospace Engineering") {
                  


                  In this case, it shouldn’t be hardcoded to the name of the page -- it should just continue traversing its current path. I’ve tried removing it completely and also using in_array, but nothing seems to work. Suggestions?
                    • 32982
                    • 674 Posts
                    test it
                    return $output_two . $output_three;
                    hi
                      Jabiertxof (formerly XYZVISUAL)
                      My bussines: http://marker.es
                      https://www.youtube.com/user/jabiertxof/videos
                      • 813 ☆ A M B ☆
                      • 326 Posts
                      Doesn’t that just concatenate the two values?

                      I may not have explained myself properly. This line:

                      if ($grandchild['pagetitle']=="Aerospace Engineering") {
                      


                      means none of the code below it will run unless the pagetitle is Aerospace Engineering, and this is not correct. For the level above it (pagetitle="Departments") and the level below it (pagetitle="Programs &amp; Majors"), this is fine. But on the "Aerospace Engineering" level, I just want it to continue traversing the levels downward without manually entering a pagetitle, as these could change.