How to select from a table and Edit the information in codeIgniter

为君一笑 提交于 2020-01-06 07:00:33

问题


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.

foreach ($query as $row){
    $this->table->add_row($row);

}
echo $this->table->generate();

Update

Contoller

public function search(){
    $this->load->model('reg_model');

    $search_term = array(
    'firstName' => $this->input->post('firstName'),
    'lastName' => $this->input->post('lastName'),
    'street' => $this->input->post('street'),
    'dob' => $this->input->post('dob')
        );


    $data['query'] = $this->reg_model->search_voters($search_term);


    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_search", $data); 
}


public function add(){    

    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_form"); 
}

public function send(){

    $this->load->library("form_validation");

    $this->form_validation->set_rules("firstName", "First Name", "required");
    $this->form_validation->set_rules("lastName", "Last Name", "required");
    $this->form_validation->set_rules("homeNum", "Home Number", "required");
    $this->form_validation->set_rules("street", "Street", "required");
    $this->form_validation->set_rules("zip", "Zip Code", "required");
    $this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
    $this->form_validation->set_rules("district", "District", "required");

    //add to database
    if ($this->form_validation->run() == FALSE){
        $this->load->view("reg_header");
        $this->load->view("reg_nav");
        $this->load->view("reg_form");

    }
    else{



        $this->load->model('reg_model');
        $this->reg_model->add_voters();


        redirect(current_url());


    }

    }
    function edit_voterS($voterNum) {
    $voter = $this->reg_model->get_voter($voterNum);

    $this->data['title'] = 'Edit Voter';

    //validate form input
    $this->load->library("form_validation");

    $this->form_validation->set_rules("firstName", "First Name", "required");
    $this->form_validation->set_rules("lastName", "Last Name", "required");
    $this->form_validation->set_rules("homeNum", "Home Number", "required");
    $this->form_validation->set_rules("street", "Street", "required");
    $this->form_validation->set_rules("zip", "Zip Code", "required");
    $this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
    $this->form_validation->set_rules("district", "District", "required");

    if (isset($_POST) && !empty($_POST))
    {       
        $data = array(
            'firstName' => $this->input->post('firstName'),
            'lastName' => $this->input->post('lastName'),
            'midInitial' => $this->input->post('midInitial'),
            'homeNum' => $this->input->post('homeNum'),
            'street' => $this->input->post('street'),
            'apt' => $this->input->post('apt'),
            'zip' => $this->input->post('zip'),
            'dob' => $this->input->post('dob'),
            'district' => $this->input->post('district')
        );

        if ($this->form_validation->run() === true)
        {
            $this->reg_model->update_voter($voterNum, $data);

            $this->session->set_flashdata('message', "<p>voter updated successfully.</p>");

            redirect(base_url().'reg/search/'.$voterNum);
        }           
    }

    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data['voter'] = $voter;

    //display the edit product form
    $this->data['firstName'] = array(
        'name'      => 'firstName',
        'id'        => 'firstName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('firstName', $voter['firstName'])
    );

    $this->data['lastName'] = array(
        'name'      => 'lastName',
        'id'        => 'lastName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('lastName', $voter['lastName'])
    );

    $this->data['midInitial'] = array(
        'name'      => 'midInitial',
        'id'        => 'midInitial',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('midInitial', $voter['firstName'])
        );

    $this->data['homeNum'] = array(
        'name'      => 'homeNum',
        'id'        => 'homeNum',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('homeNum', $voter['homeNum'])
        );


    $this->data['street'] = array(
        'name'      => 'street',
        'id'        => 'street',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('street', $voter['street'])
    );

    $this->data['apt'] = array(
        'name'      => 'apt',
        'id'        => 'apt',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('apt', $voter['apt'])
    );

    $this->data['zip'] = array(
        'name'      => 'zip',
        'id'        => 'zip',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('zip', $voter['zip'])
    );

    $this->data['dob'] = array(
        'name'      => 'dob',
        'id'        => 'dob',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('dob', $voter['dob'])
    );

    $this->data['district'] = array(
        'name'      => 'district',
        'id'        => 'district',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('district', $voter['district'])
    );


    $this->load->view('edit_form', $this->data);
}    
    function delete_voter($voterNum) {
    $this->reg_model->del_voter($voterNum);

    $this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');

    redirect('reg/search');
}

Model:

public function search_voters($search_term){
   $this->db->select('*');
   $this->db->from('voterinfo');
   $this->db->like('firstName', $search_term['firstName']);
   $this->db->like('lastName', $search_term['lastName']);
   $this->db->like('street', $search_term['street']);
   $this->db->like('dob', $search_term['dob']);
   $query = $this->db->get();
   return $query->result_array(); }

public function get_voter($voterNum) {
    $this->db->select('*');
    $this->db->where('voterNum', $voterNum);
    $query = $this->db->get('voterinfo');

    return $query->row_array();
}

public function update_voter($voterNum, $data)
{
    $this->db->where('voterNum', $voterNum);
    $this->db->update('voter', $data);
}

