Hey there.

I have been trying to populate theme_table with multiple rows in a while operation. How to I encode $rows() as an array that theme_table will iterate through several rows?

237 while ($data[] = db_fetch_object($result))
238 {
239
240 $rows = array($data[$count]->OBJ1,$data[$count]->OBJ2);
241
242
243 $count++;
244 }
245 $output .= theme_table($rows, $attributes = NULL);

This returns only the last row.

237 while ($data[] = db_fetch_object($result))
238 {
239
240 $rows = array($data[$count]->OBJ1,$data[$count]->OBJ2);
241 $output .= theme_table($rows, $attributes = NULL);
242
243 $count++;
244 }
245

This returns one table per line.

I am certain it's a basic Drupal concept that I missing. Can anyone take a second to clue me in?

Thanks in advance.
- Dan.

Comments

jaypan’s picture

I do this:

$header = array(t('column 1'), t('column 2'), t('column 3'));
$rows = array();
while($db_row = db_fetch_array($rows))
{
  $row = array();
  $row[] = $db_row['column1'];
  $row[] = $db_row['column2'];
  $row[] = $db_row['column3'];
  $rows[] = $row;
}
if(!count($rows))
{
  $row = array();
  $row[] = array('data' => t('No data found'), 'colspan' => 3);
  $rows[] = $row;
}
$table = theme('table', $header, $rows);

Hopefully this skeleton can help you find the problem in your own code.

Contact me to contract me for D7 -> D10/11 migrations.

duckzland’s picture

Try this and see if its working


while ($data[] = db_fetch_object($result))
 {

 $rows = array($data[$count]->OBJ1,$data[$count]->OBJ2); // change this to $rows[] = array($data[$count]->OBJ1,$data[$count]->OBJ2);
 $output .= theme_table($rows, $attributes = NULL);

 $count++;
 }

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

DLBaker’s picture

OK, I have something closer to what I am looking for. It makes the rows for the proper number of entities, but it's not populating the tables with any content. It's making the alternate colored bars, and there's the right amount of bars. Here's the code:

10 {
20 $result = db_query("SELECT * FROM `generic_table_name` WHERE 1");
30 $output = ''; $count = 0; $num_rows = FALSE;
40
50 $caption=t("List of objects");
60 while ($data = db_fetch_object($result))
70 {
80 $row = array();
90 $row[] = array($data->OBJ1, $data->OBJ2);
100 $rows[] = $row;
110
120 }
130
140 $output .= theme_table($header, $rows, $attributes = NULL, $caption);
150 return $output;
160
170 }

This produces all the rows I am expecting, but with nothing in them.

When I reference $rows[0] in line 140 I get the Object name, from OBJ1 and the property of OBJ1 represented by OBJ2 on the one line. The reference changes when I alter [x].

When I remove $header and $caption I get the following Error:

warning: htmlspecialchars() expects parameter 1 to be string, array given

Anyone have any ideas? I would appreciate some more feedback, and appreciate that which has been already given!

- Dan.

jaypan’s picture

1) Show us your actual code. It's hard to decipher code that isn't real.
2) To make your SQL queries more efficient, you should select the column names from the table, not *. With *, the db engine has to first query the table names, then get the data for those names. Even if you are getting all columns, you should still list them explicitly, this will increase your query efficiency. And on top of this, it will make it easier for us to decipher your problem, seeing as we can't see your column names when you use *.

Contact me to contract me for D7 -> D10/11 migrations.

DLBaker’s picture

Hey.

Thanks for the info on not using *.

As for real code thats pretty much it. The only thing I changed was table names. I was attempting to be generic to prevent distraction by the content.

Replace "List of Objects" with "List of Relationships". OBJ1 with Name, and OBJ2 with Affiliation "Select Name, Affiliation from relations where 1";

The structure is completely identical. I can get rid of $num_rows = FALSEl I haven't had the need to keep that there. And $count.


	else # We are Admin and will get a list of relations to administrate	
	{
	$result = db_query("SELECT name, affiliation  FROM `relationships` WHERE 1");
  	$output = ''; 

  	$caption=t("<b>List of relations</b>");
  		while ($data = db_fetch_object($result)) 
  			{
				$row = array();
				$row[] = array($data->name, $data->affiliation);
				$rows[] = $row;
				
			}

		$output .= theme_table($header, $rows, $attributes = NULL, $caption);	
		#$output = theme('table', $header, $rows);		
	 	return $output;         
                
	}

The call to theme_table(... or theme('table'... both produce the same result.

Any help is greatly appreciated.

- Dan.

heine’s picture

From http://api.drupal.org/api/function/theme_table/6 :

$rows An array of table rows. Every row is an array of cells, or an associative array with the following keys:

"data": an array of cells
Any HTML attributes, such as "class", to apply to the table row.

So, your while loop should be:

while ($data = db_fetch_object($result)) 
 {
  $row = array();
  $row[] = $data->name;
  $row[] = $data->affiliation;
  $rows[] = $row;
}

or the equivalent:

while ($data = db_fetch_object($result)) 
 {
  $rows[] = array($data->name, $data->affiliation);
}
DLBaker’s picture

Thanks M8.

I figured it out at the same time you posted this here.

I love the Drupal community!

- Dan.

heine’s picture

Make sure to read http://drupal.org/writing-secure-code as well; your code likely needs some escaping (http://drupal.org/node/28984).

DLBaker’s picture

On a hunch I took a look at the source and found something interesting, but I have no idea how to fix it.

            <table>
<caption><b>List of Relationships</b></caption>
<tbody>
 <tr class="odd"><td 0="Aniwaya Anilasdalv" 1="Rowan Industrial Council"></td> </tr>
 <tr class="even"><td 0="Laodell" 1="Relevent Technologies"></td> </tr>
 <tr class="odd"><td 0="Braxthis" 1="Relevent Technologies"></td> </tr>
 <tr class="even"><td 0="Adelei Dimora" 1="Relevent Technologies"></td> </tr>
 <tr class="odd"><td 0="Alitoar Fulao" 1="Relevent Technologies"></td> </tr>
</tbody>
</table>
          
duckzland’s picture

this is a snippet from my previous module that uses table with theme_table function, see if this can work with you or you can get some clue out of it

$rows = array();
// set the table header along with css class
    $headers = array(
        array( 'data' => t('Name'), 'class' => 'header td-name'), 
	array( 'data' => t('RSVP'), 'class' => 'header confirmed'), 
	array( 'data' => t('Roles'), 'class' => 'header roles'),
	array( 'data' => t('Priority'), 'class' => 'header priority'),
	array( 'data' => t('Notes'), 'class' => 'header notes'),
	array( 'data' => t('Action'), 'class' => 'header action'),
    );

// fetch the sql object and parse it to the row / column
  while ($data = db_fetch_object($results)) {
  	$row = array( 
		'data' => array(
		 array('data' => $data->somefield, 'class' => 'name'), // column one per x header
		 array('data' => $data->somefield, 'class' => 'confirmed'), // column two
		 array('data' => $data->somefield, 'class' => 'roles'),  // column three
		 array('data' => $data->somefield, 'class' => 'priority'), // column four
		 array('data' => $data->somefield, 'class' => 'notes'), // column five
		 array('data' => $data->somefield, 'class' => 'delete'), // column six
		 ),
    );

   $rows[] = $row; merge the $row data to the $rows array	

  $attributes = array('id' => 'guest-table', 'class' => 'guest-table'); // for setting the table css id and class
  $output = theme('table', $headers, $rows , $attributes); 

return $output;

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com