简体   繁体   中英

multiple foreach loops for different variables?

foreach ($textAr as $BachelorsDegrees)
{
foreach ($textArCert as $Certifications)
{
foreach ($textArDip as $Diplomas)
{
foreach ($textArAD as $AssociateDegrees)
{
foreach ($textArMD as $MastersDegrees)
{
foreach ($textArDD as $DoctorateDegrees)
{
foreach ($textArSD as $SpecialDegreePrograms)
{

what would be the best way to make the above code work (it's just a snippet of the entire code)? Each variable, for example $BachelorsDegrees, is a textarea box in the HTML form. I am trying to insert values from each textarea box into the database in individual rows. I got it working for one textarea box, but how to make it work for multiple textarea boxes?

Was thinking of doing an if else statement and an INSERT and UPDATE sql statement so that when one foreach loop worked, it would INSERT data into the database, then the loop would stop, then an else statement would go for the next foreach loop and an UPDATE query would update the next column for the same product instead of inserting. If I did multiple INSERT queries, I would be creating more rows for each variable, which I don't want. More rows for the values on each variable, but not rows for the actual variable. Each variable represents a column.

For example,

When the values from the BachelorsDegrees textarea get inserted into its column, the next loop will run (Certifications) into the next column. So I was thinking an UPDATE query would be effective. That way I don't have NULLS in every column.

If you have equal number of inputs for all the fields(Bachelorsdegree,certifications,etc), then you can take count of one input field into equation.eg

$totalinputs=count($textAr);
for($i=0;$i<$totalinputs;$i++){
   $bachelordegree=$textAr[$i];
   $certificate=$textArCert[$i];
   $diploma=$textArDip[$i];
    .....
    .....
    //use above stored variables to insert/update into      your  respective table.

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM