0

我需要将简单数据从数据库打印到特定块,我使用了下面给出的代码并输出了文本,但它不在指定的块中(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;
}
4

1 回答 1

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;
}
于 2013-03-20T04:59:27.327 回答