table in drupal with edit link

Posted by user544079 on Stack Overflow See other posts from Stack Overflow or by user544079
Published on 2010-12-21T01:22:43Z Indexed on 2010/12/29 11:53 UTC
Read the original article Hit count: 122

Filed under:

I have a table created in drupal with the edit link pointing to the input form. But the problem is, it only displays the last row values in the $email and $comment variables. Can anyone suggest how to modify the table display to have the edit link to the corresponding records?

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;
  $email = $row['email'];
  $comment = $row['comment'];
  drupal_set_message('Email: '.$email. ' comment: '.$comment);
 }


 // 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/$email/$comment/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 {contactus}";

 $html .= _MYMODULE_sql_to_table($sql);

 return $html;
 }

© Stack Overflow or respective owner

Related posts about drupal