title: How to select from a table and Edit the information in codeIgniter
body: <p>I am building an application with Codeigniter and in this application I am currently able to search through a database table and generate a table in the application displaying the results. The next step for me is to be able to select a row in the application search result table and redirect me to a form where I can edit the information. I wanted to know how I can do that. below I will give you the code that generates the table.  If more code or information is needed just let me know.</p>
  
  <pre><code>foreach ($query as $row){
      $this-&gt;table-&gt;add_row($row);
  
  }
  echo $this-&gt;table-&gt;generate();
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p><strong>Contoller</strong></p>
  
  <pre><code>public function search(){
      $this-&gt;load-&gt;model('reg_model');
  
      $search_term = array(
      'firstName' =&gt; $this-&gt;input-&gt;post('firstName'),
      'lastName' =&gt; $this-&gt;input-&gt;post('lastName'),
      'street' =&gt; $this-&gt;input-&gt;post('street'),
      'dob' =&gt; $this-&gt;input-&gt;post('dob')
          );
  
  
      $data['query'] = $this-&gt;reg_model-&gt;search_voters($search_term);
  
  
      $this-&gt;load-&gt;view("reg_header");
      $this-&gt;load-&gt;view("reg_nav");
      $this-&gt;load-&gt;view("reg_search", $data); 
  }
  
  
  public function add(){    
  
      $this-&gt;load-&gt;view("reg_header");
      $this-&gt;load-&gt;view("reg_nav");
      $this-&gt;load-&gt;view("reg_form"); 
  }
  
  public function send(){
  
      $this-&gt;load-&gt;library("form_validation");
  
      $this-&gt;form_validation-&gt;set_rules("firstName", "First Name", "required");
      $this-&gt;form_validation-&gt;set_rules("lastName", "Last Name", "required");
      $this-&gt;form_validation-&gt;set_rules("homeNum", "Home Number", "required");
      $this-&gt;form_validation-&gt;set_rules("street", "Street", "required");
      $this-&gt;form_validation-&gt;set_rules("zip", "Zip Code", "required");
      $this-&gt;form_validation-&gt;set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
      $this-&gt;form_validation-&gt;set_rules("district", "District", "required");
  
      //add to database
      if ($this-&gt;form_validation-&gt;run() == FALSE){
          $this-&gt;load-&gt;view("reg_header");
          $this-&gt;load-&gt;view("reg_nav");
          $this-&gt;load-&gt;view("reg_form");
  
      }
      else{
  
  
  
          $this-&gt;load-&gt;model('reg_model');
          $this-&gt;reg_model-&gt;add_voters();
  
  
          redirect(current_url());
  
  
      }
  
      }
      function edit_voterS($voterNum) {
      $voter = $this-&gt;reg_model-&gt;get_voter($voterNum);
  
      $this-&gt;data['title'] = 'Edit Voter';
  
      //validate form input
      $this-&gt;load-&gt;library("form_validation");
  
      $this-&gt;form_validation-&gt;set_rules("firstName", "First Name", "required");
      $this-&gt;form_validation-&gt;set_rules("lastName", "Last Name", "required");
      $this-&gt;form_validation-&gt;set_rules("homeNum", "Home Number", "required");
      $this-&gt;form_validation-&gt;set_rules("street", "Street", "required");
      $this-&gt;form_validation-&gt;set_rules("zip", "Zip Code", "required");
      $this-&gt;form_validation-&gt;set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
      $this-&gt;form_validation-&gt;set_rules("district", "District", "required");
  
      if (isset($_POST) &amp;&amp; !empty($_POST))
      {       
          $data = array(
              'firstName' =&gt; $this-&gt;input-&gt;post('firstName'),
              'lastName' =&gt; $this-&gt;input-&gt;post('lastName'),
              'midInitial' =&gt; $this-&gt;input-&gt;post('midInitial'),
              'homeNum' =&gt; $this-&gt;input-&gt;post('homeNum'),
              'street' =&gt; $this-&gt;input-&gt;post('street'),
              'apt' =&gt; $this-&gt;input-&gt;post('apt'),
              'zip' =&gt; $this-&gt;input-&gt;post('zip'),
              'dob' =&gt; $this-&gt;input-&gt;post('dob'),
              'district' =&gt; $this-&gt;input-&gt;post('district')
          );
  
          if ($this-&gt;form_validation-&gt;run() === true)
          {
              $this-&gt;reg_model-&gt;update_voter($voterNum, $data);
  
              $this-&gt;session-&gt;set_flashdata('message', "&lt;p&gt;voter updated successfully.&lt;/p&gt;");
  
              redirect(base_url().'reg/search/'.$voterNum);
          }           
      }
  
      $this-&gt;data['message'] = (validation_errors() ? validation_errors() : $this-&gt;session-&gt;flashdata('message'));
  
      $this-&gt;data['voter'] = $voter;
  
      //display the edit product form
      $this-&gt;data['firstName'] = array(
          'name'      =&gt; 'firstName',
          'id'        =&gt; 'firstName',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('firstName', $voter['firstName'])
      );
  
      $this-&gt;data['lastName'] = array(
          'name'      =&gt; 'lastName',
          'id'        =&gt; 'lastName',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('lastName', $voter['lastName'])
      );
  
      $this-&gt;data['midInitial'] = array(
          'name'      =&gt; 'midInitial',
          'id'        =&gt; 'midInitial',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('midInitial', $voter['firstName'])
          );
  
      $this-&gt;data['homeNum'] = array(
          'name'      =&gt; 'homeNum',
          'id'        =&gt; 'homeNum',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('homeNum', $voter['homeNum'])
          );
  
  
      $this-&gt;data['street'] = array(
          'name'      =&gt; 'street',
          'id'        =&gt; 'street',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('street', $voter['street'])
      );
  
      $this-&gt;data['apt'] = array(
          'name'      =&gt; 'apt',
          'id'        =&gt; 'apt',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('apt', $voter['apt'])
      );
  
      $this-&gt;data['zip'] = array(
          'name'      =&gt; 'zip',
          'id'        =&gt; 'zip',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('zip', $voter['zip'])
      );
  
      $this-&gt;data['dob'] = array(
          'name'      =&gt; 'dob',
          'id'        =&gt; 'dob',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('dob', $voter['dob'])
      );
  
      $this-&gt;data['district'] = array(
          'name'      =&gt; 'district',
          'id'        =&gt; 'district',
          'type'      =&gt; 'text',
          'value'     =&gt; $this-&gt;form_validation-&gt;set_value('district', $voter['district'])
      );
  
  
      $this-&gt;load-&gt;view('edit_form', $this-&gt;data);
  }    
      function delete_voter($voterNum) {
      $this-&gt;reg_model-&gt;del_voter($voterNum);
  
      $this-&gt;session-&gt;set_flashdata('message', '&lt;p&gt;Product were successfully deleted!&lt;/p&gt;');
  
      redirect('reg/search');
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public function search_voters($search_term){
     $this-&gt;db-&gt;select('*');
     $this-&gt;db-&gt;from('voterinfo');
     $this-&gt;db-&gt;like('firstName', $search_term['firstName']);
     $this-&gt;db-&gt;like('lastName', $search_term['lastName']);
     $this-&gt;db-&gt;like('street', $search_term['street']);
     $this-&gt;db-&gt;like('dob', $search_term['dob']);
     $query = $this-&gt;db-&gt;get();
     return $query-&gt;result_array(); }
  
  public function get_voter($voterNum) {
      $this-&gt;db-&gt;select('*');
      $this-&gt;db-&gt;where('voterNum', $voterNum);
      $query = $this-&gt;db-&gt;get('voterinfo');
  
      return $query-&gt;row_array();
  }
  
  public function update_voter($voterNum, $data)
  {
      $this-&gt;db-&gt;where('voterNum', $voterNum);
      $this-&gt;db-&gt;update('voter', $data);
  }
  
  public function del_voter($voterNum)
  {
      $this-&gt;db-&gt;where('voterNum', $voterNum);
      $this-&gt;db-&gt;delete('voter');
  }
  </code></pre>
  
  <p><strong>Search View:</strong></p>
  
  <pre><code>echo form_open("reg/search");
  
  echo form_label("First Name: ", "firstName");
  
  $data = array(
      "name" =&gt; "firstName",
      "id" =&gt; "firstName",
      "value" =&gt; set_value("firstName")
  );    
  echo form_input($data);
  
  echo form_label("Last Name: ", "lastName");
  
  $data = array(
      "name" =&gt; "lastName",
      "id" =&gt; "lastName",
      "value" =&gt; set_value("lastName")
  );    
  echo form_input($data);
  
  echo form_label("Street: ", "street");
  
  $data = array(
      "name" =&gt; "street",
      "id" =&gt; "street",
      "value" =&gt; set_value("street")
  );    
  echo form_input($data);
  
  echo form_label("Date of Birth: ", "dob");
  
  $data = array(
      "name" =&gt; "dob",
      "id" =&gt; "dob",
      "value" =&gt; set_value("dob")
  );    
  echo form_input($data);
  
  echo form_submit("searchSubmit", "Search");
  
  echo form_close();
  
  
  
  
  $this-&gt;table-&gt;set_heading(array('', 'Voter Number', 'First Name', 'Last Name',
                                  'Middle', 'Home #', 'Street',
                                  'Apt', 'Zip', 'DOB',
                                  'District', 'Edit'));
  
  foreach ($query as $row){
      $this-&gt;table-&gt;add_row($row);
  
  }
  echo $this-&gt;table-&gt;generate();
  </code></pre>
  
  <p><strong>edit form view:</strong></p>
  
  <pre><code>echo validation_errors();
  </code></pre>
  
  <p>echo form_open("reg/edit_voter");
  echo form_label("First Name: ", "firstName");</p>
  
  <pre><code>$data = array(
      "name" =&gt; "firstName",
      "id" =&gt; "firstName",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('firstName', $voter['firstName'])
  );    
  echo form_input($data);
  
  
  echo form_label("Last Name: ", "lastName");
  
  $data = array(
      "name" =&gt; "lastName",
      "id" =&gt; "lastName",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('lastName', $voter['lastName'])
  );    
  echo form_input($data);
  
  echo form_label("Middle Initial: ", "midInitial");
  
  $data = array(
      "name" =&gt; "midInitial",
      "id" =&gt; "midInitial",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('midInitial', $voter['midInitial'])
  );    
  echo form_input($data);
  
  echo form_label("Home Number: ", "homeNum");
  
  $data = array(
      "name" =&gt; "homeNum",
      "id" =&gt; "homeNum",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('homeNum', $voter['homeNum'])
  );    
  echo form_input($data);
  
  echo form_label("Street: ", "street");
  
  $data = array(
      "name" =&gt; "street",
      "id" =&gt; "street",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('street', $voter['street'])
  );    
  echo form_input($data);
  
  echo form_label("Apartment Number: ", "apt");
  
  $data = array(
      "name" =&gt; "apt",
      "id" =&gt; "apt",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('apt', $voter['apt'])
  );    
  echo form_input($data);
  
  echo form_label("Zip Code: ", "zip");
  
  $data = array(
      "name" =&gt; "zip",
      "id" =&gt; "zip",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('zip', $voter['zip'])
  );    
  echo form_input($data);
  
  echo form_label("Date of Birth: ", "dob");
  
  $data = array(
      "name" =&gt; "dob",
      "id" =&gt; "dob",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('dob', $voter['dob'])
  );    
  echo form_input($data);
  
  echo form_label("District: ", "district");
  
  $data = array(
      "name" =&gt; "district",
      "id" =&gt; "district",
      "value" =&gt; $this-&gt;form_validation-&gt;set_value('district', $voter['district'])
  );    
  echo form_input($data);
  
  
  echo form_submit("editSubmit", "edit");
  echo form_close();
  </code></pre>