Create a standard page and table in drupal module with PHP
Asked Answered
P

3

5

I made a module with a simple menu structure. I am able to programmatically retrieve a view of all my students in PHP. Now, I want to return all the students on a page in a simple table.

  • How can I make a standard page?
  • How can a return the values in a simple table?

The structure of the table is

UGhentID Name student First name student Location student

12874749 Smith Nick New York . . .

Protuberancy answered 29/3, 2011 at 7:44 Comment(0)
M
16

If you want to create a new page, you need to use hook_menu in a module.

For exemple :

/**
 * Implementation of hook_menu.
 */
function mymodule_menu() {
  $items = array();

  $items['myPage'] = array(
    'title' => 'Finances',
    'page callback' => 'mymodule_page',
    'access callback' => 'user_access',
    'access argument' => array('access nodes'),
  );
  return $items
}

/**
 * Page callback
 */
function mymodule_page() {
  $output = mymodule_table();
  return $output;
}

You can see here that I call "mymodule_table()" in the page callback, this is where you build your table.

function mymodule_table() {
    $rows = array();
    // build the table header
    $header = array();
    for ($i = 1; $i <= 5; $i++) {
      $header[] = array('data' =>  $i, 'class' => 'header-class');
    }
    $row = array();
    for ($i = 1; $i <= 5; $i++) {
      $row[] = array('data' =>  $i, 'class' => 'row-class');
    }
    $rows[] = array('data' => $row);
    $output .= theme('table', $header, $rows, $attributes = array('class' => 'my-table-class'));
    return $output;
}

This should output a table, with a header a a line a rows, with 5 columns.

Mcvay answered 29/3, 2011 at 8:36 Comment(1)
This is outdated (probably for Drupal 6). For drupal 7 you need to use an updated version, where the parameters being passed to the function other than the first are an associative array. See my answer added below, I didn't have enough room in the comments.Iodize
K
4

I'm not sure what you mean by a 'standard page' but I think you probably want to take a look at the examples project (http://drupal.org/project/examples), in particular that page_example module.

For your table, Drupal provides a theme_table function which is pretty useful. In it's simplest form you pass an array of headers and rows and it returns the html for a table.

Kerrykersey answered 29/3, 2011 at 8:22 Comment(2)
Yeah I liked your comment ... when give the source, not only certain answer, it will be helpful for the otherBartie
There is also an example with Edit and Delete options per row as explained hereFacet
I
2

Based on @Haza's answer, here's an updated table creation function that works for Drupal 7:

function mymodule_table() {
    $rows = array();
    // build the table header
    $header = array();
    for ($i = 1; $i <= 5; $i++) {
      $header[] = array('data' =>  $i, 'class' => 'header-class');
    }
    $row = array();
    for ($i = 1; $i <= 5; $i++) {
      $row[] = array('data' =>  $i, 'class' => 'row-class');
    }
    $rows[] = array('data' => $row);
    $data = array(
        'header' => $header,
        'rows' => $rows,
        'attributes' => $attributes
    );
    $output = theme('table', $data);
    return $output;
}
Iodize answered 25/7, 2014 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.