views:

20

answers:

2

I have approximately 20 content-taxonomy check boxes from one field ("features"). The checked terms display in node-example.tpl.php. I am trying to show these content-taxonomy terms in two columns displayed/sorted in a downward order instead of across.

I am trying to cobble two bits of code to accomplish this...but my php skills are not yet up to the challenge. I can't get all of the array values generated in the foreach loop to be recognized by the second section of code.

The code below was taken (and modified) from the following sources:

http://drupal.org/node/312812

roscripts.com/PHP_display_data_on_columns-127.html

I am trying to use the following code in my node-example.tpl.php file.

<?php
  echo '<table>';
  foreach ($node->field_features as $delta => $value){
  $term = taxonomy_get_term($node->field_features[$delta]['value']);
  $term_name = check_plain($term->name);
  }

  // Default # of Columns
  $numcols = 2;

  // Number of Items
  $numitems = count($term_name);

  // Number of Rows
  $numrows = ceil($numitems/$numcols);


    for ($row=1; $row <= $numrows; $row++)
    {
        $cell = 0;
        echo ' <tr>'."\n";
        for ($col=1; $col <= $numcols; $col++)
        {
        echo '  <td>'."\n";

        if ($col===1)
        {
            $cell += $row;
            print $term_name[$cell - 1];
        }
        else {
            $cell += $numrows;
            print $term_name[$cell - 1];
        }
        echo '  </td>'."\n";
        }
        echo ' </tr>'."\n";
    }

  echo '</table>';

?>
A: 

Ok, I got this to work using the answer to another foreach-loop question/answer on this site.

Declare the $items array outside the loop and use $items[] to add items to the array.

This is the final code that does exactly what I wanted.

<?php
  echo '<table>';

  $items = array();
  foreach ($node->field_features as $delta => $value)
  {
  $term = taxonomy_get_term($node->field_features[$delta]['value']);
  $term_name = check_plain($term->name);
  $items[] = $term_name;
  }

  // Default # of Columns
  $numcols = 2;

  // Number of Items
  $numitems = count($items);//print $numitems;

  // Number of Rows
  $numrows = ceil($numitems/$numcols);//print $numrows;


    for ($row=1; $row <= $numrows; $row++)
    {
        $cell = 0;
        echo ' <tr>'."\n";
        for ($col=1; $col <= $numcols; $col++)
        {
        echo '  <td>'."\n";

        if ($col===1)
        {
            $cell += $row;
            print $items[$cell - 1];
        }
        else {
            $cell += $numrows;
            print $items[$cell - 1];
        }
        echo '  </td>'."\n";
        }
        echo ' </tr>'."\n";
    }

  echo '</table>';

  ?>
Jeff843
A: 

You can use theme('table', $headers, $rows) to render your table using the theme engine.

// Default # of Columns
$numcols = 2;
$rows = array();
$cell_count = 0;
foreach ($node->field_features as $delta => $value) {
  $term = taxonomy_get_term($node->field_features[$delta]['value']);
  $term_name = check_plain($term->name);
  $cell_count += 1;
  $row_index = floor($cell_count / $numcols);
  $rows[$row_index][] = $term_name;

}
print theme('table', array(), $rows);
mongolito404
Thanks for your alternative approach. What would you add to your script to make it sort/print columns in a top to bottom order rather than across?
Jeff843
Outside the loop get the size the columns with $col_size = ceil(count($node->field_features) / $numcols). Inside the loop, use $col_index instead of $row_index, like this $col_index = floor(($cell_count - 1) / $col_size); $rows[][$col_index] = $term_name;
mongolito404