【问题标题】:Heredoc while loop, decrementing by 2 instead of 1Heredoc while循环,减2而不是1
【发布时间】:2016-06-01 17:27:31
【问题描述】:

我的 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--;
    }

【问题讨论】:

  • norepo,请详细说明。
  • @VolkerK 你的评论是什么意思?您的链接出错。
  • @Bamar “您的链接出错”?有吗?可能是错误的 html,但不是描述的 2 增量。

标签: php html heredoc


【解决方案1】:

首先,删除这个特定代码$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

希望这会有所帮助。

【讨论】:

  • 谢谢,我不知道 PHP。至于索引,我忘了提到我制作的字典从索引“1”开始,所以索引对齐没问题,但它仍然递减 2。
【解决方案2】:

您在这一行有错字:

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

id 属性值以单引号开头,以双引号结束。因此,直到下一个 &lt;audio 元素之前的所有内容都被用作 id 的一部分,而不是单独的 HTML 元素。所以你只能看到其他DIV

改为:

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

【讨论】:

    【解决方案3】:

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

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

    1. 对于包装在 PHP 中的 HTML 块,如果您使用单引号开始和结束 PHP 字符串,在 HTML 元素中使用所有双引号,并显式注入使用字符串连接的 PHP 变量,例如:'. $变量。'

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

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

    试试这个:

    $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;
        }
    
    }
    

    【讨论】:

    • 是的!所以双重增量原来是与heredoc有关的东西。你能详细说明为什么你使用这个引用结构和heredoc块引用吗?
    • 主要是个人喜好。从表面上看,它使您的代码变得丑陋。而且你的结束标识符必须在它自己行的第一列中,这会破坏你的缩进流程并且看起来真的不合适。我还有一条个人规则,即我不使用任何允许执行空白控制的 PHP 功能。您永远不知道什么时候使用不同操作系统或使用不同 IDE 的人可能需要打开您的代码,如果没有正确处理行尾,您的 heredoc 部分可能会导致难以查明的问题。
    • 是的,听起来更健壮。有什么更快的方法吗?
    • 我会考虑将此代码移动到 javascript 而不是在 PHP 中进行。 PHP 在页面呈现之前进行所有处理,所以如果你只有几个条目,它不会有太大的不同,但如果你有很多 mp3 需要加载,用户不会在页面上看到任何东西,直到 PHP 得到所有您的条目并构建页面。如果此数据来自数据库,您可以将该部分包装在 PHP 服务中,然后从 jQuery 调用该服务以加载每个条目。这样,您的页面将立即加载,并且条目将在加载时出现。这提供了更好的用户体验。
    • 好的,我以后会这样做的。感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 2020-12-10
    • 1970-01-01
    • 2014-01-14
    • 2011-10-06
    • 1970-01-01
    • 2015-01-04
    • 2018-09-23
    相关资源
    最近更新 更多