Heredoc while循环,递减2而不是1

时间:2016-02-19 23:50:38

标签: php html heredoc

我的while循环减少了2,我找不到原因。

我觉得我不明白PHP如何与HTML交互。
任何清晰的词汇? (注意字典的索引从1开始)

if (isset($title)) { 
    $i = sizeof($title);

    while ($i > 0) {
        $idnum = intostring($i); 
        $titletoadd = $title[$idnum];
        $summarytoadd = $summary[$idnum];

        $podcastblock =<<<EOD
        <div class="podcast" id="podcast$idnum">
         <p class="podcasttitle" id="podcasttitle$idnum">$titletoadd</p>
         <div class="container column horizontal-spaced ">
         <p class="podcastnumber left shrink " id="podcastnumber$idnum">$i</p>
         <p class="summary grow" id="summary$idnum">$summarytoadd</p>
         </div>
          <audio class="audioplayer tile displaymiddle wide"  id='audioplayer$idnum" controls>
          <source src="audiofiles/p$i.mp3" type="audio/mpeg">
        </audio>

        EOD;

        echo $podcastblock; 
        $i--;
    }

3 个答案:

答案 0 :(得分:1)

你在这一行上有一个错字:

<audio class="audioplayer tile displaymiddle wide"  id='audioplayer$idnum" controls>

您使用单引号启动id属性值,以双引号结束。因此,直到下一个<audio元素的所有内容都被用作id的一部分,而不是单独的HTML元素。所以你只能看到所有其他DIV

更改为:

<audio class="audioplayer tile displaymiddle wide"  id="audioplayer$idnum" controls>

答案 1 :(得分:0)

首先,删除您不需要此特定代码$idnum = intostring($i);。将其更改为$idnum = $i;

如果您尝试使用 10 或更多元素填充$title数组,则实际上不会减少2。通过这种方式,您可以观察到发生了什么

以下是您的错误:

$title = array("title1", "title2");

/* The array starts from 0 index then 1, 2 and so on
once you get the size of the array it
just counts the elements in the array
but not really knowing the last index */

$i = sizeof($title);

/* in this case our $i have now the value of 2
$title[2] does not exists because we only have two elements
namely $title[0] and $title[1]... this is your first mistake */

// second mistake:

while ($i > 0)

/* our array starts from zero, with this condition,
you're basically skipping zero index (beginning of the array) */

// to fix these you need to put the following codes

$i = (count($title) - 1); /* use count instead of sizeof but they are completely the same...
subtract 1 to the total count to get the last index of the array */

while ($i >= 0) // lastly include the 0 index

希望这有帮助。

答案 2 :(得分:0)

此代码中没有任何内容会导致$ i减少2.如果发生额外的减量,则必须在intostring()方法中。

但是,这里还有其他一些问题。你的html中有一个流氓单引号,它缺少结束div标签。我可以提出一些建议来清理一下。

  1. 对于用PHP包装的HTML块,如果用单引号开始和结束PHP字符串,使用HTML元素中的所有双引号,并明确注入,则更容易理解PHP如何与HTML交互使用字符串连接的PHP变量如:'。 $ variable。'

  2. 您不必将$ i强制转换为字符串,PHP会为您处理。

  3. 我认为for循环在这种情况下更清洁一点,它使代码的意图更加清晰。

  4. 试试这个:

    $title = array("Title1","Title2","Title3");
    $summary = array("Summary1","Summary2","Summary3");
    
    if (isset($title)) {
    
        for ($i= count($title); $i > 0; $i--) {
            $titletoadd = $title[$i];
            $summarytoadd = $summary[$i];
    
            $podcastblock = '
            <div class="podcast" id="podcast'. $i .'">
                <p class="podcasttitle" id="podcasttitle'. $i .'">'. $titletoadd .'</p>
                <div class="container column horizontal-spaced ">
                    <p class="podcastnumber left shrink " id="podcastnumber'. $i .'">'. $i .'</p>
                    <p class="summary grow" id="summary'. $i .'">'. $summarytoadd .'</p>
                </div>
                <audio class="audioplayer tile displaymiddle wide" id="audioplayer'. $i .'" controls>
                    <source src="audiofiles/p'. $i .'mp3" type="audio/mpeg">
                </audio>
            </div>';
    
            echo $podcastblock;
        }
    
    }