We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29076
    • 615 Posts
    Hi. My goal is to get the lowest value (in this case: start time) from a TV for each weekday.

    I have several documents (Yoga courses) with a TV called weekday containing Monday, Tuesday etc... and a TV called startTime containing 0900, 0930, 1000, 1030 etc... (not set as Time Stamp, just a number.)

    The startTime variates.

    I need, for each weekday, to show the lowest startTime. (That will show up as the Opening hour on that weekday.)

    MODx Revo.
    Parent resource ID = 12

    Example:

    Resource with TV weekday = Monday and startTime = 0900
    Resource with TV weekday = Monday and startTime = 1030
    Resource with TV weekday = Monday and startTime = 1100

    Resource with TV weekday = Tuesday and startTime = 1100
    Resource with TV weekday = Tuesday and startTime = 1000

    Resource with TV weekday = Wednesday and startTime = 1030
    Resource with TV weekday = Wednesday and startTime = 0930
    Resource with TV weekday = Wednesday and startTime = 0900
    Resource with TV weekday = Wednesday and startTime = 1100

    The result should be:

    Monday open at: 0900
    Tuesday open at: 1000
    Wednesday open at: 0900

    I guess this is a simple task, just not for me. Can someone please give me a clue on this one? smiley

      I think, thererfor I am! But what I am, and why...?
      • 3749
      • 24,544 Posts
      It's actually much more complex than you might think because of the way the data is stored. I can't think of anything but a kind of a brute-force method that walks through the records and stores the minimums. A custom PDO query could speed it up, but I'm too tired to work it out. this method assumes that you can provide the IDs of all the resources in the snippet tag. It also assumes that that there is at least one class on each weekday, and that no TV is set to its default value. I hope that works for you.

      This is off the top of my head and untested.

      Put this tag where you want to display the opening times:

      [[!OpeningTime? &docs=`8,12,32,145,212`]]
      



      Create a snippet called OpeningTime with this code:

      /* OpeningTime snippet */
      
      $weekdayTvId = 5;  // Change these two to the IDs of the two TVs
      $startTimeTvId = 6;
      
      $docs = $modx->getOption('docs', $scriptProperties);
      $docs = explode(',', $docs);
      
      /* Array of weekdays and start times */
      $days = array(
          'Monday' => '9999',
          'Tuesday' => '9999',
          'Wednesday' => '9999',
          'Thursday' => '9999',
          'Friday' => '9999',
      );
      
      
      foreach ($docs as $docId) {
      
          /* Get the values of the two TVs for this doc */
          $weekdayTv = $modx->getObject('modTemplateVarResource', array('contentid' => $docId, 'tmplvarid' => $weekdayTvId));
          $weekday = $weekdayTv->get('value');
          
          $startTimeTv = $modx->getObject('modTemplateVarResource', array('contentid' => $docId, 'tmplvarid' => $startTimeTvId));
          $startTime = $startTimeTv->get('value');
      
          /* Update start time if less than current value */
          if ($startTime < $days[$weekday]) {
              $days[$weekday] = $startTime;
          }
      }
      
      /* Build output */
      $output = "\n<ul>";
      
      foreach ($days as $day => $time) {
          $output .= "\n    <li>" . $day . ' open at: ' . $time . '</li>';
      }
      
      $output .= "\n</ul>";
      
      return $output;



      A little extra code could format the time, though it would be little tricky since they're not timestamps.


        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