how to assign a key to an array within another array?

0

Good I have this code in my controller (it's in mvc).

function actualizar_formato_valuaciones()
    {
         $id = $_POST['id'];
         $antes = $_POST['antes'];
       // echo $id;
        //print_r($id);
        print_r(implode(',',(array)$_POST['data']));

            $data = implode(',',(array)$_POST['data']);

            $data = str_replace("[","",$data);
            $data = str_replace("]","",$data);
            $data = str_replace("\"", "'", $data); 

            $comentarios = array(
                'form_obser_1' => $_POST['observaciones1'], 
                'form_obser_2' => $_POST['observaciones2'],
                'form_obser_3' => $_POST['observaciones3'],
                'form_obser_4' => $_POST['observaciones4'],
                'form_obser_5' => $_POST['observaciones5'],
                'form_obser_6' => $_POST['observaciones6'],
                'form_obser_7' => $_POST['observaciones7'],
                'form_obser_8' => $_POST['observaciones8'],
                'form_obser_9' => $_POST['observaciones9'],
                'form_obser_10' => $_POST['observaciones10'],
                'form_area_1' => $_POST['check1'],
                'form_area_2' => $_POST['check2'],
                'form_area_3' => $_POST['check3'],
                'form_area_4' => $_POST['check4'],
                'form_area_5' => $_POST['check5'],
                'form_area_6' => $_POST['check6'],
                'form_area_7' => $_POST['check7'],
                'form_area_8' => $_POST['check8'],
                'form_area_9' => $_POST['check9'],
                'form_area_10' => $_POST['check10'],
                'form_area_11' => $_POST['check11'],
                'form_area_12' => $_POST['check12'],
                'form_area_13' => $_POST['check13'],
                'form_area_14' => $_POST['check14'],
                'form_area_15' => $_POST['check15'],
                'form_area_16' => $_POST['check16'],
                'form_area_17' => $_POST['check17'],
                'form_area_18' => $_POST['check18'],
                'form_area_19' => $_POST['check19'],
                'form_area_20' => $_POST['check20'],
                'form_area_21' => $_POST['check21'],
                'form_area_22' => $_POST['check22'],
                'form_area_23' => $_POST['check23'],
                'form_area_24' => $_POST['check24']

        ); 

            $data2 = array_push($data,$comentarios);


            $updata = array(
            "fecha_cot_av" => date('Y-m-d'),
            "cliente_cot_av" => $_POST['clientec'],
            "tec_id_cot_av" => $_POST['tecnicoc'],
            "unidad_cot_av" => $_POST['unidadc'],
            "ven_id_cot_av" => $_POST['vendedorc'],
            "serie_cot_av" => $_POST['seriec'],
            "km_cot_av" => $_POST['kilometrajec'],
            "des_trab_cot_av1" => $_POST['descripcion1'],
            "des_trab_cot_av2" => $_POST['descripcion2'],
            "des_trab_cot_av3" => $_POST['descripcion3'],
            "des_trab_cot_av4" => $_POST['descripcion4'],
            "des_trab_cot_av5" => $_POST['descripcion5'],
            "des_trab_cot_av6" => $_POST['descripcion6'],
            "des_trab_cot_av7" => $_POST['descripcion7'],
            "precio_cot_av1" => $_POST['precio1'],
            "precio_cot_av2" => $_POST['precio2'],
            "precio_cot_av3" => $_POST['precio3'],
            "precio_cot_av4" => $_POST['precio4'],
            "precio_cot_av5" => $_POST['precio5'],
            "precio_cot_av6" => $_POST['precio6'],
            "precio_cot_av7" => $_POST['precio7'],
            "total_cot_av1" => $_POST['total1'],
            "total_cot_av2" => $_POST['total2'],
            "total_cot_av3" => $_POST['total3'],
            "total_cot_av4" => $_POST['total4'],
            "total_cot_av5" => $_POST['total5'],
            "total_cot_av6" => $_POST['total6'],
            "total_cot_av7" => $_POST['total7'],
            "sub_tot_cot_av" => $_POST['total'],
            "iva_cot_av" => $_POST['iva'],
            "total_neto_cot_av" => $_POST['totalNeto']
        );

        //$this->db->insert('cotizacion_avaluo_resultado',$insert);



            //print_r($data);
           $resultado = $this->valuaciones->actualizar_formato_valuaciones($data2,$updata,$id,$antes);

    }

my $data is an array with 120 records of 1 or 0, I save those 120 records in a table plus those in my other array $comentarios , my other array $updata is stored in another table.

My question is how can I assign the cell (key) to the array that comes in $data , this is to do an update, I already did it with an insert and it was easier, it was just

insert into 'tabla'('columna_1'....'columna_120')value(".$data.")

and it was everything but now in this case it is an update but I do not know how to do it or if I am wrong with something, I have corrected it but it marks 404 error Not Found

    
asked by Juan Jose 07.12.2018 в 02:17
source

1 answer

1

For what you comment, I think you do not need to do anything in the update regarding your other variables $ comments and $ updata. You only need to update $ data values, right?

It would be nice if you could show us how the $ _POST ['data'] variable comes from. For what you put in your comment, your variable comes like this:

  

['1', '0', '0', '1']

In that case you would have to remove the brackets (as you do in the code that you show us) and then make an explode with the delimiter "\":

$data = str_replace("[","",$data);
$data = str_replace("]","",$data);
$data = explode(",", $data);

The query for updates should have the following form:

UPDATE tabla SET columna_1 = valor_1, columna_2 = valor_2, ...;

I'm missing information about what your table is like (especially the names of the fields), but if it's how you describe it in the post, what I would do in your case would be to build a string in the following way (I assume that you will update all the fields, so you comment on the 120 values):

$query = 'UPDATE tabla SET ';
for($i=0;$i<120;$i++){
    $query .= 'columna_'.($i+1).' = '.$data[$i].',';
}
$query = substr($query, 0, -1); //quitas la última coma.

With what your variable $ query should have the form:

  

column_1 = value_1, column_2 = value_2, ... column_120 = value_120

And now you do the query (PDO, sequential, ... how do you do it):

mysqli_query($conexion, $query);

NOTE: I have written everything "bareback" (without trying anything). Maybe there is some silly mistake out there, but the idea is basically that.

NOTE2: what does not fit me very much (with the information I have), is that you are giving an error 404. That is page not found, which has nothing to do with any problem in the SQL statement or nothing like that. If you continue to give problems, you tell us.

I hope I have helped you.

    
answered by 07.12.2018 / 02:57
source