douwen8424 2016-08-23 01:27
浏览 53
已采纳

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

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. 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. 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)."

This message appears once for each title that I retrieve from the database. Here is my content_titles.module file (my custom module file):

<?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). I'm pretty stumped here. Any idea what the problem could be?

  • 写回答

2条回答 默认 最新

  • duanhuan2301 2016-08-23 16:21
    关注

    Thanks for the help everyone, I figured it out! I needed to push arrays onto the $rows array instead of the values. 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']);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。