John Davidson

php - dynamic rowspan problem in table in laravel (product variations)

0 comments
Message:


Consider the original output for example:



And then I want rowspan like this



Array code:



Here is my laravel code:



foreach ($color as $key => $val) {
foreach ($size as $key1 => $val1) {
$data .=
'<tr>
<td style="text-align:center;">
<input class="" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
</td>
<td class="py-1">' . $key1 . '</td>
<td>' . strtoupper($val) . '</td>
<td>' . strtoupper($val1) . '</td>
<td>' . ($same_price_flag === true ? '' : '<input type="number" class="form-control" autocomplete="off">') . '</td>
<td>' . ($same_price_flag === true ? '' : '<input type="number" class="form-control" autocomplete="off">') . '</td>
<td>' . ($same_price_flag === true ? '' : '<input type="number" class="form-control" autocomplete="off">') . '</td>
<td>' . ($same_price_flag === true ? '' : '<input type="number" class="form-control" autocomplete="off">') . '</td>
<td><input type="text" class="form-control" autocomplete="off"></td>
<td><input type="number" class="form-control" autocomplete="off"></td>
<td><input type="number" class="form-control" autocomplete="off"></td>
</tr>
';
}
}

}



My Answer:
> You can achieve dynamic rowspan in a table in Laravel by using the `rowspan` attribute in the HTML table. Here's an example of how you can modify your code to achieve the desired output:

php







@foreach ($color as $key => $value)

@if ($key == 0)

@endif




@endforeach
Product Color Size Price
{{ $value['product'] }} {{ $value['color'] }} {{ $value['size'] }} {{ $value['price'] }}



In this code snippet, we check if the current iteration is the first one (`$key == 0`) and if it is, we set the `rowspan` attribute for the Product column to the total count of colors. This will create the desired rowspan effect in the table.

Make sure to adjust the variable names and array keys according to your actual data structure.

Rate this post

5 of 5 based on 7840 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us