如何解决在Drupal 7中呈现查询结果的正确方法是什么?
| 我已经生成了一个查询,如下所示,并将结果格式化为链接:$result = db_query(\"SELECT name FROM {taxonomy_term_data} WHERE vid = :val\",array(\':val\' => \'1\'));
$list = array();
foreach ($result as $record) {
$list[] = l($record->name,\'blog/\' . $record->name);
}
现在,我想将此数组呈现为无序列表,然后将其返回到块中。正确的功能/语法是什么?
另外,与渲染相关的功能在哪里有很好的参考?
在此先感谢您的帮助!
解决方法
注意,“呈现查询结果的正确方法”不存在,有很多方法。它们可以呈现为列表,表格和许多其他方式。您要的是呈现链接列表的正确方法,这些链接来自数据库是不相关的。
参见http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7。而且,除了直接调用theme()之外,您还可以使用所谓的可渲染数组,这是Drupal 7中的新功能,也是现在首选的可实现方法。
$result = db_query(\"SELECT name FROM {taxonomy_term_data} WHERE vid = :val\",array(\':val\' => \'1\'));
// Prepare renderable array,define which theme function shall be used.
// The other properties match the arguments of that theme function.
$list = array(
\'#theme\' => \'links\',\'#links\' => array(),);
foreach ($result as $record) {
// Add each link to the array.
$list[\'#links\'][] = array(\'title\' => $record->name,\'href\' => \'blog/\' . $record->name));
}
// Now you can call drupal_render() and return or print that result.
// If this is inside a block or page callback,you can also directly return
// $list and Drupal will call drupal_render() automatically when the rest of
// the page is rendered.
return drupal_render($list);
,这是做到这一点的一种方法。构建$vars
数组并将其传递给theme_item_list($vars)
:
$vars[\'items\'] = $list;
$vars[\'title\'] = \'Sort entries by category\';
$vars[\'type\'] = \'ul\';
$vars[\'attributes\'] = array(
\'id\' => \'blog-taxonomy-block\',);
$content = theme_item_list($vars);
return $content;
http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list/7
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。