2016-10-17 87 views
-1
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.')); 
    } 

如何從數據庫表的形式獲取在Drupal 7中的數據, 給我的代碼.IM新手在Drupal 7所以它非常困難印版如何從數據庫中drupal7以表格形式獲取數據

回答

0

以下是從表單格式和尋呼機的「表格」表中獲取所有內容的代碼片段。如果需要,你也可以添加我在評論中添加的條件。

<?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; 

?> 

讓我知道是否有任何查詢/混淆發生相同。

相關問題