Want option to list sortable table of nodes along with voting values
Jesse Grosjean - November 9, 2005 - 20:24
| Project: | Voting |
| Version: | 4.6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
I want to be able to display a sortable table of nodes listing each node and the number of votes and average votes for that node. I'm trying to create this myself, but I'm stuck. Here you can find what I have done so far. Please let me know if I'm going about this in the correct way and what I need to do next to make it work.

#1
Here's some code that will do this. I'm not sure how best to (or if it's really necessary) integrate with the voting.module, but for me at least it's quite useful.
<?php
$header = array(
array('data' => t('title'), 'field' => 'n.title'),
array('data' => t('changed'), 'field' => 'n.changed', 'sort' => 'desc'),
array('data' => t('rating'), 'field' => 'v.vavg'),
array('data' => t('votes'), 'field' => 'v.vcount'),
);
$sql = "SELECT n.nid, n.title,"
. " n.created, n.changed, count(v.vote) vcount, avg(v.vote) vavg"
. " FROM {node} n"
. " LEFT JOIN {votes} v ON n.nid = v.content_id"
. " WHERE n.type = 'contribution'"
. " GROUP BY n.nid, n.title, n.created, n.changed";
$sql .= tablesort_sql($header);
$result = pager_query(db_rewrite_sql($sql), 50);
while ($row = db_fetch_object($result)) {
$rows[] = array(l($row->title, "node/$row->nid"), date("Y-m-d", $row->changed), number_format($row->vavg, 1), $row->vcount);
}
if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
$rows[] = array(array('data' => $pager, 'colspan' => 3));
}
$output .= theme('table', $header, $rows);
?>