-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

5
  • 6
    change to $x < $max. Commented Jun 3, 2014 at 18:52
  • Check this out: nl3.php.net/fgetcsv Commented Jun 3, 2014 at 18:55
  • 1
    change for ($x=0; $x<=$max; $x++) to for ($x=0; $x < $max; $x++) Commented Jun 3, 2014 at 19:00
  • I don't know why this question is marked as duplicate with the referenced question. there is no resemblance except for the title Commented Jun 4, 2014 at 4:26
  • This Question has a specific code I dont Understand why you have marked this question as duplicate.I request you to remove duplicate mark.Thanks Commented Jun 4, 2014 at 16:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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>";
} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.