php - Add 3 <td> inside every <tr> loop

I am retrieving data from the DB and want to fill a table with 3<td></td>
per<tr></tr>
Here's what I have:
foreach($extras AS $key => $extra){
if($key % 3 == 0){
echo "<tr>";
}
echo "<td>
<input type='checkbox' data-price='$extra->precio' value='$extra->id' name='extras[]' id='extra-$extra->id' />". $key . "
<label for='extra-$extra->id'>
<img class='mx-auto d-block' src='" . $this->config->item('base_url') . "assets/" . $extra->extra_uri . "' alt='$extra->nombre' />
<p class='kit text-center'>$extra->nombre</p>
</label></td>
";
if($key % 3 != 0){
echo "</tr>";
}
}
It gives me a weird table, however if I do it with$key % 2
then it gives me a good table with 2 td's inside, for whatever reason, I just can't make it work with 3.
I looked at: PHP: How do you determine every Nth iteration of a loop? but that's pretty much what I've done to no avail.
Answer
Solution:
$_eol = 0;
foreach($extras AS $key => $extra){
if($key % 3 == 0 || $key == 0){
$_eol++;
echo "<tr>";
}
echo "<td>
<input type='checkbox' data-price='$extra->precio' value='$extra->id' name='extras[]' id='extra-$extra->id' />". $key . "
<label for='extra-$extra->id'>
<img class='mx-auto d-block' src='" . $this->config->item('base_url') . "assets/" . $extra->extra_uri . "' alt='$extra->nombre' />
<p class='kit text-center'>$extra->nombre</p>
</label></td>
";
if($_eol == 3 ){
echo "</tr>";
$_eol = 0;
}
}
I think there will be a solution.
Answer
Solution:
try with given code
echo "<tr>";
foreach($extras AS $key => $extra){
echo "<td> <input type='checkbox' data-price='$extra->precio' value='$extra->id' name='extras[]' id='extra-$extra->id' />". $key . " <label for='extra-$extra->id'> <img class='mx-auto d-block' src='" . $this->config->item('base_url') . "assets/" . $extra->extra_uri . "' alt='$extra->nombre' /> <p class='kit text-center'>$extra->nombre</p> </label></td> ";
if($key % 3 == 0){
echo "</tr><tr>";
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update
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.