How to make an php loop into an another php loop according to an array?
Get the solution ↓↓↓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>
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: constant expression contains invalid operations
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.