Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:26:00+00:00 2026-05-26T22:26:00+00:00

I use the code below to get all the categories from the am_category table

  • 0

I use the code below to get all the categories from the am_category table into a dropdownbox in a form. It works. But when submitting the form I need the category.ID and not the name. How does one usually work with lookup tables like this one?

$category_result = db_query("SELECT id, name FROM {am_category}");
$categories = array();
foreach($category_result as $row)
{
    $categories[$row->id] = t($row->name);
}   

$form['category_options'] = array( 
'#type' => 'value', 
'#value' => $categories
); 
$form['category']['category'] = array( 
'#title' => t('Category'), 
'#type' => 'select', 
'#description' => t('Please select the category of this achievement.'), 
'#options' => $form['category_options']['#value'] 
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:26:00+00:00Added an answer on May 26, 2026 at 10:26 pm

    The value passed through to the $form_state array in your submit function will be the ID and not the name, it will be the key of the options in the drop down, not the value.

    If you want to shorten your code slightly you could use the fetchAllKeyed() method of the database query to build that array of categories automatically. You don’t need the category_options element as you’ll already have access to that array in the $form variable in your submission function.

    Also I’d be careful giving the outer and and inner elements the same name (category), that might cause some problems.

    This code should help:

    function mymodule_myform($form, &$form_state) {
      $categories = db_query("SELECT id, name FROM {am_category}")->fetchAllKeyed();
    
      $form['category_wrapper']['category'] = array(
        '#type' => 'select',
        '#title' => t('Category'),
        '#description' => t('Please select the category of this achievement.'),
        '#options' => $categories
      );
    
      return $form;
    }
    
    function mymodule_myform_submit(&$form, &$form_state) {
      $selected_category_id = $form_state['values']['category'];
    
      // Just in case you wanted to know, this would get the corresponding name for the selected category:
      $selected_category_name = $form['category_wrapper']['category']['#options'][$selected_category_id];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to Remove Badge from the tabbar item i had use below code but
I use below code to get the bitmap of all sd card photo. String[]
When I try to use the code below I get a duplicate variable error
The code below is what I am trying to use in order to get
Normally I use the below code, but is there a better way? lastOfMonth =
I am trying to use the code below to select all values of notableWork,
All the code below works. My device responds, C,7 is a reset. When I
I'm trying to get all the children of a div element (code below) in
First, I use below ode to get all image path. And save to string
I'm trying to use the code below to send messages via System.Net.Mail and am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.