display content in table in drupal 7

Solutions on MaxInterview for display content in table in drupal 7 by the best coders in the world

showing results for - "display content in table in drupal 7"
Daniele
27 May 2020
1<?php
2  echo show_table('blocks', 50);
3
4function show_table($table = NULL, $rows_per_page = 20) {
5  if (!$table || !db_table_exists($table)) {
6	  drupal_set_message(t('You must supply a valid database table name.'), 'error');
7		drupal_access_denied();
8	}
9
10  // We get the first (or only) part of the Primary key to be added to the sort sequence.
11  $result = db_query("SHOW INDEX FROM {$table}");
12  $x = db_fetch_array($result);
13  if ($x === FALSE) {
14    drupal_set_message(t("The '@table' table has no index defined. This is probably normal.", array('@table' => $table)), 'notice');
15    $first_key = NULL;
16  }
17  else {
18    $first_key = $x['Column_name'];
19  } 
20
21  drupal_set_title(t('@table Table Contents', array('@table' => ucwords($table))));
22  $output = '<p>'. t('Click on a column title to sort by that column.') .'</p><br/>';
23  $rows = array();
24
25  // Now we get the column names from the table and build the header.
26  $header = array();
27  $result = db_query("SHOW COLUMNS FROM {$table}");
28
29  while ($col_desc = db_fetch_array($result)) {
30    $header[] = array(
31      'data' => ucwords(str_replace('_', ' ', $col_desc['Field'])),
32      'field' => '`'. $col_desc['Field'] .'`',
33      );
34  }
35  
36  // Get the data rows from the table.
37  $select = "SELECT * FROM {$table}";
38  // Set it up so that the user can sort on any column, but the primary key will always be the last value to sort on.
39  $select .= tablesort_sql($header) . ($first_key ? (', '. $first_key .' ASC') : NULL);
40  // Do the query so that we can page the data.
41  $result = pager_query($select, $rows_per_page);
42
43  while ($row = db_fetch_array($result)) {
44    $line = array();
45    foreach ($row as $key => $value) {
46      // We use check_markup to apply our filters.
47      $line[] = check_markup($value, FILTER_FORMAT_DEFAULT,TRUE);
48    }
49    $rows[] = $line;
50  }
51
52  // Build the displayable table.
53  $output .= theme('table', $header, $rows);
54  $output .= theme('pager', $rows_per_page);
55  return $output;
56}
57?>
58