开发者

Mixing ordered lists, links and variables in PHP/HTML

I'm taking a break from Project Euler to learn some PHP/HTML for kicks and giggles, and I found a page of simple exercises. So, on my 'site,' I want to have an ordered list of links to pages of each of the exercises, but I decided to do it in a dynamic manner as opposed to hard coding each item as I do the exercise. Unfortunately, the page that should contain the list doesn't render at all!

Assuming I have files on my system with the names "exawk#.php," what else could be wrong with this code? Sorry if it is sloppy or horrible, it's literally my first day of web programming.

<html>
  <head>

    <title> Awaken's Exercises </title>

  </head>

  <body>

    <h1>This page contains "Awaken's Exercises" from
    <a href="http://forums.digitalpoint.com/showthread.php?t=642480">
    this page</a>.</h1>

    <ol>
    <?php
      $arex = glob("exawk*.php"); // $arex contains
                                //an array of matching files
      $numex = 0;
      $i = 0;
      foreach( $arex )
      {
        $numex++;
      }

      while( $numex >= 0 )
 开发者_StackOverflow     {
        echo "<li><a href=" .$arex[$i].
             ">Problem #" .$numex. ".</a></li>";
        $numex--;
        $i++;
      }

    ?>
    </ol>

  </body>

</html>


Enable display_errors in php.ini: foreach( $arex ) is a syntax error (missing .. as $varname).

From the commandline, you could check it with php -l /path/to/your/file.php.

Also, this sample:

  //an array of matching files
  $numex = 0;
  foreach( $arex as $youdontdoanythingwiththis)
  {
    $numex++;
  }

Could be:

 $numex = count($arex);

Better the whole thing:

while( $numex >= 0 )
{ ...etc

Could be:

$num = 1;
foreach($arex as $file){
    echo '<li><a href="'.$file.'">Problem #'.$num.'</a></li>';
    $num++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