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

    I have created a snippet to list events by month. My events do not come out in order of start date. I have pasted my snippet below and wonder if anyone would be kind enough to point me in the right direction. The TV I want to sort by is called 'Start-Date'.

    <?php
    $months = $modx->resource->getMany('Children');
    
    if (!empty($months)) {
        sort($months);
        foreach ($months as $month) {
            $output .= "\n<li>";
            $output .= "\n<div class='title'>\n<h5>" . $month->get('pagetitle') . "</h5>\n</div>";
            $output .= "<div class='content'>";
            
            $fixtures = $month->getMany('Children');
            
            if (empty($fixtures)) {
    
                $output .= "<p>No Fixtures</p>";
    
            } else {
    
                $output .= "<table>";
                $output .= "<thead>";
                $output .= "<tr>";
                $output .= "<th>Start</th>";
                $output .= "<th>End</th>";
                $output .= "<th>Fixture</th>";
                $output .= "<th>Venue</th>";
                $output .= "</tr>";
                $output .= "</thead>";
    
                foreach ($fixtures as $fixture) {
    
                    $templateVars =& $fixture->getMany('TemplateVars');
    
                    foreach ($templateVars as $tvId => $templateVar) {
                        $tvs[$templateVar->get('name')] = $templateVar->get('value');
                    }
    
                    $resourceArray = array_merge($fixture->toArray(),$tvs);
                    $output .= $modx->getChunk('FixtureTpl', $resourceArray); 
    
                }
    
                $output .= "</table>";
            }
    
            $output .= "\n</div>";
            $output .= "\n</li>";
        }
    }
    
    return $output;


    Thanks in advance.

    John

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      you can use getMany the same way like getCollection (it does call getCollection internally)

      so, I think you can try:

      $c = $modx->newQuery('modResource');
      $c->sortby('menuindex');
      $months = $modx->resource->getMany('Children',$c);


        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 8274 ☆ A M B ☆
        • 222 Posts
        The trickiest part of your question is that you want to sort by a TV, which is a different table in the database.

        You might want to utilize the $modx->getCollectionGraph() function, unless you are really good with SQL JOINS.

        http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects/getcollectiongraph#getCollectionGraph-AnotherExample

        This will let you access the related TVs to the resource via $resource->TemplateVarResources() notation.
          • 14107
          • 21 Posts
          Thank you both very much for you help.

          I've used Bruno's suggestion because that lets me get the correct sort order using 'menuindex', which is good enough for my needs. I've used the code in two places because I needed to sort the children of the children too. The updated code is below in case anyone can benefit form it.

          John

          $c = $modx->newQuery('modResource');
          $c->sortby('menuindex');
          $months = $modx->resource->getMany('Children',$c);
          
          if (!empty($months)) {
          
              foreach ($months as $month) {
                  $output .= "\n<li>";
                  $output .= "\n<div class='title'>\n<h5>" . $month->get('pagetitle') . "</h5>\n</div>";
                  $output .= "<div class='content'>";
                  
          $c = $modx->newQuery('modResource');
          $c->sortby('menuindex');
                  $fixtures = $month->getMany('Children',$c);
                  
                  if (empty($fixtures)) {
          
                      $output .= "<p>No Fixtures</p>";
          
                  } else {
          
                      $output .= "<table>";
                      $output .= "<thead>";
                      $output .= "<tr>";
                      $output .= "<th>Start</th>";
                      $output .= "<th>End</th>";
                      $output .= "<th>Fixture</th>";
                      $output .= "<th>Venue</th>";
                      $output .= "</tr>";
                      $output .= "</thead>";
          
                      foreach ($fixtures as $fixture) {
          
                          $templateVars =& $fixture->getMany('TemplateVars');
          
                          foreach ($templateVars as $tvId => $templateVar) {
                              $tvs[$templateVar->get('name')] = $templateVar->get('value');
                          }
          
                          $resourceArray = array_merge($fixture->toArray(),$tvs);
                          $output .= $modx->getChunk('FixtureTpl', $resourceArray); 
          
                      }
          
                      $output .= "</table>";
                  }
          
                  $output .= "\n</div>";
                  $output .= "\n</li>";
              }
          }
          
          return $output;