PHP 包括外部开关不工作

PHP include external swich not working

我有以下代码片段可以正常工作,但我想要开关 switch ($fixture->homeTeamName)switch ($fixture->awayTeamName) ) 包含在名为 swich_1.php 的外部文件中,模板中没有硬编码,但我未能包含它:

<table class="table table-striped">
                    <tr>
                    <th>HomeTeam</th>
                    <th></th>
                    <th>AwayTeam</th>
                    <th colspan="3">Result</th>
                    </tr>
                    <?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { 
                        switch ($fixture->homeTeamName) {
                            case 'Walsall FC':
                                $fixture->homeTeamName =  "AA";
                                break;
                            case 'Rochdale AFC':
                                $fixture->homeTeamName =  "BB";
                                break;
                            }
                        switch ($fixture->awayTeamName) {
                            case 'Oldham Athletic AFC':
                                $fixture->awayTeamName =  "CC";
                                break;
                            case 'Peterborough United FC':
                                $fixture->awayTeamName =  "DD";
                                break;
                            }
                      ?>
                    <tr>
                        <td><?php echo $fixture->homeTeamName; ?></td>
                        <td>-&nbsp;</td>
                        <td><?php echo $fixture->awayTeamName; ?></td>
                        <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                        <td>:</td>
                        <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
                    </tr>
                    <?php } ?>
                </table>

这是我试过的:

        <table class="table table-striped">
            <tr>
            <th>HomeTeam</th>
            <th></th>
            <th>AwayTeam</th>
            <th colspan="3">Result</th>
            </tr>
            <?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { 

                <? include '/models/switch_1.php' ?>
              ?>

            <tr>
                <td><?php echo $fixture->homeTeamName; ?></td>
                <td>-&nbsp;</td>
                <td><?php echo $fixture->awayTeamName; ?></td>
                <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                <td>:</td>
                <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
            </tr>
            <?php } ?>

这里是外部文件switch_1.php

<?php
switch ($fixture->homeTeamName) {
                            case 'Walsall FC':
                                $fixture->homeTeamName =  "AA";
                                break;
                            case 'Rochdale AFC':
                                $fixture->homeTeamName =  "BB";
                                break;
                            }
switch ($fixture->awayTeamName) {
                            case 'Oldham Athletic AFC':
                                $fixture->awayTeamName =  "CC";
                                break;
                            case 'Peterborough United FC':
                                $fixture->awayTeamName =  "DD";
                                break;
                            }
?>

我出现黑屏,感谢您的帮助。

你这样做:

创建文件:file.php

 <?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { 
                        switch ($fixture->homeTeamName) {
                            case 'Walsall FC':
                                $fixture->homeTeamName =  "AA";
                                break;
                            case 'Rochdale AFC':
                                $fixture->homeTeamName =  "BB";
                                break;
                            }
                        switch ($fixture->awayTeamName) {
                            case 'Oldham Athletic AFC':
                                $fixture->awayTeamName =  "CC";
                                break;
                            case 'Peterborough United FC':
                                $fixture->awayTeamName =  "DD";
                                break;
                            }
                      ?>
                    <tr>
                        <td><?php echo $fixture->homeTeamName; ?></td>
                        <td>-&nbsp;</td>
                        <td><?php echo $fixture->awayTeamName; ?></td>
                        <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                        <td>:</td>
                        <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
                    </tr>
                    <?php } ?>

然后,在您的主页中,将内容替换为 "include" 引用

<table class="table table-striped">
                    <tr>
                    <th>HomeTeam</th>
                    <th></th>
                    <th>AwayTeam</th>
                    <th colspan="3">Result</th>
                    </tr>
                   <? include 'file.php' ?>
                </table>

包含文件可以容纳任何你想要的东西。这包括您的 PHP 和 HTML 内容。它只会插入到您指定的位置。

您可以轻松地只使用 "for each" 和 "switch" 部分,如果这就是您在外部文件中想要的全部内容。

而不是修复我的示例,这非常好...我去查看了您的编辑,发现了您的错误所在:

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { 
                <? include '/models/switch_1.php' ?>
              ?>

您不能将其包含在现有块的正中间。您需要先终止该块,然后放入您的 include:

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>

                <? include '/models/switch_1.php' ?>

换句话说,将结束标记移至第一行。这样就可以了。