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

[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
Code:
function reorder_photos()
{
    foreach($_POST as $key=>$value){
        $data['position']=$this->input->post($key);
        $this->m__crud->update_record("photos",$key,$data);
    }
}

the reorder_photos js function, triggered every time the user drags and drops photos from the list
Code:
function reorder_photos(){
    var form_data="";
    $(".reorder .gallery").each(function(pos,el){
        p_id=$(this).attr("id").substring(1,100);
        p_pos=pos+1;
        form_data=form_data+"&"+p_id+"="+p_pos;
    });
    form_data=form_data.substring(1,100);
    $.post('<?php echo site_url('admin/photos/reorder_photos');?>',form_data,function(res){
        alert("done")
    });
}

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!
#12

[eluser]saidai jagan[/eluser]
Yeah thats fine,
For ex u r datas are like 354=1&564=2&999=3
First
Code:
$result = 354=1&564=2&999=3;
$split_arr = explode('&',$result);
$count = count($split_arr);
for($i=0;$i<=$count;$i++)
{
  $explode = explode('=',$split_arr[$i]);
  $this->m__crud->update_record("photos",$explode[0],$explode[1]);
}
This is untested, Hope it helps u
#13

[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
#14

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

[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).

this
Code:
$.post('&lt;?php echo site_url('admin/photos/reorder_photos');?&gt;',form_data,function(res){
        alert("done")
    });

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

Code:
&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;
&lt;/form&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:

Code:
&lt;form action="admin/photos/reorder_photos"&gt;
&lt;input type="text" id="string" name="string" value="354=1&564=2&999=3" /&gt;
&lt;/form&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
#16

[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
#17

[eluser]papanyaraka[/eluser]
[quote author="Salvatore Formisano" date="1260983567"][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);
}
[/quote]

I Have the same problem with yours...
here's my code
Code:
foreach($stock_list as $key=>$value):
   $hpp=round($stock_list[$key]['hpp_cur'],2);    
   $array_hpp_part=array(
    'bidckdcoy'=>$info['criteria_coy'],
    'bidckdcab'=>$info['criteria_cab'],
    'bidcnotranp'=>$stock_list[$key]['notranp'],
    'bidckdtrn'=>$stock_list[$key]['kodetranp'],    
    'bidcpartno'=>$stock_list[$key]'no_part']                            
    );
     $this->SvcBilling_model->updateSvcBillingDetailHPP($array_hpp_part,$hpp);
endforeach;
Model :
Code:
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']);
        $this->db->set('bidnhpp',$bidnhpp);
        $this->db->update('dtsvbilld');
      }

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.
#18

[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! :|
#19

[eluser]stuffradio[/eluser]
Is the db producing an error when you run the query?
#20

[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