2016-08-23 5 views
0

私はDrupalの新機能を使用しており、ユーザーがナビゲートできるページを生成するカスタムモジュールを作成しようとしています。ユーザーがページをクリックすると、データベースからすべてのコンテンツピースのタイトルを取得し、table_themeを使用してテーブルに配置しようとしています。テーブル内の行を生成する以外はすべて動作しています。これは、ページの上部に私にこのメッセージを与えている:theme_table()のforeach()に無効な引数が指定されていますが、有効な配列を渡していますか?

「警告:theme_tableでforeachのために供給無効な引数()()(/srv/bindings/4d0bbd3c7be847abb26f62e0dacbcc24/code/includes/theme.incのライン2107)。 "

このメッセージは、データベースから取得するタイトルごとに1回表示されます。ここに私の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;  
    } 

問題はテーマ機能の私の使用と一緒に表示されます。私はどのように私の議論が無効であるかはわかりません。私は 'テーブル'のテーマタイプを渡しています。チェックした2つの配列は空ではありません(そのため、データベースからタイトルを格納した配列をprint_rで印刷します)。私はここでかなり困惑している。どのような問題が起こる可能性がありますか?あなたは以下のように使用しなければなりません

+0

'{ノードは}'これはあなたのテーブル名ですか?これは '\' tablename \ ''で正しく引用されるべきですか? – Ghost

+0

@Ghostはい、nodeはテーブル名です。私はここで説明されているように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'); 

おかげ

関連する問題