Manuraj
Manuraj

Reputation: 13

Print data from database to a particular block content

I need to print a simple data from database to a particular block, I have used the code given below and got the text out put but it is not located in the block specified(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;
}

Upvotes: 0

Views: 168

Answers (1)

Vadim Eremeev
Vadim Eremeev

Reputation: 479

You need to connect variable $records with $block['content']. So it can looks like:

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

Upvotes: 1

Related Questions