update data with php arrays

0

Interface I have the following parameters obtained from an interface that I create.

data_id=1,2,3,4,....,53

agente_id= 123,234

cantidad= 4

I want to update the rows according to the quantity and agente_id . For example: Of the values of data_id you would only use 4 ( data_id=1,2,3,4 ) and these would be distributed homogeneously to each agent (2 to each).

So the resulting action would be something like this:

UPDATE table_name SET agente_id=123 WHERE data_id=1
UPDATE table_name SET agente_id=123 WHERE data_id=2
UPDATE table_name SET agente_id=234 WHERE data_id=3
UPDATE table_name SET agente_id=234 WHERE data_id=4

I'm using php and ajax

Thank you, I hope you can give me a hand: (

    
asked by Franco Znz 25.07.2018 в 23:18
source

1 answer

0

You scroll the array ids, up to quantity or that there are no more ids choose the agent by making the index module (divided 2 so that each one has 2) by the number of agents

<?php

$ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$qty = 5;
$agents = [123, 124]; 
$al = count($agents);
$il = count($ids);
$sql = "";
for ($i = 0; $i < $qty && $i < $il; $i++) {
  $sql .= "UPDATE table_name SET agente_id=" . 
      $agents[$i/2 % $al] . " WHERE data_id=" . 
      $ids[$i] . ";".PHP_EOL;

}
var_dump($al,$il,$sql);

watch online: link

in js:

let ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    qty = 5,
    agents = [123, 124], 
    al = agents.length,
    il = ids.length;
let sql = "";
for (let i = 0; i < qty && i < il; i++) {
  sql = sql + "UPDATE table_name SET agente_id=" + 
     agents[Math.floor(i/2) % al ] + " WHERE data_id=" + 
     ids[i] + ";";
 
}
console.log(al,il,sql);
    
answered by 26.07.2018 / 01:44
source