Posted by roodani on January 19, 2013 at 9:31pm
Hello guys
i am a beginner in drupal and i am in the process of creating a database and i want to perform a certain queries to this db using let's say a search box in my site
so i just need guys a tuto or at least what do i have to do so i don't keep up studying unrelated stuff
thx in advance
Comments
With Drupal you do not create
With Drupal you do not create a database. Depending on the functional need, using a content type may be appropriate.
ok i am sorry may be my
ok i am sorry may be my question was a bit un-explaning
what i meant i need to know how can i make a connection when i press a button to show a certain querry of my database
Instead of trying to write
Instead of trying to write code, it is in my opinion better to learn what Drupal has to offer from core and contributed modules.
=-=
if you are trying to build a search, look at the core search module.
Things to lookup
Sounds to me like what you want is a form. To do this "The Drupal way", you want to look at the FAPI and the Database abstraction Layer.
references:
hook_menu
hook_block_info
hook_block_view
a very simple 'user search' module might look like this (without the closing ?> tag, see Developing modules for details)
## This is untested, so probably will need some adjustment to work and as is the form wont put anything to page. but designed as a point of reference more than anything
<?php
/**
* Implementation of hook_menu()
* a page to contain our form (also possible to build the form into a block etc if you want - see hook_block_info and hook_block_view)
*/
function user_search_menu() {
$items = array();
$items['usersearch/form'] = array( //the URL of the page
'title' => 'User Search Page',
'description' => 'Search for a user by email',
'page callback' => 'drupal_get_form', // callback is a form, so let drupal know this
'page arguments' => array('user_search_input_form'), // the function that builds the form
'access arguments' => array('access content'), // the user permission required to access the form
'type' => MENU_NORMAL_ITEM, // menu type, see hook_menu
);
return $items;
}
/**
* Build the custom form with its 1 input field and submit button
*/
function user_search_input_form(){
$weight = 0;
$default_value = '';
$form['user_search'] = array(
'#type' => 'fieldset',
'#title' => t('Search users by emails'),
'#collapsible' => TRUE,
'#weight' => $weight++,
);
$form['user_search']['email_address'] = array(
'#type' => 'textfield',
'#title' => t("email address of the user we're searching for"),
'#description' => t('email'),
'#weight' => $weight++,
);
$form['user_search']['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#weight' => $weight++,
);
$form['#submit'][] = 'user_search_input_form_submit'; // set the form submit handler
return $form; //return the form array
}
/**
* Form submit handler, takes the input passed in via the $form_state array and performs a query to get the user data
*/
function user_search_input_form_submit($form, &$form_state){
$email = check_plain($form_state['values']['email_address']); // grab the input email address, check for any nasties in the form submission and remove them.
$user = db_select('users', 'u')//select from the users table
->fields('u') // all fields (*)
->condition('mail', $email) // where mail = $email
->execute() // run the query
->fetchAssoc(); // return the result as an array
//do something with the output (i.e. out put it to a page, save to database and so on)
}
?>
The above is something to go on for a simple form, with a query to get data. it doesn't output anything to screen, but i'm sure that should be easy enough to work out :) - I haven't tested it so it might need a bit of adjustment
Shout if you need a hand
RTFM!
For further support or projects contact me.
Issue solved? Add [Solved] to the title