CodeIgniter Forums

Full Version: Update multiple rows in a table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]eironotics[/eluser]
please can anyone show me some codes, so that i can have an idea. thanks.

El Forum

[eluser]stuffradio[/eluser]
Code:
$data = array(
               'title' => $title,
               'name' => $name,
               'date' => $date
            );

$this->db->where('id', $id);
$this->db->update('mytable', $data);

// Produces:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id

http://ellislab.com/codeigniter/user-gui...tml#update

El Forum

[eluser]eironotics[/eluser]
i already know that sir.

what i mean is to update one or more row in a table.
and do this using the check box as a selector. pls give me a sample codes.

thanks

El Forum

[eluser]Mellis[/eluser]
Hi,

This is how I would do it:
In your view: You name your input checkboxes 'id_'.$id
In your controller: for each selected checkbox you explode the post key and do the update with the $exploded[1] value.

Greetings,

Mellis

El Forum

[eluser]eironotics[/eluser]
Hi mellis,

First of all thanks,to make it clearer to you heres my code.

please help me im completely new with CI.

Controller Contacts
Code:
class Contacts extends controller{
    
    function Contacts(){
        parent:: Controller();
        $this->load->database();
        
        $this->load->model('contacts_model');
        $this->load->helper('form');
        $this->load->scaffolding('contacts');
    ;
        }
    function index(){
        $data['title']="Displaying all Contacts";
        $data['query']=$this->contacts_model->poplist();
        $data['insert']=0;
        $this->load->view('contacts_view',$data);        
        }
    
    function popEditWindow(){
        //pass to array
        $arrayid= $this->input->post('edit');
        if(count($arrayid)){
            for($i=0;$i<=count($arrayid); $i++){
            //call model to compare
            $id= $arrayid[$i];
            echo $id;
            //call the view for display
            }
            }
        else{
            echo 'No selected';
            }
        
        }    
        
    function popInsertWindow(){
    
        $data['title']="Inserting New Contacts";
        $data['insert']=1;
        $this->load->view('contacts_view',$data);
        }
    function insertDB(){
            $data['query']= $this->contacts_model->insertlist();
            if($data['query']){
                redirect('contacts/index');
                    
                }
    
            
        }
    }

?&gt;

Views: Contacts_view: it is used for inserting and popping the data in the table
Code:
&lt;body&gt;
        
        <p align=center>&lt;?=$title ?&gt;</p>
        <hr align= "center" width="650px">
        

    &lt;?php
        
        if($insert==1){
            echo '<div align=center>';
            echo form_open('contacts/insertDB');
            echo  ' Name:'.form_input('name');
            echo  'Phone:'.form_input('phone');
            echo  'Email:'.form_input('email');
            echo  '&lt;input type= submit value=Save &gt;';
            echo  form_close();
            echo '<div>';
            }    
        else{
                
        ?&gt;
        &lt;?php
    ?&gt;
    <p align="center">
&lt;?php
             echo anchor('contacts/popInsertWindow','New ');

        ?&gt;
        <table border=0 align="center" width="650px">
        
        <tr bgcolor="#e3e3e3">
        <td></td>
        <td>ID</td>
        <td>Full Name</td>
        <td>Phone</td>
        <td>Email</td>
        </tr>
        &lt;?php
        echo form_open('contacts/popEditWindow');
        
        foreach ($query->result() as $row){
        echo '<tr><td>'.form_checkbox('edit[]',$row->id);
        echo '<td>'.$row->id.'</td><td>'.$row->name.'</td><td>'.$row->phone.
        '</td><td>'.$row->email.'</td></tr>';
        
            }
        
                
        ?&gt;
            <tr>
        <td></td>
        <td>
        </td>
        <td>
        &lt;?php
             echo form_submit('editbutton','Edit');
             echo form_close();

        ?&gt;
        </td>
        <td></td>
        <td></td></td>
        </tr>
    
        </table>
        &lt;?php
        }
        ?&gt;
Model: contacts_model

Code:
class contacts_model extends Model{
    function contacts_model(){
        parent::Model();
    }
    function poplist(){
        $this->db->orderby('id','DESC');
        $query=$this->db->get('contacts');
        return $query;
        }
    function insertlist(){
        $query = $this->db->insert('contacts',$_POST);
        return $query;
        }
        
    function compareID($id){
        $query = $this->db->get_where('contacts', array('id' => $id));
        return $query;
        }
}

the probleM:

how can i populate the data from the table and fill it in the form.
and how to update the table based from the specified ID number from that form
when the update button is submitted
help me.pls.

El Forum

[eluser]Mellis[/eluser]
Allright.
I'm going to suggest a way to make this work. This is just a suggestion. Anybody who has a better approach, is very welcome to correct me, I want to learn, but for now I would handle it like this.

I would update my model a bit:

I wouldn't hardcode the $_POST in insertlist()
Code:
function insertlist($values){
        $query = $this->db->insert('contacts',$_values);
        return $query;
        }

I would replace poplist() and compareID() with a function getcontacts or something simular
Code:
getcontacts($id = null)
{
  $this->db->orderby('id','DESC');
  //check for the id here (could be a single value or an array)
  //add the where clause if $id
  $return the result
  // this way you can use it to get all or some of the contacts
}


I would make a view to add / edit a single contact and a different view to edit multiple contacts.

In my controller i would have a method contact() and contacts()
something like this:
Code:
public function contact($id = null)
{

//set your validation rules
if($this->validation->run())
  {
  if($id){ // should contain check if its a real contact id & then you could update your contact information
  }elseif(!$id){ //insert your contact information}
  //if succesfull insert or update -> redirect or do whatever you want
}else{
// load your contact form here.
if($id) // get the contact information and pass it to the contact form as standard input (after submission form fields should use validation input as value.
}
}

btw i have an input helper (saw it on the forum around here somewhere) that checks if I have $this->validation->input_name. If I don't have that, it returns a standard input i passed throught in the controller

El Forum

[eluser]eironotics[/eluser]
ok i get your point mellis.

one more thing how can i get the values of an &lt;input&gt; tag?
is it?

Code:
$this->input->type('name')

what if it is an array?

i know im stressing you too much..

thanks, anyway.

El Forum

[eluser]Mellis[/eluser]
[quote author="eironotics" date="1219159962"]ok i get your point mellis.

one more thing how can i get the values of an &lt;input&gt; tag?
is it?

Code:
$this->input->type('name')

what if it is an array?

i know im stressing you too much..

thanks, anyway.[/quote]

I don't quite get what you are asking.
If you want to use $this->input->post('contact_name') just use it wherever you need it, or assign it to a variable. ($var = $this->input->post('name')Wink
if you explode your input: $contact_name = explode('_',$this->input->post('contact_name'));
you can use the result like this: $contact_name[1] //(or whatever part you might need)

btw you're not stressing me. You're only asking questions. If I have the time and a possible answer, I'm glad to try and help.

El Forum

[eluser]Mellis[/eluser]
Were you able to solve your problem?

El Forum

[eluser]eironotics[/eluser]
yeah thanks mellis.

I was able to store all the values in &lt;input&gt; tag in an array.
then i use a foreach loop to compare the id into the table.

thanks,