In a signup form that was created as a node, I'm asking people to select their favorite movie. For consistency, instead of giving them a free text field, I want them to search for the movie name from a table and then select the option from the results that are returned. What is the best way to handle this? There will be tens of thousands of records. Alternatively, is there any way to help them autocomplete the field or populate the drop down from the database. All the current solutions in modules can't seem to handle tens of thousands of records.

I would appreciate any guidance.

Thanks,

Vic

Comments

justsomeguy.com’s picture

Hi Vic:

Perhaps you might want to use the #autocomplete_path attribute of the textfield in question?

(please note that this code is typed directly into the comment, untested :)

Something like this:

$form['my_fav_movie'] = array(
'#type' => 'textfield',
'#title' => t('My favourite movie is'),
'#autocomplete_path' => 'mymodule/autocomplete/fav_movie'
);

Then you need to create an item in your hook_menu implementation bound to the path mymodule/autocomplete/fav_movie that returns a set of matches based on whatever they've typed in. You could "enforce" the match by ensuring that the user has submitted one of your autocompleted answers in the validation stage.

I generally code up the autocomplete callback to return nothing until I have two (or three, or four - depending on the application) characters. SELECT title FROM {movies} WHERE title like 'a%' is going to return a LOT of entries - but limiting it to a slightly longer string keeps the list more manageable.

Here's a link to the relevant piece of the forms api reference:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

I hope this has been helpful.

Cheers,

Steve

Vic96’s picture

Very helpful. I'll try this. Thank you, Steve!

justsomeguy.com’s picture

your callback should dump out something like this:

$results = array( 'foo' => 'Matching foo!', 'bar' => 'Matching Bar!', 'baz' => 'Matching baz!' );
print drupal_to_js( $results );

You are welcome - good luck!

Vic96’s picture

Here's the code I'm using but it doesn't seem to work.

$form['movie_name'] = array(
'#title' => t('movie Name'),
'#type' => 'textfield',
'#description' => t('Name of the movie.'),
'#maxlength' => 255,
'#required' => TRUE,
'#autocomplete_path' => 'movie/autocomplete'
);


function movie_menu($may_cache) {  
    $items[] = array(
    'path' => 'movie/autocomplete',
    'callback' => 'movie_autocomplete',
    'type' => MENU_CALLBACK,
    'access' => TRUE,
  );
  }

function movie_autocomplete($string = '') {
db_set_active('moviedb');
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT name FROM {movies} WHERE LOWER(name) LIKE LOWER('%%s%')", $string, 0, 10);
    while ($data = db_fetch_object($result)) {
      $matches[$data->name] = check_plain($data->name);
   }
  }
db_set_active('default');
  print drupal_to_js($matches);
  exit();
}

I'm trying to get it to offer movie options based on the few characters the person types. The user can then choose one as his/her favorite movie. I'd appreciate some guidance. Thanks!