Hi,,

Can anyone clear my doubt?????

As per my requirement I want to concatenate two fields of database and display under the single column of the header. I am using this code:

$header = array(
array('data' => t('Customer_id'), 'field' => 'cust.customer_id'),
array('data' => t('Last Name'), 'field' => 'cust.res_lname'), //here I want to add one more field cust.res_name
);

$result = db_select('customer_details', 'cust')
->extend('TableSort')
->extend('PagerDefault')
->limit(20)
->fields('cust',array('customer_id','res_lname','res_name'))
->orderByHeader($header)
->execute();
$rows = array();
foreach ($result as $row) {
$rows[] = array('data' => (array) $row);
}

$build['tablesort_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['rep-tab-end'] = array(
'#markup' => '

'.$output .= theme('pager', array('tags' => array())).'

'
);

Comments

Jaypan’s picture

In the future, please wrap your code in <?php ?> tags. It makes it easier for us to read.

Anyways:

$header = array(
  array('data' => t('Customer_id'), 'field' => 'cust.customer_id'),
  array('data' => t('Last Name'), 'field' => 'cust.res_lname'),
  array('data' => t('Res name'), 'field' => 'cust.res_name'),
);

$result = db_select('customer_details', 'cust')
->extend('TableSort')
->extend('PagerDefault')
->limit(20)
->fields('cust',array('customer_id','res_lname','res_name'))
->orderByHeader($header)
->execute();

$rows = array();
foreach ($result as $r) {
  $row = array();
  $row[] = $r->customer_id;
  $row[] = $r->res_lname;
  $row[] = $r->res_name;
  $rows[] = $row;
}

$build['tablesort_table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => t('Sorry, no results found'),
);
$build['pager'] = array(
  '#theme' => 'pager',
  '#tags' => array(),
);

I made a few small changes but the overall idea should work.

Jaypan’s picture

Actually, I just realized you may want two DB column values to be shown in one HTML column. In that case, you would do the following:

$header = array(
  array('data' => t('Customer_id'), 'field' => 'cust.customer_id'),
  array('data' => t('Last Name'), 'field' => 'cust.res_lname'),
);

$result = db_select('customer_details', 'cust')
->extend('TableSort')
->extend('PagerDefault')
->limit(20)
->fields('cust',array('customer_id','res_lname','res_name'))
->orderByHeader($header)
->execute();

$rows = array();
foreach ($result as $r) {
  $row = array();
  $row[] = $r->customer_id;
  $row[] = $r->res_lname . ' ' . $r->res_name;
  $rows[] = $row;
}

$build['tablesort_table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => t('Sorry, no results found'),
);
$build['pager'] = array(
  '#theme' => 'pager',
  '#tags' => array(),
);

Note that if you do this, the column will only be sortable on the first value, not the second.