1

I've a db_select to fetch data from sql table, and $output .= theme('table'...) to generate an html table to display these data in columns. My question is, how can I add edit and delete buttons to each row?

Also, given initially these data are stored in the sql table from dropdowns, how can I redirect to the link containing the dropdowns load again and if for example I edit a row which has data from formID=5, each dropdown then shows the corresponding data for this ID, and allow me to modify accordingly to save again to the same sql table, and update the html table's view?

Example of my current sql is:

db_select('mytable') ->fields('mytable', array('formID', 'field1', 'field2'))
->execute();

1
  • Keep in mind that questions should not be requests to write code. Plus, adding a link to a table row is not different from adding anything to a table row. If you have a specific misunderstanding, ask about that; don't ask a generic question hoping the answer will show you are correct or right.
    – avpaderno
    Commented Oct 11, 2015 at 9:23

1 Answer 1

1
    $query = db_select('mytable', 'n')
    ->fields('n')
    ->execute();
  $rows = array();
  while($data = $query->fetchAssoc()){
  // Fill the table rows
    $data['operation'] = l(t('Delete'), 'admin/mid-modules/component/' . $data['cid'] . '/delete');
    $data['operation'] .= "|";
    $data['operation'] .= l(t('Edit'), 'admin/mid-modules/component/' . $data['cid'] . '/edit');
    $rows[] = array('data' => $data, 'style' => 'word-break:break-all;',);
  }

  // Prepare table header
  $header = array( t('S.No'),t('Component Name'));
  // Output of table with the paging
  $output = theme_table(
    array(
      "header" => $header,
      "rows" => $rows,
      "attributes" => array(),
      "sticky" => true, // Table header will be sticky
      "caption" => "",
      "colgroups" => array(),
      "empty" => t("Table has no row!") // The message to be displayed if table is empty
    )
  );
  return $output;
//following menus that delete or edit your selected content

    $items['admin/mid-modules/component/%ctools_js'] = array(
    'page callback' => 'popup_callback',
    'page arguments' => array(3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'popup.inc'
  );
 $items['admin/mid-modules/component/%/delete'] = array(
    'title' => 'Delete Component item',
    'page callback' => 'delete_me',
    'page arguments' => array('delete_component_record_comfirm',3),
    'access arguments' => array('administer users'),
    'type' => MENU_CALLBACK,
    'file'=>'delete_record.inc',
  ); 

/**
 * Delete  Record Confirmation ------------------------------------
 */
function delete_component_record_comfirm($form, &$form_state, $cid){
   // print_r($cid);exit;
 $form['delete'] = array(
    '#type' => 'value',
    '#value' => $cid,
  );
  return confirm_form(
    $form,
    t('Are you sure you want to delete this?'),
    'admin/mid-modules/component/'. $data['cid'],
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}
/**
 * Delete Survey Record Function
 */
function delete_component_record_comfirm_submit($form, &$form_state) { 
  $record = $form_state['values']['delete'];
  if ($record ) {
    $num_deleted = db_delete('mid_components')
      ->condition('cid', $record )
      ->execute();
    drupal_set_message('The record has been deleted!');
  }
  $form_state['redirect'] = "admin/mid-modules/component";
}
1
  • Please vote this answer if it helps you Commented Jul 13, 2015 at 7:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.