essence: in the form of 5 lines (maybe not 5), in each of them there are 2 fields (let's call a group), each group belongs to a specific line in the database (in the database has id and these 2 fields). the groups themselves in html are output in a loop, id is assigned to each group (belonging to lines in the database), when I submit a form, I get all the fields in a row, I need to understand how to group them before sending, or the laconic way to work with a bunch of data

$ a:

Array ( [0] => Array ( [ID] => 1 [forms_email] => 525, 123 [form_id] => 1 [form_name] => form_comp [active] => N ) [1] => Array ( [ID] => 38 [forms_email] => 532523 [form_id] => 2 [form_name] => TESTED [active] => N ) [2] => Array ( [ID] => 44 [forms_email] => [form_id] => 6 [form_name] => 5566 [active] => N ) ) 

the form:

 <?for($i=0;$i<count($a);$i++){?> <tr> <td><?=$a[$i]["form_name"]?></td> <td><input type="text" value="<?=$a[$i]["forms_email"]?>" name="field<?=$a[$i]['ID']?>"> <input type="checkbox" name="check<?=$a[$i]["ID"]?>"> Активен?</td> <input type="hidden" name="id<?=$a[$i]["ID"]?>" value="id<?=$a[$i]["ID"]?>"> </tr> <?}?> 

what comes in the post:

 [field1] => 525, 123 [check1] => on [id1] => id1 [field38] => 532523 [check38] => on [id38] => id38 [field44] => [id44] => id44 

the question is how best to put this data in the table in the database (given that checkbox if not selected does not come). field and check - fields in the table, id - for identification

  • Some kind of incomprehensible salad. What exactly is not clear? Give the code clearly and what did not work - Mr. Black
  • The author, where are you? Talk to me :( - Mr. Black

3 answers 3

 <?php $a = array( 0 => array( 'ID' => 1, 'form_name' => 'form_comp', 'forms_email' => '525, 123', ), 1 => array( 'ID' => 38, 'form_name' => 'TESTED', 'forms_email' => 532523, ), 2 => array( 'ID' => 44, 'form_name' => 5566, 'forms_email' => '525, 123', ) ); foreach($a as $i => $value) { $id = $a[$i]['ID']; $fn = $a[$i]['form_name']; $fe = $a[$i]['forms_email']; echo <<<HTML <tr> <td>$fn</td> <td> <input type='text' value='$fe' name='field=$id'> <input type='checkbox' name='check=$id'>Активен? </td> <input type='hidden' name='id=$id' value='id=$id'> </tr> HTML; } ?> 

source

  • As I understand it, the code looks something like this - Mr. Black
  • I usually use id when grouping fields. Then you can get data from POST by key from DB. - ilyaplot
  • @ilyaplot, Now I really do not understand what the author wants. When you change the input, something must be saved with a check mark or you need a save button - Mr. Black
 $a = isset($_POST['a'])?$_POST['a']:0; $b = isset($_POST['b'])?$_POST['b']:0; $c = isset($_POST['c'])?$_POST['c']:0; $d = isset($_POST['d'])?$_POST['d']:0; $e = isset($_POST['e'])?$_POST['e']:0; $stmt = $db->prepare("insert into table (a,b,c,d,e) values (?,?,?,?,?)"); $stmt->bind_param("iiiii",$a,$b,$c,$d,$e); 

    Thanks for the answers, I made myself as follows:

     <?for($i=0;$i<count($a);$i++){?> <tr> <td><?=$a[$i]["form_name"]?></td> <td><input type="text"value="<?=$a[$i]["forms_email"]?>" name="field<?=$a[$i]['ID']?>"> <input type="checkbox" <?if($a[$i]["active"]=="on")echo"checked"?> name="check<?=$a[$i]["ID"]?>"> Активен?</td> <input type="hidden" name="<?=$i?>" value="<?=$a[$i]["ID"]?>"> </tr> <?}?> <input type="hidden" name="count" value="<?=count($a)?>"> 

    in the handler:

     $update=$request->getPostList(); for($i=0;$i<$update["count"];$i++){ namespaces\class\ListTable::update($update[$i], array( "forms_email" => $update["field".$update[$i]], "active" => $update["check".$update[$i]], )); } 

    this is bitrix. In short, I will explain what is written here:

    post arrives, we start a cycle from 0 to count (fields in html from the base were formed according to the same cycle, i.e. the number matches), the form has fields whose names are loop iterations (0,1,2, ...) , their values ​​are the id fields in the database, 2 other fields are named field<?=$a[$i]['ID']?> , i.e. we specify a specific string in the database, also with a check. and running through the cycle, we update all fields of all rows in the database, the problem is solved