简体   繁体   English

为theme_table()中的foreach()提供了无效的参数,但是我正在传递有效的数组?

[英]Invalid argument supplied for foreach() in theme_table() but I'm passing valid arrays?

Ok, so I'm very new to Drupal and I'm trying to create a custom module that generates a page that the user can navigate to. 好的,所以我对Drupal还是陌生的,我正在尝试创建一个自定义模块,该模块生成用户可以导航到的页面。 When the user clicks on the page, I'm trying to retrieve the titles of all the content pieces from the database and arrange them in a table using table_theme. 当用户单击页面时,我试图从数据库中检索所有内容的标题,并使用table_theme将它们排列在表中。 Everything is working except the generating of the rows in the table. 除了在表中生成行之外,其他所有东西都在工作。 It's giving me this message at the top of the page: 它在页面顶部向我显示此消息:

"Warning: Invalid argument supplied for foreach() in theme_table() (line 2107 of /srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc)." “警告:为theme_table()中的foreach()提供了无效的参数(/srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc的第2107行)。

This message appears once for each title that I retrieve from the database. 对于我从数据库中检索到的每个标题,此消息都会出现一次。 Here is my content_titles.module file (my custom module file): 这是我的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;     
  }

The problem appears to be with my use of the theme function. 问题似乎出在我对主题功能的使用上。 I don't see how any of my arguments are invalid though. 我看不出我的任何论点如何无效。 I'm passing the theme type of 'table' and two arrays that I've checked aren't empty (that's why I use print_r to print the array that I store my titles from the database in). 我传递了“表”的主题类型,并且检查的两个数组都不为空(这就是为什么我使用print_r来打印存储数据库标题的数组)。 I'm pretty stumped here. 我在这里很困惑。 Any idea what the problem could be? 知道可能是什么问题吗?

Thanks for the help everyone, I figured it out! 感谢大家的帮助,我知道了! I needed to push arrays onto the $rows array instead of the values. 我需要将数组推入$ rows数组而不是值。 I changed this section of code: 我更改了这段代码:

$rows = array();
    while ($row = $results->fetchAssoc()) {
        $rows[] = $row['title'];
    }

To: 至:

$rows = array();
    while ($row = $results->fetchAssoc()) {
        $rows[] = array($row['title']);
    }

You must use as below: 您必须按以下方式使用:

  $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');

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM