[英]PHP array in twig
我在php中有此代碼,我在樹枝中將其轉換,但沒有得到相同的結果。
<?php for($j=0;$j<sizeof($products[$category]);) { ?>
<div class="wrapper">
<?php for($f=0;$f<$rows;$f++,$j++) {
if($j<sizeof($products[$category])) { ?>
<?php if($products[$category][$j]) { ?>
<?php }
if(sizeof($products[$category])<(4*$rows)-1){
if(((sizeof($products[$category])-($j)) + 1)%3 == 0){
$cnt = 1;
}
if($cnt == 1){
$f++;
}
}
?>
<?php } ?>
<?php } ?>
</div>
在樹枝文件中:
{% for j in 0..(products[category]|length)-1 %}
<div class="wrapper">
{% for f in 0..rows-1 %}
{% if j < products[category]|length %}
{% if products[category][j] %}
{% endif %}
{% if products[category]|length < ((4*rows)-1) %}
{% if(((products[category]|length)-(j)) + 1)%rows == 0 %}
{% set cnt = 1 %}
{% endif %}
{% if cnt == 1 %}
{% set f = f + 1 %}
{% endif %}
{% endif %}
{% endif %}
{% set j = j + 1 %}
{% endfor %}
</div>
如何使用for循環f迭代實現$j++
?
請幫忙。
提前致謝。
試試這個,它將為您服務
<div class="wrapper">
{% set j = 0 %}
{% for f in 0..rows-1 %}
{% if j < products[category]|length %}
{% if products[category][j] %}
{% endif %}
{% if products[category]|length < ((4*rows)-1) %}
{% if(((products[category]|length)-(j)) + 1)%rows == 0 %}
{% set cnt = 1 %}
{% endif %}
{% if cnt == 1 %}
{% set f = f + 1 %}
{% endif %}
{% endif %}
{% endif %}
{% if f < rows %}
{% set j = j + 1 %}
{% endif %}
{% endfor %}
</div>
根據所需的輸出編寫以下代碼。
像-總行數2,則項目為6,然后
1 3 5
2 4 6
行是3,項目是8,則
1 4 7
2 5 8
3 6
假設樹枝跟隨-從零開始的索引數組
{% set TOTALITEMS = (products[category]|length) %}
{% set TOTALROWS = 2 %}
{% set ITEMSPERROW = TOTALITEMS / TOTALROWS %} <!-- make sure you get integer number only, if not then make it by helper functions of php / twig -->
{% for rowIndex in 0 .. TOTALROWS - 1 %}
<!-- start a div which will contain a row -->
{% for columnIndex in 0 .. ITEMSPERROW - 1 %}
<!-- here you have a item ready to dump in html -->
{% products[category][ (columnIndex * TOTALROWS) + rowIndex ] %}
{% endfor %} <!-- end of loop for columnIndex ...-->
<!-- end a div which contained a row -->
{% endfor %} <!-- end of loop for rowIndex ...-->
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.