Note that there are some explanatory texts on larger screens.

plurals
  1. POdrupal table with the edit link
    text
    copied!<p>I have a table in drupal which displays all the content from a table. I have added an edit link to each record . This link should take the user to the input form which has the values populated in it corresponding to the record. RIght now it is just populating the form with the last row. </p> <p>For a row x, i need the form populated with the values for record x. </p> <p>The table is created as </p> <pre><code>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['p1']; $comment = $row['p2']; } // 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 =&gt; $data) { // create row data $row = array( 'edit' =&gt; l(t('Edit'),"admin/content/test/$p1/$p2/Table1", $options=array()),); // loop through column names foreach ($columnNames as $c) { $row[] = array( 'data' =&gt; $data[$c], 'class' =&gt; 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' =&gt; $c, 'class' =&gt; 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; } function display(){ $results = array(); $html = ""; $resource = db_query("select * from contactus"); $output = ''; while($row = db_fetch_array($resource)){ $results[] = $row; } if(!count($results)){ $html.= "Unable to display table"; return $html; } $rows = array(); $columnNames = array_keys($results[0]); foreach($results as $key=&gt;$data){ $row = array(); foreach($columnNames as $c){ $row = array( 'data' =&gt; $data[$c], 'class' =&gt; strtolower(str_replace(' ', '-', $c)), ); } $rows[] = $row; } $header = array(); foreach($columnNames as $c){ $header[] = array( 'data' =&gt; $c, 'class' =&gt; strtolower(str_replace(' ', '-', $c)), ); } $html .= theme('table', $header, $rows); return $html; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload