2016-08-23 48 views
0

好的,所以我對Drupal非常陌生,我試圖創建一個自定義模塊來生成用戶可以導航到的頁面。當用戶點擊頁面時,我試圖從數據庫中檢索所有內容片段的標題,並使用table_theme將它們排列在表格中。除了生成表格中的行之外,所有內容都正在工作。它給我這個消息,在頁面的頂部:在theme_table()中爲foreach()提供了無效參數,但是我傳遞了有效數組?

「警告:提供的foreach)在theme_table(參數無效()(行/srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.inc 2107)。 「

此消息對於從數據庫中檢索的每個標題都會出現一次。這裏是我的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來打印從數據庫中存儲我的標題的數組)。我很難過這裏。任何想法可能是什麼問題?

+0

'{節點}'這是你的表名?應該正確引用'\'tablename \''? – Ghost

+0

@Ghost是的,節點是表名。我試圖按照這裏描述的方式關注Drupal的數據庫API:[https://www.drupal.org/node/310072](https://www.drupal.org/node/310072),它似乎工作,因爲我爲了調試目的而打印的數組包含正確的內容標題。 – ccmetz92

回答

1

感謝大家的幫助,我想它了!我需要將數組推送到$ rows數組而不是值。我改變了這部分代碼:

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

要:

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

必須如下使用:

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

感謝

相關問題