public function del_voter($voterNum)
{
    $this->db->where('voterNum', $voterNum);
    $this->db->delete('voter');
}

Search View:

echo form_open("reg/search");

echo form_label("First Name: ", "firstName");

$data = array(
    "name" => "firstName",
    "id" => "firstName",
    "value" => set_value("firstName")
);    
echo form_input($data);

echo form_label("Last Name: ", "lastName");

$data = array(
    "name" => "lastName",
    "id" => "lastName",
    "value" => set_value("lastName")
);    
echo form_input($data);

echo form_label("Street: ", "street");

$data = array(
    "name" => "street",
    "id" => "street",
    "value" => set_value("street")
);    
echo form_input($data);

echo form_label("Date of Birth: ", "dob");

$data = array(
    "name" => "dob",
    "id" => "dob",
    "value" => set_value("dob")
);    
echo form_input($data);

echo form_submit("searchSubmit", "Search");

echo form_close();




$this->table->set_heading(array('', 'Voter Number', 'First Name', 'Last Name',
                                'Middle', 'Home #', 'Street',
                                'Apt', 'Zip', 'DOB',
                                'District', 'Edit'));

foreach ($query as $row){
    $this->table->add_row($row);

}
echo $this->table->generate();

edit form view:

echo validation_errors();

echo form_open("reg/edit_voter"); echo form_label("First Name: ", "firstName");

$data = array(
    "name" => "firstName",
    "id" => "firstName",
    "value" => $this->form_validation->set_value('firstName', $voter['firstName'])
);    
echo form_input($data);


echo form_label("Last Name: ", "lastName");

$data = array(
    "name" => "lastName",
    "id" => "lastName",
    "value" => $this->form_validation->set_value('lastName', $voter['lastName'])
);    
echo form_input($data);

echo form_label("Middle Initial: ", "midInitial");

$data = array(
    "name" => "midInitial",
    "id" => "midInitial",
    "value" => $this->form_validation->set_value('midInitial', $voter['midInitial'])
);    
echo form_input($data);

echo form_label("Home Number: ", "homeNum");

$data = array(
    "name" => "homeNum",
    "id" => "homeNum",
    "value" => $this->form_validation->set_value('homeNum', $voter['homeNum'])
);    
echo form_input($data);

echo form_label("Street: ", "street");

$data = array(
    "name" => "street",
    "id" => "street",
    "value" => $this->form_validation->set_value('street', $voter['street'])
);    
echo form_input($data);

echo form_label("Apartment Number: ", "apt");

$data = array(
    "name" => "apt",
    "id" => "apt",
    "value" => $this->form_validation->set_value('apt', $voter['apt'])
);    
echo form_input($data);

echo form_label("Zip Code: ", "zip");

$data = array(
    "name" => "zip",
    "id" => "zip",
    "value" => $this->form_validation->set_value('zip', $voter['zip'])
);    
echo form_input($data);

echo form_label("Date of Birth: ", "dob");

$data = array(
    "name" => "dob",
    "id" => "dob",
    "value" => $this->form_validation->set_value('dob', $voter['dob'])
);    
echo form_input($data);

echo form_label("District: ", "district");

$data = array(
    "name" => "district",
    "id" => "district",
    "value" => $this->form_validation->set_value('district', $voter['district'])
);    
echo form_input($data);


echo form_submit("editSubmit", "edit");
echo form_close();

回答1:


This requires two steps.

  • the first step is to load the existing data from the result.
  • the second step is to update the data you edited

For the first stop you want to create a link/button for each $row with the id of the result, let it call to a function in the controller with the id as parameter( for example loadEdit($id)).

let that function make a call to your model you are handling the results in, and send the id with it as its only parameter:

public function loadEdit($id){
    $this->load->model('model_results');
    $id = $id;
    $editData = $this->model_results->loadEdit_results($id);
}

in the loadEdit_results function in your model you only want to get 1 $row. The one corresponding with the id of the result you want to to edit. So you write something like this query in that function:

public function edit_page($id){
    $query = $this->db->get_where('pages', array('id' => $id));
}

This will get the one result with corresponding id.

Now it's time for the next step. The updating part:

I guess you know how to show the results in the view, so i don't go into that. Just create a form with the data of the result in a form, and let that form submit to the edit function (for exmaple postEdit($id)) in your controller (dont forget, use the id as the parameter for the function)

the postEdit($id) function will have to make a call to your model, and send all the data there:

public function edit_page_validation($id){
    $this->load->model('model_pages');          
    $this->model_pages->edit_page_update($id);
}

Then create a function in your model to update the result with the corresponding id.

public function edit_page_update($id){
$data = array(
    /*
    the array with the posted data from the form
    */
);

$query = $this->db->where('id', $id);
$query = $this->db->update('results', $data);
}

This might be a bit much to take in if you've never done something like this before. So here are some links to help you out:

http://ellislab.com/codeigniter/user-guide/database/active_record.html

Hopefully i helped you a bit :)



来源:https://stackoverflow.com/questions/20330770/how-to-select-from-a-table-and-edit-the-information-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!