theme_table() 中为 foreach() 提供的参数无效,但我传递的是有效数组?
Invalid argument supplied for foreach() in theme_table() but I'm passing valid arrays?
好的,所以我是 Drupal 的新手,我正在尝试创建一个自定义模块来生成用户可以导航到的页面。当用户单击该页面时,我试图从数据库中检索所有内容片段的标题,并使用 table_theme 将它们排列在 table 中。除了在 table 中生成行外,一切正常。它在页面顶部给我这条消息:
"Warning: Invalid argument supplied for foreach() in theme_table() (line 2107 of /srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc)."
对于我从数据库中检索到的每个标题,此消息都会出现一次。这是我的 content_titles.module 文件(我的自定义模块文件):
<?php
/**
* @file
* A module that creates a page to display all existing content titles
*/
/**
* Implements hook_help()
*
* Displays help and module information
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function content_titles_help($path, $arg) {
switch ($path) {
case "admin/help#content_titles":
return '' . t("Displays all existing content titles
on an overview page") . '';
break;
}
}
/**
* Implements hook_menu()
*
* Enables the module to register a link to be placed in a menu
*/
function content_titles_menu() {
$items['test/content_titles'] = array(
'title' => 'Overview Test Page',
'page callback' => 'content_titles_simple',
'access callback' => TRUE,
'expanded' => TRUE,
);
return $items;
}
/**
* Constructs the Content Titles page
*/
function content_titles_simple() {
return array('#markup' => create_content_table());
}
/**
* This function will create the table to hold the content titles in
*/
function create_content_table() {
// Retrieve content titles from the database
$results = db_query('SELECT title FROM {node}');
$header = array('Content Titles');
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
print_r($rows); // FOR DEBUGGING PURPOSES
$output = theme('table', array('header' => $header,
'rows' => $rows));
return $output;
}
问题似乎与我使用主题功能有关。我不明白我的任何论点是如何无效的。我正在传递 'table' 的主题类型,并且我检查过的两个数组不为空(这就是为什么我使用 print_r 来打印我从数据库中存储标题的数组的原因)。我在这里很困惑。知道问题出在哪里吗?
你必须使用如下:
$header = array(
array('data' => t('Content Titles'), 'field' => 'title'),
);
$rows = array();
$query = db_select('node', 'n')->fields('n', array('title'));
$pager = $query->extend('PagerDefault')
->limit(10);
$results = $pager->execute();
foreach ($results as $row) {
$rows[] = array($row->title);
}
return theme('table', array(
'header' => $header,
'rows' => $rows)) . theme('pager');
谢谢
谢谢大家的帮助,我搞定了!我需要将数组推入 $rows 数组而不是值。我更改了这部分代码:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
收件人:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = array($row['title']);
}
好的,所以我是 Drupal 的新手,我正在尝试创建一个自定义模块来生成用户可以导航到的页面。当用户单击该页面时,我试图从数据库中检索所有内容片段的标题,并使用 table_theme 将它们排列在 table 中。除了在 table 中生成行外,一切正常。它在页面顶部给我这条消息:
"Warning: Invalid argument supplied for foreach() in theme_table() (line 2107 of /srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc)."
对于我从数据库中检索到的每个标题,此消息都会出现一次。这是我的 content_titles.module 文件(我的自定义模块文件):
<?php
/**
* @file
* A module that creates a page to display all existing content titles
*/
/**
* Implements hook_help()
*
* Displays help and module information
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function content_titles_help($path, $arg) {
switch ($path) {
case "admin/help#content_titles":
return '' . t("Displays all existing content titles
on an overview page") . '';
break;
}
}
/**
* Implements hook_menu()
*
* Enables the module to register a link to be placed in a menu
*/
function content_titles_menu() {
$items['test/content_titles'] = array(
'title' => 'Overview Test Page',
'page callback' => 'content_titles_simple',
'access callback' => TRUE,
'expanded' => TRUE,
);
return $items;
}
/**
* Constructs the Content Titles page
*/
function content_titles_simple() {
return array('#markup' => create_content_table());
}
/**
* This function will create the table to hold the content titles in
*/
function create_content_table() {
// Retrieve content titles from the database
$results = db_query('SELECT title FROM {node}');
$header = array('Content Titles');
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
print_r($rows); // FOR DEBUGGING PURPOSES
$output = theme('table', array('header' => $header,
'rows' => $rows));
return $output;
}
问题似乎与我使用主题功能有关。我不明白我的任何论点是如何无效的。我正在传递 'table' 的主题类型,并且我检查过的两个数组不为空(这就是为什么我使用 print_r 来打印我从数据库中存储标题的数组的原因)。我在这里很困惑。知道问题出在哪里吗?
你必须使用如下:
$header = array(
array('data' => t('Content Titles'), 'field' => 'title'),
);
$rows = array();
$query = db_select('node', 'n')->fields('n', array('title'));
$pager = $query->extend('PagerDefault')
->limit(10);
$results = $pager->execute();
foreach ($results as $row) {
$rows[] = array($row->title);
}
return theme('table', array(
'header' => $header,
'rows' => $rows)) . theme('pager');
谢谢
谢谢大家的帮助,我搞定了!我需要将数组推入 $rows 数组而不是值。我更改了这部分代码:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = $row['title'];
}
收件人:
$rows = array();
while ($row = $results->fetchAssoc()) {
$rows[] = array($row['title']);
}