-1

Hi I am getting Undefined Offset for the below code.

            <?php
    function readCSV($fileName) {
      $rows = array();
      $rows = file($fileName);
      $max = sizeof($rows);
          for ($x=0; $x<=$max; $x++) {
      echo "<li> <a href='#".$rows[$x]."'>$rows[$x]</a> </li>";

} 

      return $rows;
}   

?>

  <span><b>Available Positions: </b></span>
  <ul>
 <? (readCSV('joinUs.csv');?>
  </ul>

Please let me know where I am making mistake in the above code.

Thanks

Rahul Jain
  • 115
  • 3
  • 12

2 Answers2

0

You are accessing a key which has not been set in your for loop.

for ($x=0; $x < $max; $x++) {
   echo "<li> <a href='#".$rows[$x]."'>$rows[$x]</a> </li>";
} 
Nick
  • 2,862
  • 5
  • 34
  • 68
0

change

 for ($x=0; $x<=$max; $x++) 

to

for ($x=0; $x < $max; $x++) 

should work like maska.. :)

since the count starts from 0 the for loop should be used with a condition of 1 less of your max condition. A simple counting funda.

dhpratik
  • 1,100
  • 4
  • 19
  • 43