【问题标题】:How to select from a table and Edit the information in codeIgniter如何从表中选择并编辑 codeIgniter 中的信息
【发布时间】:2013-12-18 07:22:15
【问题描述】:

我正在使用 Codeigniter 构建一个应用程序,在这个应用程序中,我目前能够搜索数据库表并在应用程序中生成一个显示结果的表。对我来说,下一步是能够在应用程序搜索结果表中选择一行并将我重定向到可以编辑信息的表单。我想知道我该怎么做。下面我会给你生成表格的代码。如果需要更多代码或信息,请告诉我。

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

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

更新

控制器

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');
}

型号:

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');
}

搜索视图:

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();

编辑表单视图:

echo validation_errors();

echo form_open("reg/edit_voter"); echo form_label("名字:", "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();

【问题讨论】:

    标签: php mysql forms codeigniter edit


    【解决方案1】:

    这需要两个步骤。

    • 第一步是从结果中加载现有数据。
    • 第二步是更新你编辑的数据

    对于第一站,您想为每个$row 创建一个链接/按钮,结果为id,让它以id 作为参数调用控制器中的函数(例如@987654325 @)。

    让该函数调用您正在处理结果的模型,然后发送id 以它为唯一参数:

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

    在模型中的 loadEdit_results 函数中,您只想获得 1 个 $row。与您要编辑的结果的id 对应的那个。 所以你在那个函数中写了这样的查询:

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

    这将得到一个带有对应id的结果。

    现在是进行下一步的时候了。更新部分:

    我猜你知道如何在视图中显示结果,所以我不深入讨论。 只需在表单中创建一个包含结果数据的表单,然后将该表单提交给控制器中的编辑函数(例如postEdit($id))(不要忘记,使用id作为函数的参数)

    postEdit($id) 函数必须调用您的模型,并将所有数据发送到那里:

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

    然后在你的模型中创建一个函数,用相应的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);
    }
    

    如果您以前从未做过类似的事情,这可能有点难以接受。所以这里有一些链接可以帮助你:

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

    希望对你有所帮助:)

    【讨论】:

    • 我仍然有问题。我刚刚更新了我的帖子并从我的 MVC 中添加了功能。
    • 通过这篇文章,我告诉了你如何做到这一点。研究它,并尝试一些事情。您现在还存在哪些问题?我的示例并不是要盲目复制/粘贴:P
    • 我现在的问题是能够从表格中实际选择选民以将我发送到编辑表单。
    • 您可以输出带有选民信息的表格,对吗?因此,您需要做的是向该表添加一个按钮/链接,该按钮/链接指向一个函数,并将投票者id 作为参数。类似:&lt;a href="/controller/editUser/$id"&gt;Edit voter&lt;/a&gt;。我希望你知道如何捕捉这些参数,并在函数中使用它们。然后在你的模型中创建一个使用id的函数,返回值,然后在你的视图中创建一个表单,其中已经填写了选民的值。然后你可以编辑这些字段,POST它,然后使用update 查询。希望这有点道理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2022-06-14
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多