Jump to content

Help outputting Multidimensional array


ginga8

Recommended Posts

I have the following multidimensional array, I am pretty new to this.

 

$sorted = array();

foreach($events as $event)

{

$year = date('Y', strtotime($event->start_date));

if(empty($sorted[$year]['total_hours']))

{

$sorted[$year]['total_hours'] = 0;

}

$sorted[$year]['event'][] = $event->title;

$sorted[$year]['event_start_date'][] = $event->start_date;

$sorted[$year]['event_hours'][] = $event->workshop_hours_4;

$sorted[$year]['total_hours'] += $event->workshop_hours_4;

}

 

 

With the year being dynamic, how would I output it to look something like the following

 

2007

EventID1 - 4 hours

EventID2 - 5 hours

TOTAL HOURS - 9 hours

 

2008

EventID3 - 5 hours

EventID4 - 2 hours

EventID5 - 3 hours

TOTAL HOURS - 10 hours

 

and so on.

 

Any suggestions would be great Thanks

Link to comment
https://forums.phpfreaks.com/topic/142103-help-outputting-multidimensional-array/
Share on other sites

i would do it a little differently. the title, hours, etc are already in a nice object. so just make an array of those objects. also, we can calculate the total later when we print it:

 

<?php
  $sorted = array();
  foreach($events as $event)
  {
    $year = date('Y', strtotime($event->start_date));
    if(!isset($sorted[$year]))
      $sorted[$year] = array();
    $sorted[$year][] = $event;
  }
  
  foreach($sorted as $year => $s_events){
    $total = 0;
    print "<b>$year</b><br />\n";
    foreach($s_events as $s_event){
      print "{$s_event->title} - {$s_event->workshop_hours_4} hours<br />\n";
      $total += $s_event->workshop_hours_4; 
    }
    print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n";
  }
?>

Here is my function getting the info

function civicrm_workshop_hours_SB_get_event() {

$events = array();

    $sql = db_query('SELECT

    civicrm_contact.first_name,

    civicrm_contact.last_name,

    civicrm_event.title,

    civicrm_event.start_date,

    civicrm_event.end_date,

    civicrm_uf_match.uf_name,

    civicrm_uf_match.uf_id,

    civicrm_participant.status_id,

    civicrm_value_total_hours_2.workshop_hours_4

    FROM

    {swiftrad_civicrm.civicrm_contact}

    Inner Join {swiftrad_civicrm.civicrm_participant} ON civicrm_contact.id = civicrm_participant.contact_id

    Inner Join {swiftrad_civicrm.civicrm_event} ON civicrm_participant.event_id = civicrm_event.id

    Inner Join {swiftrad_civicrm.civicrm_uf_match} ON civicrm_uf_match.contact_id = civicrm_contact.id

    Inner Join {swiftrad_civicrm.civicrm_value_total_hours_2} ON civicrm_value_total_hours_2.entity_id = civicrm_event.id

    WHERE

    civicrm_participant.status_id =  2

    ');

    // put into array

    while ($event = db_fetch_object($sql)) {

 

        $events[] = $event;

    }

    return $events;

}

 

as premiso said, if you sort the events DESC by date, you can do it without an extra sort...it's a little more complicated though:

<?php
  $last_year = null;
  $total = 0;
  if(count($events)){
    foreach($events as $event)
    {
      $year = date('Y', strtotime($event->start_date));
      if($year != $last_year){
        if(!empty($last_year))
          print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n"; 
        print "<b>$year</b><br />\n";
        $last_year = $year;
        $total = 0;
      }
      print "{$event->title} - {$event->workshop_hours_4} hours<br />\n";
      $total += $event->workshop_hours_4; 
    }
    print "<b>TOTAL HOURS - {$total} hours</b><br /><br />\n";
  } 
?>

...the above should work, but is untested

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.