a little help with theme_table
slamMan - March 8, 2006 - 18:50
I am new to using theme_template and it has me a bit confused. The following function breaks my site i.e. module including this function is activated the site displays only blank page. Remove this function and all is ok. I am piecing this together an don't understand all of functions it is using yet so I am hoping someone with more experience can help me fix this. Once I have it working I am sure I can back into how we got there.
function pipe_show() {
//Set up the table Headings
$header = array(
array('data' => t('ID'), 'field' => 'nid'),
array('data' => t('First Name'), 'field' => 'client_fname'),
array('data' => t('Last Name'), 'field' => 'client_lname'));
$sql = 'SELECT client_fname, client_lname, nid FROM {node_client} u WHERE nid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$records = db_fetch_object($result)
// get the data
$rows[] = array($records->uid, format_name($records), $records->client_fname, format_name($records), $records->client_lname, format_name($records));
}
$pager = theme('pager', NULL, 50, 0, tablesort_pager());
if (!empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => '3'));
print theme('table', $header, $rows);
} 
It seem as if you forgot a 'while'
try this one:
<?phpfunction pipe_show() {
//Set up the table Headings
$header = array(
array('data' => t('ID'), 'field' => 'nid'),
array('data' => t('First Name'), 'field' => 'client_fname'),
array('data' => t('Last Name'), 'field' => 'client_lname'));
$sql = 'SELECT client_fname, client_lname, nid FROM {node_client} u WHERE nid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
while ($record = db_fetch_object($result)) {
// get the data
$rows[] = array($record->nid, $record->client_fname, $record->client_lname);
}
$pager = theme('pager', NULL, 50, 0, tablesort_pager());
if (!empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => '3'));
}
print theme('table', $header, $rows);
}
?>
Two main points, missing loop and what you print
There are two main points with your code you need to address. One, you need a loop to process all the rows of data and you are currently only printing the table and only if there are enough clients to require paging. There is also the question of how you build the row(s), you have six columns and I am unsure of what you intended with the calls to format_name. (those calls have been removed below). Below is a modified version that should adress this
<?php
function pipe_show() {
//Set up the table Headings
$header = array(
array('data' => t('ID'), 'field' => 'nid'),
array('data' => t('First Name'), 'field' => 'client_fname'),
array('data' => t('Last Name'), 'field' => 'client_lname'));
$sql = 'SELECT client_fname, client_lname, nid FROM {node_client} u WHERE nid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
// Loop through all the records
while ( $records = db_fetch_object($result) ) {
// Build a row for the table from the data
$rows[] = array($records->uid, $records->client_fname, $records->client_lname);
}
$pager = theme('pager', NULL, 50, 0, tablesort_pager());
// Add the pager to the table if needed
if ( count($rows) && !empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => '3'));
}
// If we have rows for a table, theme the table
// otherwise set output to a message
if ( count($rows) ) {
$output = theme('table', $header, $rows);
}
else {
$output = "There are currently no clients";
}
// And finally, produce the page
print theme('page', $output);
}
?>
Thanks
I appreciate the responses. I wasn't sure if the loop was handled by one of the functions. In trying to disect how this was done with other modules I was unsure if it was needed.
the format_name() again is from another piece of code and I assumed it had something to do with applying css to the rows of data.
I will try these suggestions. Thanks for the help.
...Works perfectly. Thanks again.