La seguente funzione visualizza in tabella un tipo di contenuto in ordine per Nid, volevo sapere come impostare l'ordine iniziale con $query .= tablesort_sql($header);
<?php
function node_showscript_nodes() {
global $language;
$header = array(
array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
array('data' => t('Changed'), 'field' => 'changed', 'sort' => 'asc'),
array('data' => t('Created'), 'field' => 'created', 'sort' => 'asc'),
array('data' => t('Nid'), 'field' => 'nid', 'sort' => 'asc'),
);
$query = "SELECT nid, title, created, changed FROM {node} WHERE type='script' AND status='1' AND language='$language->language'";
$query .= tablesort_sql($header);
$result = pager_query($query, 25);
while ($row = db_fetch_array($result)) {
$nid = $row['nid'];
$title = $row['title'];
$link = l(t($title), 'node/'.$nid, array('attributes' => array('class' => 'link', 'title' => $title)));
$formatted_created = date("d-m-Y H:i:s", $row['created']);
$changed = $row['changed'];
$formatted_changed = format_interval(time() - $changed);
$rows[] = array($link, $formatted_changed, $formatted_created, $row['nid']);
}
if (!$rows) {
$rows[] = array(array('data' => t('Non ci sono record in questa tabella'), 'colspan' => 4));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 25, 0);
print theme('page', $output);
}
?>
Se header cambia (codice sotto) l'ordine iniziale per titolo, ma io vorrei comunque lasciare il primo campo Title?! Come utilizzzare tablesort_sql?
<?php
$header = array(
array('data' => t('Nid'), 'field' => 'nid', 'sort' => 'asc'),
array('data' => t('Changed'), 'field' => 'changed', 'sort' => 'asc'),
array('data' => t('Created'), 'field' => 'created', 'sort' => 'asc'),
array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
);
?>
L'unica cosa che devi fare è definire il parametro sort per il tuo campo di default. Nel tuo caso tu vorresti title come campo ordinato di default, quindi il tuo header diventerebbe:
<?php
$header = array(
array('data' => t('Nid'), 'field' => 'nid'),
array('data' => t('Changed'), 'field' => 'changed',),
array('data' => t('Created'), 'field' => 'created',),
array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
);
?>
Gli altri campi saranno comunque ordinabili (hai definito nell'array il parametro field).
Non sono convinta perchè in http://api.drupal.org/api/drupal/includes--tablesort.inc/function/tables... trovo:
tablesort_sql($header, $before = '');
probabilmente il parametro $before indica campo con qui fare l'ordinamento iniziale. Grazie per la risposta comunque.
il before serve a tutt'altra cosa veramente. se guardi nella documentazione:
premetto che non ho mai usato il parametro before, ma li ci potresti passare un valore tipo
LIMIT 1,10
e questo verrà aggiunto alla query generata.
cmq tablesort_sql l'ho usato parecchie volte, posso assicurarti che funziona così :D
Grazie!
Vedi http://www.danzisiweb.altervista.org/sviluppo/node/shownodes/script
C
i
a
o
Ecco come utilizzare tablesort_sql($header, $before=''); In pratica se ho una tabella di n campi posso decidere via script quale campo va ordinato per primo all'apertura della pagina, poi posso cliccare sulle intestazioni per avere l'ordinamento per colonna. Funziona!
$query .= tablesort_sql($header, $before='name,');
Alla prossima