Come impostare l'ordine iniziale con tablesort_sql?

6 contenuti / 0 new
Ultimo contenuto
Come impostare l'ordine iniziale con tablesort_sql?

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:

Quote:
$before An SQL string to insert after ORDER BY and before the table sorting code. Useful for sorting by important attributes like "sticky" first.

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

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!

<?php
function role_show_role() {
   global
$language;
      
$header = array(
      array(
'data' => t('Cid'), 'field' => 'cid', 'sort' => 'asc'),
        array(
'data' => t('Rid'), 'field' => 'rid', 'sort' => 'asc'),
        array(
'data' => t('Name'), 'field' => 'name', 'sort' => 'asc'),
     );
    
$query = "SELECT rid, name FROM {role}";
?>

$query .= tablesort_sql($header, $before='name,');
<?php
     $result
= pager_query($query, 25);
     while (
$row = db_fetch_array($result)) {
       
$rows[] = array($row['cid'], $row['rid'], $row['name']);
     }
     if (!
$rows) { $rows[] = array(array('data' => t('Non ci sono record in questa tabella'), 'colspan' => 3)); }
    
$output = theme('table', $header, $rows);
    
$output .= theme('pager', NULL, 25, 0);
     print
theme('page', $output);
     exit;
}
?>

Alla prossima