【问题标题】:Unexpected end of file when using nested for/foreach loops [duplicate]使用嵌套的 for/foreach 循环时文件意外结束 [重复]
【发布时间】:2019-10-25 07:57:56
【问题描述】:

我循环出大量数据来创建表,但是在循环在文件底部终止后,我收到了 EOF PHP 错误。这是一种独特的语法,但我认为代码没有问题。下面是代码(从第一个 foreach 循环开始):

            <div class="tbody">

                <?php foreach ($extra_html['data'] as $referrers) { ?>

                <div class="tr-container">

                    <div class="tr" data-referral-id="28401">

                        <div class="referral-data-container">
                            <span class="data blue referred-by"><?=$referrers['referrer']?></span>
                            <span class="data referral-number"><?=$referrers['referrals_made']?></span>
                            <span class="data blue qualified-number"><?=$referrers['referrals_qualified']?></span>
                            <span class="data sold-number green"><?=$referrers['referrals_sold']?></span>
                            <input type="text" value="$<?=$referrers['next_payout_amount']?>" class="data-input next-payout-amount input-widths" style="margin-right:25px;">
                            <input type="date" value="<?=($referrers['next_payout_date'] === '0' ? 'yyyy-MM-dd' : $referrers['next_payout_date'])?>" class="data-input next-payout-date input-widths" style="margin-right:25px;">
                            <input type="text" value="<?=$referrers['salesperson']?>" class="data-input salesperson input-widths">
                        </div>

                        <div class="plus-sign"></div>

                    </div>

                    <div class="nested-table">

                        <div class="nested-thead">

                            <div>
                                <span class="heading number-width">#</span>
                                <span class="heading referral-name-width">Referral</span>
                                <span class="heading referral-date-width">Referral Date</span>
                                <span class="heading status-width">Status</span>
                                <span class="heading checkboxes-width">Qualified Payout</span>
                                <span class="heading input-widths">Sold Payout</span>
                            </div>

                        </div>

                        <div class="nested-tbody">

                            <?php for ($i = 0; $i < count($referrers['referrals']); $i++) { ?>

                            <div class="nested-tr">

                                <span class="data number-width"><?=$i?></span>
                                <span class="data blue referral-name-width" onclick="showReferralModal(this)"><?=$referrers['referrals'][$i]['referral']?></span>
                                <span class="data referral-date-width"><?=$referrers['referrals'][$i]['date_added']?></span>

                                <div class="status-width">

                                    <select>
                                        <option selected>New Referral</option>
                                        <option>Qualified Referral</option>
                                        <option>DQ'd Referral</option>
                                        <option>Sold Referral</option>
                                    </select>

                                </div>

                                <div class="checkboxes-width">
                                    <input type="checkbox" class="qualified-payout-checkbox blue">
                                </div>

                                <div class="checkboxes-width">
                                    <input type="checkbox" class="sold-payout-checkbox blue">
                                </div>

                            </div>

                            <? } ?>

                        </div>

                    </div>

                </div>

                <?php } ?>

            </div>

        </div>

    </div>

    <div class="modal-container">

        <div class="white-box">

            <div class="top">
                <h3>Name Here</h3>
                <div class="close" onclick="closeModal()"></div>
            </div>

        </div>

    </div>

<script src="/js/referral.js"></script>

<?php include_once '../view/includes/footer_nav.php'; ?>
<?php include_once '../view/includes/footer.php'; ?>

为了解释独特的语法,我在关闭内部 for 循环后关闭了第一个 foreach 循环三个关闭 div 标记 (&lt;/div&gt;)。 EOF 错误出现在最后一个 include_once 语句之后。我这样做是为了从我的 IDE (Sublime) 中获得语法高亮。我尝试使用这种语法:

<?php foreach ($extra_html['data'] as $referrers): ?>
    // html
    <?php for ($i = 0; $i < count($referrers['referrals']); $i++): ?>
        // more html
    <?php endfor; ?>
    // html
<?php endforeach; ?>

但我收到“意外的 foreach 语句”错误。

所以我想我的问题有两个方面:
如何解决 EOF 错误以及我目前的处理方式?如果答案是否定的,那么更好的方法是什么?

【问题讨论】:

  • 我个人更喜欢第二种方式,因为它更具可读性。听起来您在 foreach 之前发生了某种错字,错误消息只是说“这个 foreach 在这里做什么?”可能是一个未封闭的报价或类似的东西。

标签: php


【解决方案1】:

嗯,我不确定,但这种语法对我来说似乎不正确:

                    </div>

                    <? } ?>

                </div>

如果是这样的话,这将是一个非常简单的解决方案;)

【讨论】:

  • 你是说改成这样吗?我会试试的
  • 天哪!!!!不,你是说那是问题......解决了它!!!!谢谢!!当你盯着同一个代码几个小时时,看起来那些东西就离你远去了……谢谢!!!
  • @AdamMcGurk 哈哈,我们都去过那里。不客气。 (请不要忘记接受答案,需要那个streetcred)
  • 请不要回答解决方案是“修复错字”的问题。这种类型的问题没有长期价值,因为其他有相同问题的人不会通过搜索找到这个问题。 “错字”是标准的关闭投票原因之一。回答此类问题可能会阻止 OP 删除它以从 StackOverflow 中消除膨胀。
  • @JohnConde 很高兴知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
相关资源
最近更新 更多