drupal 8 views relation to custom table code example
Example: display content in table in drupal 7
<?php
echo show_table('blocks', 50);
function show_table($table = NULL, $rows_per_page = 20) {
if (!$table || !db_table_exists($table)) {
drupal_set_message(t('You must supply a valid database table name.'), 'error');
drupal_access_denied();
}
$result = db_query("SHOW INDEX FROM {$table}");
$x = db_fetch_array($result);
if ($x === FALSE) {
drupal_set_message(t("The '@table' table has no index defined. This is probably normal.", array('@table' => $table)), 'notice');
$first_key = NULL;
}
else {
$first_key = $x['Column_name'];
}
drupal_set_title(t('@table Table Contents', array('@table' => ucwords($table))));
$output = '<p>'. t('Click on a column title to sort by that column.') .'</p><br/>';
$rows = array();
$header = array();
$result = db_query("SHOW COLUMNS FROM {$table}");
while ($col_desc = db_fetch_array($result)) {
$header[] = array(
'data' => ucwords(str_replace('_', ' ', $col_desc['Field'])),
'field' => '`'. $col_desc['Field'] .'`',
);
}
$select = "SELECT * FROM {$table}";
$select .= tablesort_sql($header) . ($first_key ? (', '. $first_key .' ASC') : NULL);
$result = pager_query($select, $rows_per_page);
while ($row = db_fetch_array($result)) {
$line = array();
foreach ($row as $key => $value) {
$line[] = check_markup($value, FILTER_FORMAT_DEFAULT,TRUE);
}
$rows[] = $line;
}
$output .= theme('table', $header, $rows);
$output .= theme('pager', $rows_per_page);
return $output;
}
?>