2013-03-20 113 views
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; 
} 

回答

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; 
}