首页 > 解决方案 > 如何将数组形式的数据放入更新查询中并从更新查询中删除数组到字符串的转换

问题描述

当使用 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> 

我的数据库表是:- 在此处输入图像描述

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

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

            }

标签: phparrays

解决方案


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

你的数组是这样的:

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

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

所以你的循环应该是这样的:

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


}

推荐阅读