Documentation Menu

1.5 Template Project

The Joomla! Documentation Working Group is running a project to develop detailed reference and tutorial material on Joomla! 1.5 templates.  There is a project page on the documentation wiki where you can see the work in progress and help us by contributing your knowledge.

Who's Online

We have 3 guests online

Help Site License

The Joomla! Help Site content is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution NonCommercial ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Home arrow patTemplate arrow Freqently Asked Questions arrow How can I create tables with several rows and columns?

How can I create tables with several rows and columns? PDF Print E-mail

This is a problem that often occurs, when you are creating lists with more than one columns? We refer to this problem as 'nested repetitions', as it's not only needed when creating to dimensional tables, but also nested lists.
At first glance, it may seem quite hard, but once you've understood it, it's quite easy... At first, let's take a look at the template structure:

001    <table border="1" cellpadding="10" cellspacing="0">
002          <!-- template for table row -->
003          <patTemplate:tmpl name="row">
004                <tr>
005                      <!-- template for table cell -->
006                      <patTemplate:tmpl name="cell">
007                            <td>{real_name} is {superhero}</td>
008                      </patTemplate:tmpl>
009                </tr>
010          </patTemplate:tmpl>
011    </table>

There are two templates, on called "row," which has to be repeated, and one called "cell,'" which is repeated for all cells in each row.  In these cells the content has to be displayed.

To repeat the cells and the rows, use the following PHP code:

01        <?PHP
02        // data to display
03        $data = array(
04                       array( "real_name" =>"Clark Kent", "superhero" =>"Superman" ),
05                       array( "real_name" =>"Bruce Wayne", "superhero" =>"Batman" ),
06                       array( "real_name" =>"Kyle Rayner", "superhero" =>"Green Lantern" ),
07                       array( "real_name" =>"Wally West", "superhero" =>"The Flash" ),
08                       array( "real_name" =>"Linda Danvers", "superhero" =>"Supergirl" ),
09                     );
10        // number of columns per row
11        $cols  = 3;
13        // calculate number of rows
14        $rows    = ceil( count( $data ) / $cols );
15        $counter = 0;
17        // loop for each row
18        for( $i = 0; $i <$rows; $i++ )
19        {
20          // clear cells from last row
21          $tmpl->clearTemplate( "cell" );
23          // put data for one row in a new array
24          $rowData = array();
25          for( $j = 0; $j <$cols; $j++ )
26          {
27            if( isset( $data[$counter] ) )
28              array_push( $rowData, $data[$counter++] );
29          }
30          // add the data of one row to the cells
31          $tmpl->addRows( "cell", $rowData );
32          // parse this row and append the data to previously parsed rows
33          $tmpl->parseTemplate( "row", "a" );
34        }
35        $tmpl->displayParsedTemplate();
36        ?>

The most important function is parseTemplate( “row”, “a” ), which parses a template and appends it to previously parsed contents of the template. This allows you to parse several rows.

Make use of array_chunk()
If you've already installed PHP 4.2.0 you may use array_chunk() to split the full data into arrays for each row.

Last Updated ( Thursday, 20 October 2005 )
< Prev   Next >