将数据从数据库打印到特定块内容

时间:2013-03-20 04:49:43

标签: drupal

我需要将一个简单的数据从数据库打印到一个特定的块,我已经使用了下面给出的代码并输出了文本,但它不在指定的块中(hello_world)。

function hello_world_block_view($delta = '') {
  $block = array();

  if ($delta == 'hello_world') {
 $sql = "SELECT Test_name FROM Test_table";
        $result = db_query($sql);
        $record = $result->fetch(); 
        foreach ($record as $records) {
             echo $records;
        }

    $block['subject'] = t('Hello world Subject');
    $block['content'] = t('Need to print database content');
  }

  return $block;
}

1 个答案:

答案 0 :(得分:1)

您需要将变量$records$block['content']相关联。所以它看起来像:

function hello_world_block_view($delta = '') {
  $block = array();

  if ($delta == 'hello_world') {
    $output = '';
    $sql = "SELECT Test_name FROM Test_table";
    $result = db_query($sql);
    $record = $result->fetch(); 
    foreach ($record as $records) {
      $output .= $records;
    }

    $block['subject'] = t('Hello world Subject');
    $block['content'] = $output;
  }

  return $block;
}