Isabella Isabella - 4 months ago 11
HTML Question

PHP include external swich not working

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>-&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>


this is what I tried:

<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 } ?>


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.

Answer

You do it like this:

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>-&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 } ?>

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>

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.

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.