How to deal with arrays with different number of indices in the same loop

Asked

Viewed 36 times

-2

I have a script that makes a registration

works like this: in a dynamic table, each row has an input that is checkbox type and it loads an id and another input is number type and loads any value

Hence it occurs that $_POST gets arrays with distinct numbers of indices. for example, if in this table I mark only the last checkbox and put a value in the last input number, the checkbox array will have a true value for its Dice 0, at the same time that the value I put in the input number will be in another Dice, which means the loop will not catch the correct number value..

foreach($fator as $k => $v){
        try{
            $sql = "INSERT INTO " . EMP_MODALIDADES . " (eid,mid,fator) VALUES (:eid,:mid,:fator)";
            $insert = $link->prepare($sql);
            $insert->bindValue(':eid',$eid);
            $insert->bindValue(':mid',$v);
            $insert->bindValue(':fator',$fator[$k]);
            $insert->execute();
        }
        catch(PDOException $erro){
            die('INSERT error: '.$erro->getMessage());
        }

1 answer

0

If anyone has this doubt, I was able to solve the problem using an Hidden input along with the input number.. then, I replicated the ids that were 'lost' in the smaller arrays (if I didn’t check all checkboxes) and could make the insertions properly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.