如何将数组形式的数据放在更新查询中,并从更新查询中删除数组到字符串的转换

时间:2018-09-18 10:40:15

标签: php arrays

在使用foreach循环时,其内部的foreach循环将运行两次以上示例:-假定 第一个foreach循环运行两次,第二个内部foreach循环运行一次,现在发生的情况是,第一个foreach循环运行两次,内部foreach也运行两次,由于这种辞职,我无法获取ID和编辑数据库中的数据。

这种形式的输入数组:-

Array ( [reps_value] => Array ( [0] => q1 [1] => q2 ) [reps_percent] => Array ( [0] => e1 [1] => e2 ) [id] => Array ( [0] => 1 [1] => 2 ) )

我在查询中得到空值

UPDATE dev_prescription2 SET `reps_percent`= '',`reps_value`= '' WHERE id= ''

在更新查询值中不显示,并且数组到字符串的转换有时显示

 public function prescriptionUpdateData($data) {
        //print_r("Print");
       print_r($data);
      foreach( $data as $data1){ 
   print_r( $reps_percent = $data1['reps_percent']);
   print_r($id = $data1['id']);
    print_r($reps_value = $data1['reps_value']);

     $result = $this->db->query("UPDATE dev_prescription2 SET `reps_percent`= '".$reps_percent."',`reps_value`= '".$reps_value."'
              WHERE 
               id= '".$id."'");

            }

            $insertid = $this->db->insert_id();
                if ($result > 0) {
                    return TRUE;
                } else {
                   return $this->db->_error_message();
                }

         }

我的html格式是:-

<?php foreach ($data['prescription_week1'] as $key=>$value){
        $location[]  = ($value['reps_value']); 
        $location[] =   ($value['reps_percent']);
        $location[] =   ($value['id']);
         }  
//        print_r($location);
         ?>  


i am using array in html format
<td align="center" valign="middle" bgcolor="#FFFFFF"><font face="Agency-Roman" color="#000000"><br></font></td>
                <td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;"><font face="Agency-Roman" color="#000000"><input style="width:90%" type="text"  name="reps_value[]" id="reps_value" value="<?php echo $location[0] ?>" class="languages_list" /></font></td>
        <td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%"><font face="Agency-Roman" size="1" color="#FF0000"><input style="width:90%" type="text"  name="reps_percent[]" id="reps_value" value="<?php echo $location[1] ?>" /><input style="width:90%" type="hidden"  name="id[]" id="id"  value="<?php echo $location[2] ?>"/></font></td>
        <td align="left" valign="bottom" bgcolor="#FFFFFF"><font color="#000000"><br></font></td>
        <td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;"><font face="Agency-Roman" color="#000000"><input style="width:90%" type="text" name="reps_value[]" id="reps_value" value="<?php echo $location[3] ?>"/></font></td>
        <td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%"><font face="Agency-Roman" size="1" color="#FF0000"><input style="width:90%" type="text"  name="reps_percent[]" id="reps_value" value="<?php echo $location[4] ?>" /><input style="width:90%" type="hidden"  name="id[]" id="id" value="<?php echo $location[5] ?>"/></font></td>
        <td align="left" valign="bottom" bgcolor="#FFFFFF"><font color="#000000"><br></font></td> 

我的数据库表是:- enter image description here

this is my controller part:-
public function prescriptionUpdate() {

        $response = $response = $this->Godspeed->prescriptionUpdateData($_POST);

            }

1 个答案:

答案 0 :(得分:0)

嘿,开发人员,这是您的问题和解决方案:

您的数组是这个

Array ( 
    [reps_value] => Array ( 
                        [0] => q1 
                        [1] => q2 
                   ) 
    [reps_percent] => Array ( 
                          [0] => e1 
                          [1] => e2 
                      ) 
    [id] => Array ( 
                [0] => 1 
                [1] => 2 ) 
)

正如您在此处看到的,$data是具有三个键的数组,其中三个键包含其中的数组。

所以您的循环应如下所示:

foreach ($data as $key => $value) {
    if($key == 'reps_value'){
       // Do something with reps_value arrey
    }

    if($key == 'reps_percent'){
       // Do something with reps_percent array
    }

    if($key == 'id'){
        $idArray = $value;
        // Here you can loop again over the ids
        // your current $id is blank because its an array and not an integer
    }
}

您可能想创建函数来破坏foreach循环,以免3个if语句变得太复杂。

例如:

您可以为每个键创建功能

foreach ($data as $key => $value) {
        if($key == 'reps_value'){
           reps_value($value);
        }

        if($key == 'reps_percent'){
           reps_percent($value);
        }

        if($key == 'id'){
            ids($value);
        }
    }

public function reps_value($array){
  // Loop over the array and do your functionality
}

public function reps_percent($array){
  // Loop over the array and do your functionality
}

public function ids($array){
  // Loop over the array and do your functionality
}

让我知道它的工作原理,或者您对我提出的解决方案有任何疑问。

项目进展顺利,欢迎您使用StackOverflow

每个OP请求的更新

这是功能相同的解决方案,就像您拥有的一样:

public function prescriptionUpdateData($data) {

$reps_values = null;
$reps_percents = null;
$ids = null;

foreach( $data as $key => $values){ 

        if($key == 'reps_value'){
           $reps_values = $values;
        }

        if($key == 'reps_percent'){
           $reps_percents = $values;
        }

        if($key == 'id'){
            $ids = $values;
        }

}

$arrayLength = count($ids);

if($arrayLength == $reps_values && $arrayLength == $reps_percents){

    for($i = 0; $i < $arrayLength; $i++){
        $result = $this->db->query("UPDATE dev_prescription2 SET `reps_percent`= '".$reps_percents[$i]."',`reps_value`= '".$reps_values[$i]."' WHERE id= '".$ids[$i]."'");

        if ($result > 0) {
            return TRUE;
        } else {
            return $this->db->_error_message();
        }

    }

}else{
 // Missmatched arrays
}

// $insertid = $this->db->insert_id(); this line is not required as you are doing the update


}