Articles
function category_select_art($cids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $distant = FALSE, $order = 'n.sticky DESC, n.created DESC') {
if (count($cids) > 0) {
// For each category ID, generate an array of descendant category IDs to the right depth.
$descendant_cids = array();
if ($depth === 'all' || $depth < 0) {
$depth = NULL;
}
foreach ($cids as $index => $cid) {
$category = category_get_category($cid);
$cnid = $category->cnid ? $category->cnid : $cid;
$tree = category_get_tree($cnid, $cid, -1, $depth, $distant);
$descendant_cids[] = array_merge(array($cid), array_map('_category_get_cid_from_category', $tree));
}
$group_by = 'n.nid, n.sticky, n.title, n.created';
if ($operator == 'or') {
$str_cids = implode(',', call_user_func_array('array_merge', $descendant_cids));
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {category_node} cn ON n.nid = cn.nid WHERE cn.cid IN ('. $str_cids .') AND n.status = 1 GROUP BY '. $group_by .' ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {category_node} cn ON n.nid = cn.nid WHERE cn.cid IN ('. $str_cids .') AND n.status = 1';
}
else {
$joins = '';
$wheres = '';
foreach ($descendant_cids as $index => $cids) {
$joins .= ' INNER JOIN {category_node} cn'. $index .' ON n.nid = cn'. $index .'.nid';
$wheres .= ' AND cn'. $index .'.cid IN ('. implode(',', $cids) .')';
}
$sql = 'SELECT n.nid, n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' GROUP BY '. $group_by .' ORDER BY '. $order;
$sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE n.status = 1 ' . $wheres;
}
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 5), 0, $sql_count);
}
else {
$result = db_query($sql);
}
}
return $result;
}
$nid = (int)arg(1);
$cid = $_GET['cid'];
if($cid != null){
$result = db_query(db_rewrite_sql('SELECT cft.field_topics_nid FROM {content_field_topics } cft INNER JOIN {node} n ON cft.nid = n.nid AND cft.vid = n.vid WHERE n.nid = %d AND n.status = 1'), $cid);
$cids = array();
while ($cat = db_fetch_object($result)) {
$cids[] = $cat->field_topics_nid;
}
$result2 = category_select_art($cids, 'or', -1, FALSE, FALSE, 'n.created DESC');
$nodes = array();
while ($node = db_fetch_object($result2)) {
$nodes[] = $node->nid;
}
$sql2 = "SELECT n.nid, n.title, n.type, u.name, u.uid, n.created FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.type = 'article' AND n.nid IN (" . implode(', ', $nodes) . ")";
$header = array(
array('data' => t('Title'), 'field' => 'n.title'),
array('data' => t('User'), 'field' => 'name'),
array('data' => t('Created'), 'field' => 'created', 'sort' => 'desc'),
);
$result3 = pager_query(db_rewrite_sql($sql2 . tablesort_sql($header)), 25, 0);
while ($data = db_fetch_object($result3)) {
$rows[] = array(
l($data->title, "node/$data->nid"),
l($data->name, "user/$data->uid"),
format_interval(time() - $data->created, 2),
);
}
print theme('table', $header, $rows);
print theme('pager', NULL, 25);
}
?>
>