how to fetch data from database in drupal7 in table form - drupal-7

how to fetch data from database in drupal7:
fields i have name:
subject:
email:
message:
give me the code for drupal 7 i want it in table form.
my insert code is this:
function form_example_form_submit($form, &$form_state) {
echo $name = $form_state['values']['textfield'];
echo $email = $form_state['values']['mail'];
echo $subject = $form_state['values']['subject'];
echo $message = $form_state['values']['message'];
echo $ip=ip_address();
echo $cb=$name;
//echo $timestamp = REQUEST_TIME;
echo $time=time();
$nid=db_insert('form') // Table name no longer needs {}
->fields(array(
'name' => $name,
'email' => $email,
'subject' => $subject,
'message' => $message,
'ip' => $ip,
'created_by' => $cb,
//'created_at' => $time,
))
->execute();
//print_r($nid);
drupal_set_message(t('The form has been submitted.'));
}
how can i fetch the data from database in drupal 7 in the form of table ,
give me the code .i m newbie in drupal 7 so its very difficult forme

Here is the snippet to fetch all content from "form" table with table format and pager. Also you can add condition which I have added in comment if required.
<?php
// Set header
$header = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('Email'), 'field' => 'email'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Message'), 'field' => 'message'),
);
//query to fetch all content
$query = db_select('form', 'f');
$query->fields('f');
//$query->condition('f.name', $search_name, '=') //if needed
$table_sort = $query->extend('TableSort') // Table sort extender
->orderByHeader($header); // Order by headers
$pager = $table_sort->extend('PagerDefault')
->limit(20); // Set page limit
$arr_result = $pager->execute();
$rows = array();
foreach($arr_result as $result) {
$rows[] = array(
$result->name,
$result->email,
$result->subject,
$result->message,
);
}
// Set empty output
$output = '';
if (!empty($rows)) {
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
/*'attributes' => array(
'id' => 'sort-table' // add if want to add sorting
) */
));
$output .= theme('pager');
}
else {
$output .= t("No results found.");
}
return $output;
?>
Let me know if any query/confusion occurs for the same.

Related

Wordpress Loop Tax Query with multiple terms

I have a taxonomy called "fachbereiche". First I load the taxonomies of the current page:
<?php $term_list = wp_get_post_terms($post->ID, 'fachbereiche', array("fields" => "all", 'parent' => '0'));
foreach($term_list as $thisslug)
{
$output = $thisslug->slug;
echo $output;
?>
The current page has the taxonomy slugs: "bauelemente" and "baumarkt". The echo $output returns bauelementebaumarkt.
Now I want to find all posts of a custom post type "marken" with the same taxonomies as we got above ("bauelemente" and "baumarkt"), so I load the following query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'marken',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=> 'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'fachbereiche',
'field' => 'slug',
'terms' => array($output)
),
),
)
);
}
?>
The query returns only the posts with the taxonomy for "baumarkt". I think because the variable $output returns bauelementebaumarkt. I think that you have to seperate "bauelemente" and "baumarkt". Please have in mind that there can be more than 2 terms or just 1.
Your $output should be an array instead of string, so add this before foreach:
$output = array();
Then inside foreach you should do this:
$output[] = $thisslug->slug;
And finally in tax_query it should be like this:
'terms' => $output,

Updating and deleting data in D7

