'How to divide students into N Section and every section to N groups PHP MYSQL?

I would like to divide M students into sections and every section into groups, but there are some constraints :

  1. The user who gives the number of sections and the number of groups from {input}
  2. the section and the group will be inserted into the student table which contains "section_name" and "group_number"

I tried to do some PHP/MySQL code

if ((isset($_GET['grouper'])) && !empty($_GET['groupe']) && !empty($_GET['section'])) {


$get_etud_sec = $_GET['section'];
$get_etud_grp = $_GET['groupe'];
$database = getPDO();
$insert1 = $database->prepare("UPDATE L2 SET section = 'A' ORDER BY nom LIMIT $get_etud_sec;");
$insert2 = $database->prepare(


    "UPDATE L2 SET groupe = '1' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '2' WHERE groupe != '1' AND section = 'A'ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '3' WHERE groupe != '1' AND  groupe != '2' AND section = 'A' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '4' WHERE groupe != '1' AND  groupe != '2' AND  groupe != '3' AND section = 'A' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '5' WHERE groupe != '1' AND  groupe != '2' AND  groupe != '3' AND  groupe != '4' AND section = 'A'ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '6' WHERE groupe != '1' AND  groupe != '2' AND  groupe != '3' AND  groupe != '4' AND  groupe != '5' AND section = 'A'ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '7' WHERE groupe != '1' AND  groupe != '2' AND  groupe != '3' AND  groupe != '4' AND  groupe != '5' AND  groupe != '6' AND section = 'A'ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '8' WHERE groupe != '1' AND  groupe != '2' AND  groupe != '3' AND  groupe != '4' AND  groupe != '5' AND  groupe != '6' AND  groupe != '7' AND section = 'A' ORDER BY nom LIMIT $get_etud_grp;"
);

$database = getPDO();
$insert3 = $database->prepare("UPDATE L2 SET section = 'B' WHERE section != 'A' ORDER BY nom LIMIT $get_etud_sec;");
$insert4 = $database->prepare(
    "UPDATE L2 SET groupe = '1' WHERE section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '2' WHERE groupe != '1' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '3' WHERE groupe != '1' AND groupe != '2' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '4' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '5' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '6' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '7' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '8' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND groupe != '7' AND section = 'B' ORDER BY nom LIMIT $get_etud_grp;"
);
$database = getPDO();
$insert5 = $database->prepare("UPDATE L2 SET section = 'C' WHERE section != 'A' AND section != 'B' ORDER BY nom LIMIT $get_etud_sec;");
$insert6 = $database->prepare(
    "UPDATE L2 SET groupe = '1'WHERE section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '2' WHERE groupe != '1' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '3' WHERE groupe != '1' AND groupe != '2' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '4' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '5' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '6' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '7' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '8' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND groupe != '7' AND section = 'C' ORDER BY nom LIMIT $get_etud_grp;"
);
$database = getPDO();
$insert7 = $database->prepare("UPDATE L2 SET section = 'D' WHERE section != 'A' AND section != 'B' AND section != 'C' ORDER BY nom LIMIT $get_etud_sec;");
$insert8 = $database->prepare(
    "UPDATE L2 SET groupe = '1' WHERE section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '2' WHERE groupe != '1' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '3' WHERE groupe != '1' AND groupe != '2' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '4' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '5' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '6' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '7' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;
UPDATE L2 SET groupe = '8' WHERE groupe != '1' AND groupe != '2' AND groupe != '3' AND groupe != '4' AND groupe != '5' AND groupe != '6' AND groupe != '7' AND section = 'D' ORDER BY nom LIMIT $get_etud_grp;"
);
$insert1->execute();
$insert2->execute();
$insert3->execute();
$insert4->execute();
$insert5->execute();
$insert6->execute();
$insert7->execute();
$insert8->execute();

}

But unfortunately, I couldn't Set the code in a way that the user who give the number of section and the numbers of groups

Any ideas, please?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source