robith jadzab
robith jadzab

Reputation: 39

PHP : Making multiple loop of for and foreach

This is continuation of my previous question.

I have this code :

$bag3 = 7;
$row = 4;
$arr = array("red", "green", "blue", "yellow");
foreach ($arr as $ay) {
    for ($nom = 1; $nom <= $bag3; $nom++, $row++){
    }
    $m1 = ($row - $bag3) + 1;
    echo "->mergeCells('A".$m1.":A".$row."')"."\n";
    for ($nom = 1; $nom <= $bag3; $nom++, $row++){
        $satu = ($row - $bag3) + 1;
        echo "->setCellValue(A".$satu.",".$ay.")"."\n";
    }
    $dua = $satu + 1;
    echo "->setCellValue(A".$dua.",".$ay.")"."\n";
}

And its output:

->mergeCells('A5:A11')
->setCellValue('A5',red)
->setCellValue('A6',red)
->setCellValue('A7',red)
->setCellValue('A8',red)
->setCellValue('A9',red)
->setCellValue('A10',red)
->setCellValue('A11',red)
->setCellValue('A12',red)
->mergeCells('A19:A25')
->setCellValue('A19',green)
->setCellValue('A20',green)
->setCellValue('A21',green)
->setCellValue('A22',green)
->setCellValue('A23',green)
->setCellValue('A24',green)
->setCellValue('A25',green)
->setCellValue('A26',green)
->mergeCells('A33:A39')
->setCellValue('A33',blue)
->setCellValue('A34',blue)
->setCellValue('A35',blue)
->setCellValue('A36',blue)
->setCellValue('A37',blue)
->setCellValue('A38',blue)
->setCellValue('A39',blue)
->setCellValue('A40',blue)
->mergeCells('A47:A53')
->setCellValue('A47',yellow)
->setCellValue('A48',yellow)
->setCellValue('A49',yellow)
->setCellValue('A50',yellow)
->setCellValue('A51',yellow)
->setCellValue('A52',yellow)
->setCellValue('A53',yellow)
->setCellValue('A54',yellow)

you see, from the second "Merge cell" $satu value become like ($dua + 7). but what I wanted is the second $m1 = $dua + 1 and so the third and fourth, and value of $satu after "merge cell" start from ($dua + 1).

I have asked to my senior, but he seems as confused as me. So, what should I change in my code? Thanks and sorry for the bad English.

Upvotes: 0

Views: 49

Answers (1)

Nick
Nick

Reputation: 147146

Your issue is with the empty for loop, which is causing $row to increment by 14 through each outer loop, where you only want it to increment by 7. You can fix that by removing that loop and then adjusting the values in the mergeCells and setCellValue output strings. You can add the setCellValue from the second array by using the key from the first one to index into it:

$bag3 = 7;
$row = 4;
$arr = array("red", "green", "blue", "yellow");
$gab = array("der", "neerg", "uelb", "wolley");
foreach ($arr as $key => $ay) {
    echo "->mergeCells('A" . ($row + 1) . ":A" . ($row + $bag3) . "')" . "\n";
    for ($nom = 1; $nom <= $bag3; $nom++){
        echo "->setCellValue('A" . ++$row . "','$ay')" . "\n";
    }
    echo "->setCellValue('A" . ++$row . "','{$gab[$key]}')" . "\n";
}

Output:

->mergeCells('A5:A11')
->setCellValue('A5','red')
->setCellValue('A6','red')
->setCellValue('A7','red')
->setCellValue('A8','red')
->setCellValue('A9','red')
->setCellValue('A10','red')
->setCellValue('A11','red')
->setCellValue('A12','der')
->mergeCells('A13:A19')
->setCellValue('A13','green')
->setCellValue('A14','green')
->setCellValue('A15','green')
->setCellValue('A16','green')
->setCellValue('A17','green')
->setCellValue('A18','green')
->setCellValue('A19','green')
->setCellValue('A20','neerg')
->mergeCells('A21:A27')
->setCellValue('A21','blue')
->setCellValue('A22','blue')
->setCellValue('A23','blue')
->setCellValue('A24','blue')
->setCellValue('A25','blue')
->setCellValue('A26','blue')
->setCellValue('A27','blue')
->setCellValue('A28','uelb')
->mergeCells('A29:A35')
->setCellValue('A29','yellow')
->setCellValue('A30','yellow')
->setCellValue('A31','yellow')
->setCellValue('A32','yellow')
->setCellValue('A33','yellow')
->setCellValue('A34','yellow')
->setCellValue('A35','yellow')
->setCellValue('A36','wolley')

Note that you may want to output the $ay and $gab values inside quotes as well using

echo "->setCellValue('A" . ++$row . "','$ay')" . "\n"; 

Demo on 3v4l.org

Upvotes: 1

Related Questions