PHP包括外部swich不工作 [英] PHP include external swich not working
问题描述
我有以下代码段工作正常,但我希望开关开关($ fixture-> homeTeamName)和开关($ fixture-> awayTeamName)是包含在名为 swich_1.php 的外部文件中,但未在模板中进行硬编码,但我未能将其包括在内:
I have the following snippet which works fine, but I want the switches switch ($fixture->homeTeamName) and switch ($fixture->awayTeamName) to be included from external file named swich_1.php not hard coded in the template, but I failed to include it:
<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>- </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>- </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
here is the external file 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;
}
?>
我有空白屏幕,感谢您的帮助。
I got blank screen, your help is appreciated.
推荐答案
你这样做:
创建文件:file.php
Create the file: 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>- </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参考
Then, in your main page, replace the contents with the "include" reference
<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内容。它只会插入你要插入的地方。
The include file can hold anything you want. This includes your PHP and your HTML stuff. It will just be inserted where you tell it to be inserted.
你可以很容易地只使用for each和switch部分,如果全部的话你想要在外部文件中。
You could just as easily only use just the "for each" and "switch" portions, if that's all you want in the external file.
而不是修复我的例子,这非常好...... 我去看了你的编辑和发现你犯错的地方:
Rather than fix my example, which was pretty good... I went and looked at your edit and found where you made your mistake:
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) {
<? include '/models/switch_1.php' ?>
?>
您不能在现有区块的中间包含权利。您需要先终止该块,然后输入您的包含:
You cannot include right in the middle of an existing block. You need to terminate the block first, THEN put in your include:
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
<? include '/models/switch_1.php' ?>
换句话说,将结束标记移动到第一行。这样就可以了。
In other words, move where you have that closing tag up to the first line. That will do the trick.
这篇关于PHP包括外部swich不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!