We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37126
    • 37 Posts
    I've cobbled together a snippet that returns an array of disabled date ranges from another snippet to feed into a datepicker. It seems to work however it's only returning one row. Being a bit of a novice at PHP could anyone tell me where I'm going wrong and how to return all rows?

    Thanks

    Snippet 1 Provides the array of from and to dates booked
    <?php
    $template = $modx->getOption('template','');
    $assetsPath = $modx->getOption('slots.assets_path', null, $modx->getOption('assets_path').'components/slots/');
    $Slots =& $modx->slots;
    
    $booking = $modx->getService('slots','Slots',$modx->getOption('slots.core_path',null,$modx->getOption('core_path').'components/slots/').'model/slots/',$scriptProperties);
    if (!($booking instanceof Slots)) return '';
    
    /* setup default properties */
    $c = $modx->newQuery('Slot');
    $slots = $modx->getCollection('Slot',$c);
    
    $query = $modx->newQuery('Slot');
    
    $forms = $modx->getCollection('Slot',$query);
    $output = '';
    foreach ($forms as $form) {
        $slotArray = $form->toArray();
    foreach($slotArray as $key => $value) {
        $f = $form->get('datefrom', $value);
        $l =  $form->get('dateto', $value);
    
    
    $days =  $modx->setPlaceholders(array(
    'start'=> $f,
    'end'=> $l
    ));
    }
    }
    return $output;


    Snippet 2 Reads the from and to dates and puts them into a date range adding dates in between
    <?php
    function dateRange($first,$last,$step='+1 day',$format='j-n-Y'){
    $dates=array();
    $current=strtotime($first);
    $last=strtotime($last);
     
    while($current<=$last){
    $dates[]=date($format,$current);
    $current=strtotime($step,$current);
    }
    return $dates;
    }
    $output = '';
    
    $disablerange = $modx->runsnippet('getDateRange');
    $a = $modx->getPlaceholder('start');
    $b = $modx->getPlaceholder('end');
    
    $array = (dateRange($a,$b));
    
    $output .= '[';
    $dis = implode("','", $array);
    $output .=  "'".$dis."'";
    $output .= ']';
    
    return $output;
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      In your second foreach, you are setting the placeholder for each item, so as you loop through the array the placeholders are getting re-set. When the looping is finished, the placeholders will be storing the last values you set them to.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 37126
        • 37 Posts
        Worked it out eventually with just one snippet as follows:

        function dateRange($first,$last,$step='+1 day',$format='j-n-Y'){
        $dates=array();
        $current=strtotime($first);
        $last=strtotime($last);
          
        while($current<=$last){
        $dates[]=date($format,$current);
        $current=strtotime($step,$current);
        }
        return $dates;
        }
        $assetsPath = $modx->getOption('slots.assets_path', null, $modx->getOption('assets_path').'components/slots/');
        $Slots =& $modx->slots;
         
        $booking = $modx->getService('slots','Slots',$modx->getOption('slots.core_path',null,$modx->getOption('core_path').'components/slots/').'model/slots/',$scriptProperties);
        if (!($booking instanceof Slots)) return '';
         
        $c = $modx->newQuery('Slot');
        $slots = $modx->getCollection('Slot',$c);
        
        $output = '';
        $output .= "[";
        foreach ($slots as $slot) {
        $slotArray = $slot->toArray();
        $first = $slot->get('datefrom');
        $last = $slot->get('dateto');
        $array = (dateRange($first,$last));
        $dis = implode("','", $array);
        $output .=  "'".$dis."',";
        }
        $output .=  "]";
        
        return $output;


        Only issue is a trailing comma after the last returned array.