2011-01-05 27 views
0

我需要創建一個帶有每個記錄的刪除鏈接的drupal表。我有一個編輯鏈接的現有表。他們使用hook_menu傳遞記錄的id並重定向到聯繫表單。我如何擴展這個功能來添加編輯。代碼是帶刪除鏈接的drupal表

function _MYMODULE_sql_to_table($sql) { 
    $html = "";  
    // execute sql 
    $resource = db_query($sql);  
    // fetch database results in an array 
    $results = array(); 
    while ($row = db_fetch_array($resource)) { 
    $results[] = $row; 
     $id = $row['id']; 
     $email = $row['email']; 
     $comment = $row['comment']; 
//  drupal_set_message('Email: '.$email. ' comment: '.$comment. ' id: '.$id); 
    } 
    // ensure results exist 
    if (!count($results)) { 
    $html .= "Sorry, no results could be found."; 
    return $html;  
    } 

    // create an array to contain all table rows 
    $rows = array(); 
    // get a list of column headers 
    $columnNames = array_keys($results[0]); 

    // loop through results and create table rows 
    foreach ($results as $key => $data) { 
    // create row data 
    $row = array(

     'edit' => l(t('Edit'),"admin/content/test/".$data['id']."/ContactUs", $options=array()), 
     'delete' => l(t('Delete'),"admin/content/".$data['id']."/ContactUs",$options = array()), 
     ); 

    // loop through column names 
    foreach ($columnNames as $c) { 
     $row[] = array(
     'data' => $data[$c], 
     'class' => strtolower(str_replace(' ', '-', $c)), 
    ); 
    } 

    // add row to rows array 
    $rows[] = $row; 

    } 

    // loop through column names and create headers 
    $header = array(); 
    foreach ($columnNames as $c) { 
    $header[] = array(
     'data' => $c, 
     'class' => strtolower(str_replace(' ', '-', $c)), 
    ); 
    } 

    // generate table html 
    $html .= theme('table', $header, $rows); 

    return $html; 

} 

// then you can call it in your code... 
function _MYMODULE_some_page_callback() { 

    $html = ""; 

    $sql = "select * from {contact3}"; 

    $html .= _MYMODULE_sql_to_table($sql); 

    return $html; 
} 

回答

0

我假設你已經有一個窗體來創建你的數據(不管它是什麼)。然後,您可以擴展該表單以選擇性地接收ID,如果有,則加載相應的數據,向表單中添加一個隱藏的(#type value)表單元素,其中包含id和相關的#default_value條目。然後,如果您有新條目(無ID)或現有條目,並且相應採取行動,則必須在您的提交回調中找出答案。

PS:我想你有沒有使用節點系統的原因,那麼這將已經存在。

PPS:你的代碼是完全不可讀的,你應該重新格式化。