views:

22

answers:

1

I have an array of objects, containing lists of times. I have to stick this in a Matrix-alike array, in order to put it in a table. This is a part of my earlier SO question on how to build calendar tables

$node->sessions = array(
  1286452800, // '2010-10-07 14:00:00'
  1286460000, // '2010-10-07 16:00:00'
);
$node->title = 'Foo';
$nodes[1] = $node;

$node->sessions = array(
  1286452800, // '2010-10-07 14:00:00'
  1286460000, // '2010-10-07 16:00:00'
  1286461800, // '2010-10-07 16:30:00'
);
$node->title = 'Bar';
$nodes[2] = $node;

$node->sessions = array(
  1286460000, // '2010-10-07 16:00:00'
  1286461800, // '2010-10-07 16:30:00'
  1286465400, // '2010-10-07 17:30:00'
);
$node->title = 'Baz';
$nodes[3] = $node;

These represent a table of playdates of movies. As can be seen in action here. The code for this, right now is far too complex (IMHO) and could be simplified, just that I don't know how. Maybe trough some Matrix-library? Or some algorythm that I am unaware of?

The resulting structure, should be an array representing the header (column names of the table) and an array-with-arrays representing the rows. As follows:

$header = array(
  ' ', // empty "cell"
  ttt, // ttt being the timestamp for '14:00'
  uuu, // idem for '15:00'
  vvv, // idem for '16:00'
  www, // idem '17:00'
);
//  title, 14:00, 15:00, 16:00, 17:00
$rows = array(
  array(
    'Bar', 1286452800,  '', array(1286460000, 1286461800), '',  //Both 16:00 and 16:30 grouped under 16:00 'header'
  ),
  array(
    'Baz', '',  '', array(1286460000, 1286461800), 1286465400
  ),
  array(
    'Foo', 1286452800,  '', 1286460000,  '',
  )  
);

Each row should have the same amount of "cells". Timestamps should be grouped under the hour they belong to (16:15, 16:30 etc all grouped under 16:00) Also note that the above-given output structure might be optimised too, please let me know if there are ways to simplify the code, by simplifying or changing the resulting "matrix".

Pseudocode for what I do now:

  1. usort $nodes by title
  2. loop over all the $nodes to find the width of the matrix (14:00 to 17:00) and fill the $header
  3. loop over all the $nodes again, for each node loop over $header and fill an array with either empties ('') or with the timestamps.

Now, for a the three entries above, that is not a big problem, but this matrix can grow very wide (large $headers array) and deep (many $nodes) resulting in a lot of duplicate looping. But performance and duplicate looping is not my greatest concern, mostly having cleaner and less nested, less complex code, is. :)

A: 

I'm scrapping your and coming up with how i would do it in hopes it may help you.

First get the data in this format:

$movies = array (
    array (
        'title' => 'Babies',
        'times' => array(
                    1286460000,1286467200,1286476200,1286487000
        )
    ),
    array (
        'title' => 'El secreto de sus ojos',
        'times' => array(
                    1286474400,1286481600,1286483400
        )
    )
    // more movies...
);

Here's a link to working code: http://ideone.com/P4zZn

Here's the code

<?php
date_default_timezone_set('GMT');
$start_hour = 12; // Start hour of matrix
$end_hour = 22; // End hour of matrix
$skip_hours = array( 18, 20 );

// Get the timestamp for each hour in the matrix
foreach( range( $start_hour, $end_hour ) as $hr ) {
    if ( in_array( $hr, $skip_hours ) ) {
        continue;
    }
    $time_sections[] = mktime( $hr,0,0, 10, 7, 2010 ); // You'll want to put null or the month/day to use the current month/day
}

$movies = array (
    array (
        'title' => 'Babies',
        'times' => array(
                    1286460000,1286467200,1286476200,1286487000
        )
    ),
    array (
        'title' => 'El secreto de sus ojos',
        'times' => array(
                    1286474400,1286481600,1286483400
        )
    )
);


?>

<table border="1">
    <thead>
        <th>Movie</th>
        <?php foreach( $time_sections as $time_section ): ?>
        <th><?php echo date( 'G:i', $time_section ) ?> </th>
        <?php endforeach; ?>
    </thead>
    <tbody>
    <?php foreach( $movies as $movie ): ?>
        <tr>
            <td><?php echo $movie['title'] ?></td>
            <?php foreach( $time_sections as $time_section_index => $time_section ): ?>
            <td>
                <!-- while there are movie times left and the first movie time in the array is less than the next timestamp print the movie time -->
                <?php while( count( $movie['times'] ) && current( $movie['times'] ) < $time_sections[$time_section_index + 1] ): ?>
                    <!-- echo the time and pull it from the array -->
                    <?php echo date( 'G:i', array_shift( $movie['times'] ) ) ?> 
                <?php endwhile; ?>
            </td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
Galen
using range() was the magic tip I needed. Thanks!
berkes