Hi,

I create a table from my database and is displayed ok in my page using hook_page, but i don´t know how can i display the same info inside a block, can somebody help me, please?

function fact_argentina_table_page() {
$output = '';

$results = db_query("SELECT * FROM {facts_indicator_country_table} LIMIT 5");

// Printing theme_table().
$output .= '

Argentina Facts

';
$headers = array(t(''), t('Value'), t('%Change'),);
$rows[] = array();
foreach($results as $result){
$rows[]=array(
$result->fact_indicator_name,
$result->fact_indicator_value,
$result->fact_indicator_change,

);
}

$output .= theme('table', array('header' => $headers, 'rows' => $rows));

return $output;

}

Comments

Jaypan’s picture

function mymodule_block_info()
{
  $blocks['some_block'] = array
  (
    'info' => t('Title to be shown on block admin page'),
  );
  return $blocks;
}

function my_module_block_view($delta = '')
{
  if($delta == 'some_block')
  {
    $block_content = some_function_that_generates_block_content();
    $block = array
    (
      'subject' => t('Title to be shown in the block'),
      'content' => $block_content,
    );
    return $block;
  }
}

The above is a skeleton structure of how to create a block programmatically.

luis_mejia’s picture

<?php

/**
* Implements hook_menu().
*/

function fact_argentina_table_menu() {
$items['fact-argentina-table'] = array(
'description'=>'Display Summary details from a country',
'page callback' => 'fact_argentina_table_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}

/**
* Implements hook_block_info().
*/
function fact_argentina_table_block_info() {
$blocks['factargentinatable'] = array(
'info' => t('Facts Table Argentina'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}

/**
* Implements hook_block_view().
*/
function fact_argentina_table_block_view($delta = '') {
$block = array();
if ($delta == 'factargentinatable') {
$block = array(
'subject' => t('Argentina Facts'),
'content' => fact_argentina_table_page() //Call page function with the query,
); //cause i want to dipslay the table in page and a block
}
return $block;
}

/**
* Implement hook_page
*/
function fact_argentina_table_page() {
$output = '';

$results = db_query("SELECT * FROM {facts_indicator_country_table} LIMIT 5");

// Printing theme_table().
$output .= '

Argentina Facts

';
$headers = array(t(''), t('Value'), t('%Change'),);
$rows[] = array();
foreach($results as $result){
$rows[]=array(
$result->fact_indicator_name,
$result->fact_indicator_value,
$result->fact_indicator_change,

);
}

$output .= theme('table', array('header' => $headers, 'rows' => $rows));

return $output;

}

Jaypan’s picture

Are you asking a question?

luis_mejia’s picture

No, after your answer finally everything ok,

i just put here the code to other people.

Thanks for all

Jaypan’s picture

No problem. For future reference, you can wrap your code in <?php ?> tags to make it more readable.

srinath1976’s picture

Hi Sir,

i am also following same solution given by you.

my code is

function products_menu() {

$items['products-menu'] = array(

    'title' => 'My Menu',
    
    'description' => 'Display Summary details from a Product',

    'page callback' => 'products_block_view',

    'access arguments' => array('access content'),
    
    'type' => MENU_NORMAL_ITEM,

    );
    
    $items['menu/submenu'] = array(

    'title' => 'My Sub menu',

    'page callback' => 'products_mysubmenu_page_callback',

    'access callback' => TRUE,

    'type' => MENU_NORMAL_ITEM,

    );

    return $items;

};

function products_block_info()
{
  $blocks['ProductList'] = array
  (
    'info' => t('List Of This Product'),
  );
  return $blocks;
}

function products_block_view($delta = '')
{
  if($delta == 'ProductList')
  {
    //$block_content = some_function_that_generates_block_content();
    $block = array
    (
      'subject' => t('Product List'),
      'content' => $retreve_products_content,
    );
    return $block;
  }
}

function retreve_products_content($delta = '')
{
  $output = '';

  $results = db_query("SELECT * FROM {products}");
  $headers = array(t('Id'), t('Name'), t('Type'), t('Catogery'), t('Type'), t('Price'), t('Qty'), t('FileName'));
$rows[] = array();
foreach($results as $result){
$rows[]=array(
$result->pid,
$result->pname,
$result->type,
$result->pcat,
$result->pprice,
$result->pqty,
$result->pqty
);
}

$output .= theme('table', array('header' => $headers, 'rows' => $rows));
return $output;
}

?>

hen i click My Menu the url is localhost:81/eshopy1/products-menu and displayed white screen.

Instead of calling products_block_view callback it is taking products-menu in the url

what changes i have to do sir please tell me

Thanks