0

I am studying how to create a custom module in Drupal7, so far I have created a simple module that will accept subscribers by registering.

These are the fields I have for my database, and has finished the function that will accept user input.

Table: simple_subscribe

sid , firstname, lastname, email

I then proceeded to create the function that will display all of the subscribers so far this is what I got

function subscription_view_form() {
  $result = db_query("SELECT * from subscribe");

  $record = $result->fetchAll();  
  echo '<pre>';
  print_r($record);
  echo '</pre>';
}

I am using print_r($record); to display the information, but all it displays is the objects on top of the website, before the navigation

Array
(
    [0] => stdClass Object
        (
            [sid] => 1
            [firstname] => John
            [lastname] => Doe
            [email] => [email protected]
        )

    [1] => stdClass Object
        (
            [sid] => 2
            [firstname] => Test
            [lastname] => Beta
            [email] => [email protected]
        )

)

edit

I am using the hook_menu() to display a form on /subscriptions but I can't output the subscribers inside the content area, I also wanted to list it in a table, I only did the print_r($records) to test if it is fetching the data. I wanted to know how to output it properly inside the content area not above it?

function subscription_menu() {
  $items[ 'subscriptions' ] = array( 
    'title' => 'View the Subscribers', 
    'description' => 'Subscription form available for subscribers',
    'page callback' => 'drupal_get_form',
    'page arguments' => array( 'subscription_view_form' ),
    'access arguments' => array( 'access content' ),
    'file' => 'subscription.pages.inc'
  );

  return $items;
4
  • what's your specific question? your code is working as you've told it to. print_r() is going to print when the function is run.
    – jdu
    Commented Jan 28, 2014 at 18:42
  • indeed. so are we to assume that subscription_view_form() is a page callback in a hook_menu() item? if so, it is supposed to return information, not print it directly; otherwise, yes, this is exactly what will happen. or is it being called somehow or somewhere else. a lot more detail is going to be needed to help you out further.
    – Jimajamma
    Commented Jan 28, 2014 at 18:48
  • @jdu I have edited my question and added more explanation.
    – lozadaOmr
    Commented Jan 29, 2014 at 0:32
  • @Jimajamma I used hook_menu() but I am not aware of how to output it properly, I forgot to mention I was using print_r for testing, because I don't know how to display it inside the content area. Please see my edited question
    – lozadaOmr
    Commented Jan 29, 2014 at 0:33

1 Answer 1

1

I really think you should dive into Drupal's Examples Module to look at how a lot of the hooks and so forth you are (mis)using actually work.

But, quickly:

function subscription_menu() {

  $items=array();

  $items[ 'subscriptions' ] = array( 
    'title' => 'View the Subscribers', 
    'description' => 'Subscription form available for subscribers',
    'page callback' => 'subscription_records',
    'access arguments' => array( 'access content' ),
    'file' => 'subscription.pages.inc'
  );

  return $items;

}

and

function subscription_records() {

  $result = db_query('SELECT * FROM {subscribe}');
  $records = $result->fetchAll();

  $content = '<pre>' . print_r($records, TRUE) . '</pre>';

  return $content;

}

clean up most of the glaring misunderstandings.

However, you will still need to look into Render Arrays and Dynamic Queries to really clean up your code, as well as the Drupal Theming Guide with special attention to theme_table() to create the page you seem to be after. And, of course, if you need user input, the Form API Reference is a must. But, once again, all of these are explained and demonstrated in the Examples Module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.