I am trying to nest a php loop into another php loop according to an php array. First loop will continue the loop according how many elements the array has and I want the second loop will continue according to the element. If the first element is 2 the user input repeat 2 times after that the second element is 3 the user input repeat 3 times. when all the elements are gone the loop stopped. but I don't know how to do it.
<body style="margin: 0 auto; width: 50%;">
<div class="input-field col s12">
<?php
$dolils = array("2","3","4");
$total_dolils = count($dolils);
for ($x = 1; $x <= $total_dolils; $x++) {
?>
<p class="blue-text" style="color: blue; font-weight: bold;"><?php echo $x; ?> নং দিনে দলিলের হিসাবঃ </p>
<?php
$z = 0;
for($y = 0; $y <= $dolils[$z]; $y++) {
?>
<p class="red-text" style="color: red; font-weight: bold;">দলিলের নাম্বার</p>
<input id="input_number" type="number" name="dolils_no[]" data-length="10">
<p>মোট পৃষ্ঠাঃ </p>
<input id="input_number" type="number" name="page[]" data-length="10">
<p>মোট লাইনঃ </p>
<input id="input_number" type="number" name="line[]" data-length="10">
<?php
$z++;
echo "<br/>";
echo $z;
echo "<br/>";
if($z >= end($dolils)){
die;
}
}
}
?>
</div>
</body>
Basic logic:
<?php
$content = '<div>';
$dolils = array (2, 3, 4);
foreach ($dolils as $dolil) {
for ($i = 1; $i <= $dolil; $i++) {
$content .= "<p>iteration {$dolil}/{$i} </p>";
}
}
$content .= '</div>';
// after all
echo $content;
bonus tip:
Stop mixing PHP and HTML like this is not comfortable, error pron and just ugly. Instead build HTML in one piece of PHP code or better get familiar with templating engines and/or MVC patterns.