<?php if ($cat['cptdtw']) { $key=0;foreach ($cat['cptdtw'] as $c) { ?>
<div class="feature-item">
<p><img height="70" src="{dr_get_file($c.file)}" /></p>
<p class="{$c.title}">{$c.title}</p>
</div>
<?php $key++;} } ?> 當前欄目多文件循環(huán) 想改為 從第五個開始循環(huán) 共顯示6個數(shù)據(jù) 咋改呢?
開源是一種精神,但不是義務,幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復@迅睿官方技術專家
<?php if ($cat['cptdtw']) { $key=0;foreach ($cat['cptdtw'] as $c) num=5,6 { ?>
<div class="feature-item">
<p><img height="70" src="{dr_get_file($c.file)}" /></p>
<p class="{$c.title}">{$c.title}</p>
</div>
<?php $key++;} } ?> 這樣嗎?
參考文檔:《數(shù)組剪切函數(shù) dr_arraycut》
<?php if ($cat['cptdtw']) { $cat['cptdtw']=dr_arraycut($cat['cptdtw'], '5,11');$key=0;foreach ($cat['cptdtw'] as $c) { ?>
這種php代碼的,可以找ai幫你改寫就行了
開源是一種精神,但不是義務,幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
回復@迅睿官方技術專家
<?php if ($cat['cptdtw']) { $cat['cptdtw']=dr_arraycut($cat['cptdtw'], '0,5');$key=0;foreach ($cat['cptdtw'] as $c) { ?>
<div class="feature-item">
<p><img height="70" src="{dr_get_file($c.file)}" /></p>
<p class="{$c.title}">{$c.title}</p>
</div>
<?php $key++;} } ?>
<?php if ($cat['cptdtw']) { $cat['cptdtw']=dr_arraycut($cat['cptdtw'], '5,11');$key=0;foreach ($cat['cptdtw'] as $c) { ?>
<div class="feature-item">
<p><img height="70" src="{dr_get_file($c.file)}" /></p>
<p class="{$c.title}">{$c.title}----</p>
</div>
<?php $key++;} } ?>
同時存在二組的話 只有一組生效呢?
開源是一種精神,但不是義務,幫忙是情分,不幫也不要抱怨,建議大家多研究代碼、多閱讀代碼、多翻閱社區(qū)歷史問題!
出現(xiàn) “兩組代碼同時存在時只有一組生效” 的核心原因是:第一組代碼修改了
$cat['cptdtw']的原始值,導致第二組代碼處理的是被截斷后的數(shù)組,無法獲取到正確的元素。具體分析:
第一組代碼中,
$cat['cptdtw'] = dr_arraycut(...)這行代碼直接修改了原始數(shù)組,將其截斷為 “0-5” 的部分(即前 5 條數(shù)據(jù))。第二組代碼再次判斷
$cat['cptdtw']時,此時的$cat['cptdtw']已經(jīng)是第一組截斷后的結果(只有 5 條數(shù)據(jù))。當?shù)诙M執(zhí)行
dr_arraycut($cat['cptdtw'], '5,11')時,由于原始數(shù)組已被截斷為 5 條(索引 0-4),從索引 5 開始截取自然會得到空數(shù)組,因此第二組循環(huán)不會執(zhí)行,最終只有第一組生效。解決方案:
保留原始數(shù)組,對原始數(shù)組進行 “分別截取”,避免修改原始變量。可以通過臨時變量保存原始數(shù)據(jù),再分別處理:
php
<?php // 保存原始數(shù)組到臨時變量,避免被修改$original_cptdtw = $cat['cptdtw'] ?? [];// 第一組:截取0-5的部分(前5條)if ($original_cptdtw) { $first_part = dr_arraycut($original_cptdtw, '0,5'); $key=0; foreach ($first_part as $c) { ?><div class="feature-item"> <p><img height="70" src="{dr_get_file($c.file)}" /></p> <p class="{$c.title}">{$c.title}</p></div><?php $key++; } } // 第二組:截取5-11的部分(從第5條開始,取6條)if ($original_cptdtw) { $second_part = dr_arraycut($original_cptdtw, '5,11'); $key=0; foreach ($second_part as $c) { ?><div class="feature-item"> <p><img height="70" src="{dr_get_file($c.file)}" /></p> <p class="{$c.title}">{$c.title}----</p></div><?php $key++; } } ?>關鍵原理:
通過
$original_cptdtw = $cat['cptdtw'] ?? []保存原始數(shù)組,兩組代碼分別從原始數(shù)組中截取不同范圍的元素,彼此不干擾,最終兩組循環(huán)都會正常執(zhí)行。AI編程才是大師