We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47719
    • 11 Posts
    Hi everyone,

    I'm new to modx and was wondering how i can accomplish this:

    I have created a php multidimensional array which has a list of months and within those months a list events, it looks like this

    Array
    (
        [201406] => Array
            (
                [name] => June
                [entries] => Array
                    (
                        [0] => stdClass Object
                            (
                                [MIGX_id] => 1
                                [title] => Spring Term begins
                                [description] => Wrap up warm and please remember to bring £3 for your tickets. Let's go pet some Gnomes!
                                [Date] => 2014-06-04 16:10:00
                            )
    
                        [1] => stdClass Object
                            (
                                [MIGX_id] => 3
                                [title] => test tstrsr
                                [description] => srsfsddsds
                                [Date] => 2014-06-06 15:55:27
                            )
    
                    )
    
            )
    
        [201405] => Array
            (
                [name] => May
                [entries] => Array
                    (
                        [0] => stdClass Object
                            (
                                [MIGX_id] => 2
                                [title] => Something else
                                [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie velit quam, non volutpat massa luctus sit amet. Integer ornare malesuada magna, cursus ultricies erat.
                                [Date] => 2014-05-07 16:11:00
                            )
    
                    )
    
            )
    
    )
    
    


    I would like to create html similar to this:

    <div class="month">
    							<div class="left">
    								<div><span>J</span></div> <p>January</p>
    							</div>
    							<div class="right">
    								<div class="accordion">
    									<div class="event">
    										<div class="eventTitle">
    											<strong>6th</strong> Spring Term Begins
    										</div>
    										<div class="eventDescription">
    											<p>Wrap up warm and please remember to bring £3 for your tickets. Let's go pet some Gnomes!</p>
    										</div>
    									</div>
    									<div class="event">
    										<div class="eventTitle">
    											<strong>6th</strong> Spring Term Begins
    										</div>
    										<div class="eventDescription">
    											<p>Wrap up warm and please remember to bring £3 for your tickets. Let's go pet some Gnomes!</p>
    										</div>
    									</div>
    								</div>
    							</div>
    							<div class="clear"> </div>
    						</div>
    


    Which is repeated for the amount of months in the array. So i'm basically making a listing of a listing, looping through the months and within each month looping through events in that month...if that makes sense.

    This is easy enough to do in php/html but modx docs say not to put html in snippets (not sure of the reason). So how could i accomplish this with chunks?

    Thanks for any help.


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

      • 47719
      • 11 Posts
      Actually could i call a snippet from inside my chunk? That would prob work...mmm...

      Actually i don't think it will because i can't pass an array to the chunk and then to the snippet [ed. note: livingdeadbeat last edited this post 12 years, 4 months ago.]
        • 4172
        • 5,888 Posts
        you can collect all outputs by looping thrue your array and have a
        inner - loop for the items

        $eventsoutput .= $modx->getChunk($itemTpl,$eventArray);


        and in your outer month-loop call another chunk with the monthArray and the rendered events.

        $monthArray['events'] = $eventsoutput;
        $monthoutput .= $modx->getChunk($monthTpl,$monthArray);


        But where is this array-construct coming from?
        Seems to be a MIGX-TV involved.

        May be there is a simpler way to get done, what you are trying.



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

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 47719
          • 11 Posts
          Hi bruno.

          Thank you for the reply again!

          The array is coming from a migx list of events with dates, i'm then making that single array into a multi array with this php code

          $events = $modx->resource->get('Events');
          
          if($events[1]){
             $entries = json_decode($events[1]);
          
          $monthToEntries = array(); 
          foreach ( $entries as $item ){
           $date = \DateTime::createFromFormat('Y-m-d H:i:s', $item->Date);
           $monthKey = $date->format('Ym');
           
           if ( ! array_key_exists($monthKey, $monthToEntries) ) {
            $monthToEntries[$monthKey] = array(
             'name' => $date->format('F'),
             'entries' => array()
            );
           }
           
           $monthToEntries[$monthKey]['entries'][] = $item;
          }
          
          foreach($monthToEntries as $month){
             $properties = array('month' => $month['name']);
            $output = $modx->getChunk('Months',$properties);
             print $output;
          }
          
          
          }
          


          i'm basically trying to make this

          http://imgur.com/g607tH9
          • discuss.answer
            • 4172
            • 5,888 Posts
            something like that could work (untested):

            <?php
            
            $events = $modx->resource->getTVValue('Events');
            $entries = $modx->fromJson($events);
            
            
            $output = '';
            $monthToEntries = array();
            
            if (is_array($entries)) {
                foreach ($entries as $item) {
            
                    $date = $strtotime($item['Date']);
                    $monthKey =  strftime('%Y%m',$date);
            
                    if (!array_key_exists($monthKey, $monthToEntries)) {
                        $monthToEntries[$monthKey] = array('name' => strftime('%B',$date), 'entries' => array());
                    }
            
                    $monthToEntries[$monthKey]['entries'][] = $item;
                }
            
                foreach ($monthToEntries as $month) {
            
                    $entriesoutput = '';
                    if (is_array($month['entries'])) {
                        foreach ($month['entries'] as $entry) {
                            $entriesoutput .= $modx->getChunk('tplEntry', $entry);
                        }
                    }
            
                    $properties = array();
                    $properties['entries'] = $entriesoutput;
                    $properties['month'] = $month['name'];
                    $output .= $modx->getChunk('tplMonths', $properties);
                }
            }
            
            return $output;
            
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 47719
              • 11 Posts
              Awesome bruno thanks so much!