Welcome Guest, Not a member yet? Register   Sign In
Issue with database record updating: updating record inside a foreach loop?

[eluser]Salvatore Formisano[/eluser]
not sure how I could get it for you mate, it's all js powered at the minute, meaning I have no landing page once the thing has finished. let me past some code though:

the reorder_photos method in the photos controller
function reorder_photos()
    foreach($_POST as $key=>$value){

the reorder_photos js function, triggered every time the user drags and drops photos from the list
function reorder_photos(){
    var form_data="";
    $(".reorder .gallery").each(function(pos,el){
    $.post('<?php echo site_url('admin/photos/reorder_photos');?>',form_data,function(res){

so basically, I have this amin panel with a list of draggable photos, and if the user wants to change the picture's order, all he has to do is literally grab them and move them (using jquery ui).

the reorder_photos js function will trigger and then send the form_data string to the reorder_photos method in the photos controller, which SHOULD then do his job.

the method is able to return a json containing all the data it received, but for some reason it can't put that same data in the database.

THAT is why I believe my error lies in the synthax, or in some bad concept. Who knows!

[eluser]saidai jagan[/eluser]
Yeah thats fine,
For ex u r datas are like 354=1&564=2&999=3
$result = 354=1&564=2&999=3;
$split_arr = explode('&',$result);
$count = count($split_arr);
  $explode = explode('=',$split_arr[$i]);
This is untested, Hope it helps u

[eluser]Salvatore Formisano[/eluser]
thanks, but how do I get the $result string from my setup?

The one I mentioned is not a string, they come as $_POST, so I have $_POST['354']=1,$_POST['564']=2 etc.

is it worth turning this into a string, then exploding it into an array, then looping through the array and updating the records?

couldn't there be a cleaner solution?

Thanks mate

[eluser]saidai jagan[/eluser]
Oh Oh u r result comes like this ?
$_POST['354']=1,$_POST['564']=2,$_POST['384']=3,$_POST['598']=4 etc..... ?
By comma separated ?

[eluser]Salvatore Formisano[/eluser]
no no no.. sorry, I realize I am not being very clear Smile

what my js does is basically the same action performed by a javascript input serializer (jquery serialize for instance).

$.post('&lt;?php echo site_url('admin/photos/reorder_photos');?&gt;',form_data,function(res){

sends the form_data string as if it was a full form with these fields:

&lt;form action="admin/photos/reorder_photos"&gt;

&lt;input type="text" id="354" name="354" value="1" /&gt;
&lt;input type="text" id="564" name="564" value="2" /&gt;
&lt;input type="text" id="384" name="384" value="3" /&gt;

this kind of context is emulated by the javascript, which you can see in the js I posted.

This is to say that my controller does NOT receive a single string like this:

&lt;form action="admin/photos/reorder_photos"&gt;
&lt;input type="text" id="string" name="string" value="354=1&564=2&999=3" /&gt;

therefore I don't have something like $_POST['string'] which contains "354=1&564=2&999=3"

I have $_POST['354'] which contains "1", $_POST['564'] which contains 2...

I hope I was a bit more clear, not sure though (very tired!)

but an handful of ids and values

[eluser]Salvatore Formisano[/eluser]
Actually, I just attached a firebug screenshot so I'm 100% you'll get what I tried to say in a reeeally confused way Smile

here it is

[quote author="Salvatore Formisano" date="1260983567"][quote author="saidai jagan" date="1260983116"]hai u can directly giv like this ?

Hi there,

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

function update_record($table,$id,$data)

I Have the same problem with yours...
here's my code
foreach($stock_list as $key=>$value):
Model :
function updateSvcBillingDetailHPP($SvcBillingInfo,$bidnhpp)
        $this->db->where('bidckdcoy', $SvcBillingInfo['bidckdcoy']);
        $this->db->where('bidckdcab', $SvcBillingInfo['bidckdcab']);
        $this->db->where('bidcnotranp', $SvcBillingInfo['bidcnotranp']);
        $this->db->where('bidckdtrn', $SvcBillingInfo['bidckdtrn']);
        $this->db->where('bidcpartno', $SvcBillingInfo['bidcpartno']);

It's work for several record then it's STOP without any error;
I've try to echo the last update query and run it directly on mysql and it works fine.

Can anyone Solve this problem. Thanks.

[eluser]Salvatore Formisano[/eluser]
Mmh.. could it be that we're simply trying to do something conceptually wrong? I mean, this might be the wrong approach and someone might end up showing a 1 line of code solution...

that'd be great! :|

Is the db producing an error when you run the query?

[eluser]Salvatore Formisano[/eluser]
errr. how do I check it?

I am completely new to CI so I don't know... the query did work with static content passed to the query (without $id and $position but with "564" and "1")

check the code I pasted, and if you need me to paste more code let me know.

thanks mate

Theme © iAndrew 2016 - Forum software by © MyBB