In a module that I am developing, I use a piece of code to generate a table:

while ($story = db_fetch_object($result)) {
  $rows[] = story_list_row($type, $fields, $story);
}

// ...

$output = theme_table($header, $rows);

Currently, I only return one row ($row1):

function story_list_row($type, $fields, $story) {
   $row1[] = array('data' => l($story->name, "user/$story->uid"));
   $row1[] = array('data' => date('d', $story->created), 'nowrap' => 'nowrap');
   $row2[] = array('data' => 'foo!', 'rowspan' => 2);

  return $row1;
}

Now, I want to return 2 rows ($row1 and $row2), since they contain related information. The resulting table should look like this:

+--------------+-----+
| story_name 1 | mon |
+--------------+-----+
| foo!               |
+--------------+-----+
| story_name 2 | fri |
+--------------+-----+
| foo!               |
+--------------+-----+
| story_name 3 | tue |
+--------------+-----+
| foo!               |
+--------------+-----+

Comments

Fluffy Convict’s picture

Sorry, wrong button ... so my question is how I should alter my code to return 2 table rows at once?

nevets’s picture

I would pass rows to your function () like this

while ($story = db_fetch_object($result)) {
  story_list_row($type, $fields, $story,  $rows);
}

// ...

$output = theme_table($header, $rows);

And change story_list_row() like this

function story_list_row($type, $fields, $story,   &$rows) {
   $row1[] = array('data' => l($story->name, "user/$story->uid"));
   $row1[] = array('data' => date('d', $story->created), 'nowrap' => 'nowrap');
   $rows[] = $row1;
   $row2[] = array('data' => 'foo!', 'rowspan' => 2);
   $rows[] = $row2;
}
Fluffy Convict’s picture

I then get an error: "Fatal error: Cannot use [] for reading in C:\xampp\htdocs\odisys\modules\story\story.module on line 344". Line 344 is where is says "$rows[] = $row1[]". Any thoughts?

nevets’s picture

My comment does not have line with $rows[] = $row1[], it does have $rows[] = $row1; so the problem may be the extra [].

Fluffy Convict’s picture

Thanks!

iimitk’s picture

Just merge the two arrays together:

function story_list_row($type, $fields, $story) {
   $row1[] = array('data' => l($story->name, "user/$story->uid"));
   $row1[] = array('data' => date('d', $story->created), 'nowrap' => 'nowrap');
   $row2[] = array('data' => 'foo!', 'rowspan' => 2);
   $allrows = array_merge($row1, $row2);

  return $allrows;
}

________________________
"Creativity is knowing how to hide your resources" - Albert Einstein.

Fluffy Convict’s picture

no, by merging the two arrays you only create one row with additional cells :)