I have made a form in D7 using form API, registered some users and retrieved their registered data from database;now what i want is to add an edit and a delete link in front of every row in the retrieved table;i have done that in PHP but how to implement same in Drupal?;anybody having any idea how to do that?
My retrieval code is:
function form_data_menu() {
$items['formdata'] = array(
'title' => 'Form Data',
'page callback' => 'form_data_form',
'access callback' => TRUE,
);
return $items;
}
function form_data_form()
{
$results = db_query('SELECT * FROM {drupal}');
$header = array(t('Id'),t('Name'),t('College'),t('Education'),t('Percentage'),t('Application'));
$rows = array();
foreach($results as $result) {
$rows[] = array(
$result->id,
$result->name,
$result->college,
$result->education,
$result->percentage,
$result->application,
);
}
return theme('table',array('header'=>$header,'rows'=>$rows));
}
I can add links though but the main problem is how to run update and delete queries in D7?
Any help will be appreciated. Thank you!
Start with the example module "dbtng_example.module".
https://www.drupal.org/project/examples
This will give an example of how to set up a module to save to the database. I used this recently to create a custom module to save data to a new database table within drupal. To figure out how to set up the hook_schema, you can look at ".install" files within the drupal core and contributed codebase.
Then to create an edit or delete link, you need to alter your hook_menu to pass in the unique id for that element:
$items['admin/structure/nates-custom-page/update/%'] = array(
'title' => 'Update entry',
'page callback' => 'drupal_get_form',
'page arguments' => array('nates_module_form_update', 5),
'weight' => -5,
'access arguments' => array('administer DFP'),
);
$items['admin/structure/nates-custom-page/delete/%'] = array(
'title' => 'Delete entry',
'page callback' => 'drupal_get_form',
'page arguments' => array('nates_module_form_delete', 5),
'access arguments' => array('administer DFP'),
);
Above is an example where I added the argument (indicated by the percentage sign in the array key, ("%"), that will represent the unique id for that item I want to edit or delete.
Then alter the edit form to load the item you want to edit by the id.
And add a delete form.
Here's an example of a delete form:
function nates_module_form_delete($form, &$form_state) {
$entry = nates_module_entry_load_by_pid(arg(5));
$form = array();
$form['pid'] = array(
'#type' => 'value',
'#value' => arg(5),
);
$output = "";
foreach($entry as $key => $value) {
$output .= "<p><strong>$key</strong>: $value</p>";
}
$form['markup'] = array(
'#type' => 'markup',
'#markup' => $output,
);
return confirm_form(
$form,
t('Are you sure you want to delete this item? '),
'example/list',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
return $form;
}
Notice I return a confirmation form first.
And here's the submit handler function that processes it after they submit the request:
function nates_module_form_delete_submit($form, &$form_state) {
global $user;
nates_module_entry_delete($form_state['values']['pid']);
drupal_set_message(t("Deleted entry"));
}
Then, edit your listing page to add the edit links and delete links:
function nates_module_list() {
$output = '';
// Get all entries in the dfp_amobee2 table.
if ($entries = nates_module_entry_load_all()) {
$rows = array();
foreach ($entries as $entry) {
// Sanitize the data before handing it off to the theme layer.
$entry->editlink = l('edit','admin/structure/nates-custom-page/update/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));
$entry->deletelink = l('delete','admin/structure/nates-custom-page/delete/' . $entry->pid, array('query' => array('destination' => 'admin/structure/nates-custom-page')));
unset($entry->pid);
if(!empty($entry->tid)) {
$entry->alias = l($entry->alias, 'taxonomy/term/'.$entry->tid);
}
else if(isset($entry->pages)) {
if(drupal_valid_path($entry->pages)) {
$entry->alias = l(drupal_lookup_path('alias', $entry->pages),$entry->pages);
} else {
$entry->alias = check_markup($entry->pages);
}
} else {
$entry->alias = "";
}
unset($entry->pages);
if(!$entry->tid) {
$entry->tid = "";
}
$rows[] = (array) $entry;
}
// Make a table for them.
$header = array(t('path'), t('tid'), t('dfp tag machine_name'),t('amobee_as'), '', '');
$output .= '<p>' . t('If the "tid" is filled in, amobee_as will be used on taxonomy pages and on node pages (where the main term id matches). If the path is filled it, will be used on that path. Path supercedes "tid" when there is a conflict.') . '</p>';
$output .= theme('table', array('header' => $header, 'rows' => $rows));
}
else {
drupal_set_message(t('No entries have been added yet.'));
}
return $output;
}

Populating #options, #header for tableselect in ajax callback function

What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-
Notice: Undefined index: red in theme_tableselect() (line 3285 of
D:\wamp\www\drupal7\includes\form.inc).
where 'red' is the index of a dummy row value and #options don't get populated with the new values. What is the way to get this working ?
Here is the code for the form-
$form['mltag_new']['tag'] = array(
'#type' => 'button',
'#value' => t("Suggest Tags"),
'#ajax' => array(
'callback' => 'mltag_suggest_tags_ajax',
'wrapper' => 'mltag_suggest_tags_table_div',
'effect' => 'slide',
),
);
$options1 = array(); //initial dummy values
$options1['red']['tag'] = "A red row";
$options1['red']['chi'] = "A red row";
$form['mltag_new']['myselector'] = array (
'#type' => 'tableselect',
'#title' => 'My Selector',
'#header' => $header,
'#options' => $options1,
'#prefix' => '<div id="mltag_suggest_tags_table_div">',
'#suffix' => '</div>',
);
return $form;
and the Ajax callback looks something like this-
function mltag_suggest_tags_ajax($form, $form_state) {
//$content has some content
//pass the content to a function
include_once 'includes/content_tag.inc';
$tags = mltag_content_tag($content, variable_get('algo_type'), 20);
if (empty($tags)) {
$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
$form['mltag_new']['sample_text']['#markup'] = $output;
return $form['mltag_new']['sample_text'];
}
else {
$algo = variable_get('algo_type');
if ($algo == 1) {
$header = array(
'tag' => t('Tag'),
'frequency' => t('Frequency'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'frequency' => $value,
);
}
}
elseif ($algo == 2) {
$header = array(
'tag' => t('Tag'),
'chi' => t('Chi Square Value'),
);
$options = array();
foreach ($tags as $key => $value) {
$options[$key] = array(
'tag' => $key,
'chi' => $value,
);
}
}
$form['mltag_new']['myselector']['#header'] = $header;
$form['mltag_new']['myselector']['#options'] = $options;
return $form['mltag_new']['myselector'];
}
}
I replied to your post on Drupal.org about how I'm working on a somewhat similar problem. Try adding
$form['mltag_new']['myselector'] =
form_process_tableselect($form['mltag_new']['myselector']);
just before your return. Hopefully that helps you more than it did me. Beware that the #options just get rendered when the block reloads from the ajax, but the original $form object doesn't seem to be aware.
I know that this is a few years later, but I found this while searching for my own solution:
The tableselect module creates checkboxes in the $ form that have to be removed. in the example above, they would be in $form['mltag_new']['myselector'] with keys equal to the original $option1 in your original code. If you unset those, then call
$form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);
before your return, it will eliminate the dummy rows.

How does a form call itself on submit in Drupal

I am creating a custom module that I would like to use to display a form, from a module. The form queries a database and displays some information. The database connection is working, and the data has been retrieved successfully. The form has also been displayed successfully, but what I can figure out is how to link all together to have the page displayed again when submitting the form. My code is:
<?php
function menu($may_cache) {
$items = array();
$items['admin/reporting/report_details'] = array(
'title' => 'Report: User details by stores',
'access arguments' => array('access content'),
'page callback' => 'say_report_details',
'type' => MENU_CALLBACK,
);
return $items;
}
// This function should execute the logic if the $_GET variable is set
function say_report_details($values = array()) {
// The $_GET logic will be somtehing like this
// if (count($_GET) > 0)
// Get all the form values from the $_GET wit something like:
// if (count($_GET) > 0) {
// $start = mktime(0, 0, 0, $_GET['from_month'], $_GET['from_day'],
$_GET['from_year']);
// $end = mktime(23, 59, 59, $_GET['to_month'], $_GET['to_day'], $_GET['to_year']);
if ($_GET['store'] > 0) {
$form = drupal_get_form("report_details_form");
$output = theme("report_page", $form, $output);
return $output;
}
function report_details_form() {
$form["search"] = array(
'#type' => 'fieldset',
'#title' => t('Search params'),
'#collapsible' => FALSE,
'#tree' => TRUE,
);
for ($i = 1; $i < 32; $i++) {
$days_opt[$i] = $i;
}
for ($i = 1; $i < 13; $i++) {
$month_opt[$i] = $i;
}
for ($i = 2008; $i < date("Y") + 3; $i++) {
$year_opt[$i] = $i;
}
$form["search"]["from_day"] = array(
'#type' => 'select',
'#title' => 'Date from',
'#options' => $days_opt,
'#default_value' => (($_GET['from_day'] == "") ? date("d") : $_GET['from_day'])
);
$form["search"]["from_month"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $month_opt,
'#default_value' => (($_GET['from_month'] == "") ? date("m") : $_GET['from_month'])
);
$form["search"]["from_year"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $year_opt,
'#default_value' => (($_GET['from_year'] == "") ? date("Y") : $_GET['from_year'])
);
$form["search"]["to_day"] = array(
'#type' => 'select',
'#title' => 'Date to',
'#options' => $days_opt,
'#default_value' => (($_GET['to_day'] == "") ? date("d") : $_GET['to_day'])
);
$form["search"]["to_month"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $month_opt,
'#default_value' => (($_GET['to_month'] == "") ? date("m") : $_GET['to_month'])
);
$form["search"]["to_year"] = array(
'#type' => 'select',
'#title' => ' ',
'#options' => $year_opt,
'#default_value' => (($_GET['to_year'] == "") ? date("Y") : $_GET['to_year'])
);
$result = db_query('SELECT taxonomy_term_data.name, taxonomy_term_data.tid FROM
taxonomy_term_data WHERE vid = 10');
$strs = array("all" => "All");
foreach ($result as $store) {
$strs[$store->tid] = $store->name;
}
$form["search"]["store"] = array(
'#type' => 'select',
'#title' => 'Stores',
'#options' => $strs,
'#default_value' => $_GET['store']
);
$form["submit"] = array("#type" => "submit", "#value" => "Show report");
return $form;
}
function theme_report_page($form, $result = array()) {
$output = '
<div id="report_form">
'. $form .'
</div>
<div id="report_result">
'. $result .'
</div>
';
return $output;
}
function theme_report_details_form($form) {
unset($form['search']['from_day']['#title']);
unset($form['search']['from_month']['#title']);
unset($form['search']['from_year']['#title']);
unset($form['search']['to_day']['#title']);
unset($form['search']['to_month']['#title']);
unset($form['search']['to_year']['#title']);
unset($form['search']['store']['#title']);
$output = "<fieldset>
<legend>Search params</legend>
<div class='fieldtitles'>". t('Date From') .":</div>
". drupal_render($form['search']['from_day']) ."
". drupal_render($form['search']['from_month']) ."
". drupal_render($form['search']['from_year']) ."
<div class='clearing'> </div>
<div class='fieldtitles'>". t('Date To') .":</div>
". drupal_render($form['search']['to_day']) ."
". drupal_render($form['search']['to_month']) ."
". drupal_render($form['search']['to_year']) ."
<div class='clearing'> </div>
<div class='fieldtitles'>". t('Store') .":</div>
". drupal_render($form['search']['store']) ."
<div class='clearing'> </div>
". drupal_render($form['submit']) ."
</fieldset>";
unset($form['search']);
$output .= drupal_render($form);
return $output;
}
function report_details_form_submit($form_id, $form_values) {
$query = "";
// This next line is the one we are having an issue right now, as is trowing an
error due to $form_values is empty for some UNKNOWN reason...
foreach ($form_values['search'] as $key => $value) {
$query .= "&". $key ."=". $value;
}
return array('admin/reporting/report_details', $query);
}
I did not look over all the details of the form, but the submit function for D7 should look like this:
function my_module_example_form_submit($form, &$form_state) {
// do something
}
I think that should fix the problem. If not check out this link: http://api.drupal.org/api/drupal/includes%21form.inc/group/form_api/7
Please read & follow the answer mikewink provided.
In addition in the three working forms that I've created the menu item array is as follows:
$items = array();
$items['admin/config/system/dataforms/SiteAccessform']=array(
'title'=>'Site Access' //this should be watever the Title of your form is
,'description'=>'Configuration for the Data Forms module' //shows up by your menu item as it's description (where applicable)
,'page callback'=>'drupal_get_form' //this, as I understand it, is required for forms to work properly (& may be part of the cause of your problems)
,'page arguments'=>array('dataforms_main_form') //IMPORTANT this is the actual name of your function that will be executed to produce the form
,'access arguments'=>array('administer settings for dataforms') //??
,'type'=>MENU_CALLBACK//these work when the path is accessed though no menu items are created
,
);
A few more points, when you are on a particular 'menu' item with it's page arguments array element set to your form, it considers it ONE form only. That is to say that the one function will be the only function that will be able to create a form (in my experience). Therefore use logic in that function to produce different forms if applicable... ,(as opposed to attempting to have a button that calls another function... which did not work for me).
Also, again referring back to mikewink's answer, the $form_state variable is the method of storing state that you want to pay attention, & not the standard $_GET or $_POST variables.
This link might be mildly helpful: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
However I gleaned the most valuable information from the examples module: https://drupal.org/project/examples
Good Luck!

Print terms from a specific vocab at page.tpl drupal 7

I used to have a function to create a custom menu by loading all terms from a specific vocab at drupal 6:
function _taxonomy_top_links($vid = NULL) {
$terms = taxonomy_get_tree($vid);
$taxos = array();
foreach ($terms as $term) {
$taxos[] = array('title' => $term->name, 'taxonomy/term/' . $term->tid, 'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)));
}
return theme('links', $taxos, array('id' => 'menu-'. $vid, 'class' => 'menu clearfix'));
}
This doesn't work at drupal 7 which I guess related to the new field api. How do you grab all terms from a specific vocab to preprocess at page level?
Thanks for any help.
Most of your code should actually work fine, it's the theme part that is incorrect.
$terms = taxonomy_get_tree($vid, 0, NULL, TRUE);
$links = array();
foreach ($terms as $term) {
$uri = entity_uri('taxonomy_term', $term);
$link = array(
'title' => $term->name,
'href' => $uri['path'],
'attributes' => array('rel' => 'tag'),
);
$link += $uri['options'];
if (!empty($term->description)) {
$link['title'] = strip_tags($term->description);
}
$links['tid-' . $term->tid] = $link;
}
$variables = array(
'links' => $links,
'attributes' => array(
'id' => 'menu-' . $vid,
'class' => array('menu', 'clearfix'),
),
);
return theme('links', $variables);

Resources