我有以下代码段工作正常,但我想要开关开关($ fixture-> homeTeamName)和开关($ 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>- </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
<?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;
}
?>
我有空白屏幕,感谢您的帮助。
答案 0 :(得分:2)
你这样做:
创建文件: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”参考
替换内容<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' ?>
换句话说,将结束标记移动到第一行。这样就可以了。