tags:

views:

13

answers:

1

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"&gt;
    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 )
      {
        echo "<li><a href=" .$arex[$i].
             ">Problem #" .$numex. ".</a></li>";
        $numex--;
        $i++;
      }

    ?>
    </ol>

  </body>

</html>
+1  A: 

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++;
}
Wrikken
Ahh, there is a length function for arrays! I was looking for it under a different name. Thank you!
Andrew