i have table in drupal displays content table. have added edit link each record . link should take user input form has values populated in corresponding record. right populating form last row.
for row x, need form populated values record x.
the table created
function _mymodule_sql_to_table($sql) { $html = ""; // execute sql $resource = db_query($sql); // fetch database results in 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 found."; return $html; } // create array contain table rows $rows = array(); // list of column headers $columnnames = array_keys($results[0]); // loop through results , create table rows foreach ($results $key => $data) { // create row data $row = array( 'edit' => l(t('edit'),"admin/content/test/$p1/$p2/table1", $options=array()),); // loop through column names foreach ($columnnames $c) { $row[] = array( 'data' => $data[$c], 'class' => strtolower(str_replace(' ', '-', $c)), ); } // add row rows array $rows[] = $row; } // loop through column names , create headers $header = array(); foreach ($columnnames $c) { $header[] = array( 'data' => $c, 'class' => strtolower(str_replace(' ', '-', $c)), ); } // generate table html $html .= theme('table', $header, $rows); return $html; } // can call in code... function _mymodule_some_page_callback() { $html = ""; $sql = "select * {contactus}"; $html .= _mymodule_sql_to_table($sql); return $html; } function display(){ $results = array(); $html = ""; $resource = db_query("select * contactus"); $output = ''; while($row = db_fetch_array($resource)){ $results[] = $row; } if(!count($results)){ $html.= "unable display table"; return $html; } $rows = array(); $columnnames = array_keys($results[0]); foreach($results $key=>$data){ $row = array(); foreach($columnnames $c){ $row = array( 'data' => $data[$c], 'class' => strtolower(str_replace(' ', '-', $c)), ); } $rows[] = $row; } $header = array(); foreach($columnnames $c){ $header[] = array( 'data' => $c, 'class' => strtolower(str_replace(' ', '-', $c)), ); } $html .= theme('table', $header, $rows); return $html; }
you'll want have $rows = array();
appear before while
loop. you're doing destroying array , redeclaring empty array on each pass. why last row appearing you.
Comments
Post a Comment