Foreach上的PHP语法错误

时间:2016-12-06 04:36:36

标签: php

我已尝试使用此网站的代码:https://www.ibm.com/developerworks/library/x-buildpdfphp/进行测试。但是,在php页面上发现了语法错误。这是代码: 我已将'语法错误'放在错误所在的位置。

<html><head><title>Event Results</title></head>
<body>
<?php
include_once ('XML.php');
$results = getResults();
foreach( $results as $event ) {
?>
<h1><?php echo( $event['name']) ?></h1>
<table><tbody>
<?php
foreach( $event['games'] as $game ) {
  $s1 = (int) $game['score1'];  <-- Syntax Error Here -->
  $s2 = (int) $game['score2'];  <-- Syntax Error Here -->
?>
<tr>
  <td style="font-weight:<?php echo( ( $s1 > $s2 ) ? 'bold' : 'normal') ?>">
    <?php echo( $game['team1'] ) ?></td>
  <td><?php echo( $s1 ) ?></td>
  <td style="font-weight:<?php echo( ( $s2 > $s1 ) ? 'bold' : 'normal') ?>">
    <?php echo( $game['team2'] ) ?></td>
  <td><?php echo( $s2 ) ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</body></html>

1 个答案:

答案 0 :(得分:1)

解决方案:删除第二个foreach。

注意:删除第一行的注释并删除模型。

<html><head><title>Event Results</title></head>
<body>
<?php
//include_once ('XML.php');
//$results = getResults();

// mockup
$results = array(
                    0=>array(
                                    "name"=>"Basket",
                                    "games"=>array(
                                                    "score1"=>"70",
                                                    "score2"=>"90",
                                                    "team1"=>"Los Supremos",
                                                    "team2"=>"Rapid Fox"
                                                    )
                                   ),    
                    1=>array(
                                    "name"=>"Baseball",
                                    "games"=>array(
                                                    "score1"=>"20",
                                                    "score2"=>"6",
                                                    "team1"=>"Girls Bats",
                                                    "team2"=>"Powergirls"
                                                    )
                                   ),                                       
                );

//var_dump($results);
//die();


foreach( $results as $event ) 
{
    ?>
    <h1><?php echo( $event['name']) ?></h1>
    <table><tbody>
    <?php
    $game = $event['games'];

      $s1 = $game['score1'];  //<-- Syntax Error Here -->
      $s2 = $game['score2'];  //<-- Syntax Error Here -->
    ?>
    <tr>
      <td style="font-weight:<?php echo( ( $s1 > $s2 ) ? 'bold' : 'normal') ?>">
        <?php echo( $game['team1'] ) ?></td>
      <td><?php echo( $s1 ) ?></td>
      <td style="font-weight:<?php echo( ( $s2 > $s1 ) ? 'bold' : 'normal') ?>">
        <?php echo( $game['team2'] ) ?></td>
      <td><?php echo( $s2 ) ?></td>
    </tr>
    </tbody>
    </table>    
    <?php
}
?>

</body></html>