CodeIgniter Forums

Full Version: excuse my ignorance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]RobertB.[/eluser]
Hello guys,
Trying to update multiple rows in a table. I also need to validate the field.

table
id | price

I'm sending this array from the view to the controller

Code:
//form field
<input id="1" type="text" value="24.95" name="price[1]">

//Get me this array

Array
(
  [1] => 24.94
  [2] => 24.94
  [7] => 24.94
  [8] => 24.94
  [9] => 24.94
)

The js file
Code:
$('#submit').live('click', function(){
     $.ajax({
            url: "selector/editPrices",
            type: "POST",
            dataType: "html",
            data: $('#content :input').serialize(),
            beforeSend: function(){
                //showLoader();
            },
              success: function(data) {
                 alert(data);                                  
                //processed(html);
             }
        });
    });

The Controller
Code:
function editPrices()
    {
        $price = $this->input->post('price');
        print_r($price);
    }

I need to update the table after validation

Thanks in advanced

El Forum

[eluser]JHackamack[/eluser]
After you have done validation you can use:
Code:
foreach($price as $key=>$value) {
$update = array('price_column'=>$value);
$this->db->where('column_id',$key);
$this->db->update('table_name',$update);
}

El Forum

[eluser]RobertB.[/eluser]
Thank you

The update part is working perfectly, the validation part i'm having problems

I'm getting this message 4 times because the array has 4 items
Code:
<p>The Price field is required.</p>
<p>The Price field is required.</p>
<p>The Price field is required.</p>
<p>The Price field is required.</p>


This is the code
Code:
function editPrices()
    {
        $this->load->library('form_validation');
        $price = $this->input->post('price');

        foreach ($price as $key => $value) {
            $this->form_validation->set_rules('price['.$value.']', 'Price', 'trim|required|integer|xss_clean');
        }
        if ($this->form_validation->run() == false) {
            echo validation_errors();
        } else {
            //$price = $this->input->post('price');
            $this->selector->editPrice($price);
        }
    }

If I take the required part of the validation out everything works. Any ideas?

Thanks again.

El Forum

[eluser]JHackamack[/eluser]
One thing to try is change :
Code:
foreach ($price as $key => $value) {
            $this->form_validation->set_rules('price['.$value.']', 'Price', 'trim|required|integer|xss_clean');
        }

to
Code:
foreach ($price as $key => $value) {
            $this->form_validation->set_rules('price['.$key.']', 'Price', 'trim|required|integer|xss_clean');
        }
because you're looking for 1,2,3,4.etc.. instead of 24.94, etc.

El Forum

[eluser]RobertB.[/eluser]
Thanks, that did it.