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