簡體   English   中英

為什么php while循環不掃描x #of次並包含x#個文件?

[英]Why doesn't php while loop scan through x #of times and include x # of files?

我有一些代碼可用於根據表單輸入創建電子郵件。 第一個輸入之一是名為“ nosections ”的數字輸入,用於控制電子郵件中有多少個部分。 然后有“ sectionblock ”輸入(例如, sectionblock1sectionblock2 ),用於控制電子郵件的每個部分中包含哪些內容塊。

我想使用一個while循環,該循環將遍歷每個節的值,直到達到“ nosections”值,並在遍歷每個節時拉出每個節的塊...但是,它似乎僅包括第一個sectionblock(即sectionblock1 )。

例如,說出以下事實:

  • $ nosections = 3
  • $ sectionblock1 =“品嘗類塊”
  • $ sectionblock2 =“ rrtastings”
  • $ sectionblock3 =“葡萄酒特惠”

有人可以告訴我為什么如果我運行上面的代碼,它僅包含tastingclassblock的代碼塊,而不包含rastasting和winespecials的代碼塊嗎?

<?php 
$i=1; 

while ($i <= $nosections):
    if(${'sectionblock'.$i} == 'weekendtasting'):
        include('plaintext/weekend-tastings/weekend-tasting.php');
    elseif(${'sectionblock'.$i} == 'tastingclassblock'):
        include('plaintext/special-classes/tasting-class-code.php');
    elseif(${'sectionblock'.$i} == 'rrtastings'):
        include('plaintext/resroomtastings/tasting-class-code.php');
    elseif(${'sectionblock'.$i} == 'weeklyad'):
        include('plaintext/weekly-ad/weeklyad.php');
    elseif(${'sectionblock'.$i} == 'customerappreciation'):
        include('plaintext/customer-appreciation/customer-appreciation.php');
    elseif(${'sectionblock'.$i} == 'giftsets'):
        include('plaintext/holiday-giftsets/gift-sets-code.php');
    elseif(${'sectionblock'.$i} == 'winespecials'):
        include('plaintext/winespecials/wine-specials.php');
    elseif(${'sectionblock'.$i} == 'spiritspecials'):
        include('plaintext/spiritspecials/spirit-specials.php');
    elseif(${'sectionblock'.$i} == 'featuredfood'):
        include('plaintext/featured-foodgroup/featured-food-group.php');
    elseif(${'sectionblock'.$i} == 'featuredrecipe'):
        include('plaintext/featured-recipe/featured-recipe.php');
    endif;

$i++;
endwhile; ?>

在本地測試您的代碼,一切運行正常。 首先,您應該檢查$nosections是否真的得到3的值。 其次,您應該檢查包含是否有任何錯誤,因為在本地我只是回顯了它們,但是如果您遇到任何錯誤,則可能是因為包含錯誤而導致while循環停止。 include('plaintext/special-classes/tasting-class-code.php');之后include('plaintext/special-classes/tasting-class-code.php');檢查錯誤include('plaintext/special-classes/tasting-class-code.php'); 被包括在內。 最好的祝福。

很抱歉浪費您的所有時間。 我才意識到問題出在哪里。 在我試圖包括的幾個模塊中,我還使用變量$ i在while循環中運行。 因此,在運行這些循環時,$ i被誇大了超過$ nosections的數量。 我將變量名稱更改為$ s,一切似乎都可以正常工作。

感謝您的友好答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM