CodeIgniter Forums
Issue with database record updating: updating record inside a foreach loop? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Issue with database record updating: updating record inside a foreach loop? (/thread-25542.html)

Pages: 1 2 3


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-15-2009

[eluser]Salvatore Formisano[/eluser]
Hey everone,

I'll try to be quick, I am trying to use a foreach loop to parse a $_POST coming from javascript to reorder a few elements in a database table.

Here's the code:
Code:
function reorder_photos()
{
     foreach($_POST as $key=>$value){
          $data['position']=$value;
          $this->m__crud->update_record("photos",$key,$data);
     }
}

the update_record method from the m__crud model it quite straightforward:
Code:
function update_record($table,$id,$data)
    {
        $this->db->where('id',$id);
        $this->db->update($table,$data);
    }

so basically, I have a string like this one 354=1&564=2&999=3 (photo with id of 354=position 1 & photo with id of 564= position 2 & ...)

The string is then sent as $_POST the reorder_photos method.

The data is fine (I formed an array with php and printed it back as json through an alert).


also (just in case...) I tried this outside of the loop
Code:
$data['position']="1";
$this->m__crud->update_record("photos","354",$data);

and it worked, updating the record with an id of 354 setting the position to 1.


Is the record update not allowed inside a foreach?


Many thanks,
Salvatore


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-15-2009

[eluser]Salvatore Formisano[/eluser]
I changed the title to make it more clear, hoping a few more people will bump into it :|


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]Salvatore Formisano[/eluser]
wow... 34 views and no replies... am I trying to do something epic?

... or is my mistake so obvious that nobody has bothered?


Big Grin


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]saidai jagan[/eluser]
hai u can directly giv like this ?
Code:
$this->m__crud->update_record("photos",$key,$value);



Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]LuckyFella73[/eluser]
To call a method within a loop shouldn't be a problem.

Maybe you have to check the loop for errors. Here is an
example from an other thread:

Code:
foreach($_POST as $key => $value) {
  $value = $this->input->post($key);
}
Maybe your code should look like this:
Code:
foreach($_POST as $key => $value) {
          $position = $this->input->post('position');
          $this->m__crud->update_record("photos",'position',$position);
     }
I'm not very good with arrays (btw. looking at my code it does not to be
the cleanest solution ...) and didn't test this, but I think the problem lies
in this lines ...


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]Salvatore Formisano[/eluser]
Thanks for replying mate,

the thing is, I tried filling an array with the data inside the foreach, and I then printed it. All the values were there... weird huh?

I also tried to look for other threads on the subject, no luck though.

Hopefully someone a bit more php/ci savvy than I am will help me through this Smile


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]Salvatore Formisano[/eluser]
[quote author="saidai jagan" date="1260983116"]hai u can directly giv like this ?
Code:
$this->m__crud->update_record("photos",$key,$value);
[/quote]

Hi there,

yes I can, as you can see I am referencing a model, here's the update_record method of that model:

Code:
function update_record($table,$id,$data)
{
$this->db->where('id',$id);
$this->db->update($table,$data);
}



Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]saidai jagan[/eluser]
Then now it is working right ?


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]Salvatore Formisano[/eluser]
No it is not because a wrong assumption was made:

as I wrote in the first post, I have a javascript which creates a query string like this one 354=1&564=2&999=3 (photo with id of 354=position 1 & photo with id of 564= position 2 & ...)

there isn't any 'position' input. The key is the photo's id, and the value is the photo's position.

In this scenario the following code
Code:
$position = $this->input->post('position');

doesn't make sense.


I created an array with php and sent it back as json and when I do that it works. But the database update doesn't happen... I'm really going nuts after this thing! :|


Issue with database record updating: updating record inside a foreach loop? - El Forum - 12-16-2009

[eluser]saidai jagan[/eluser]
Can u pls show the print_r($_post) ?