Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I tried this code to display MySQL data into coded HTML table via PHP:

<?php
    // Get all the data from the "login" table
    $result = mysql_query("SELECT * FROM login") or die(mysql_error());

    echo '<table class="table">
          <thead>
              <tr>
                  <th>FirstName</th> 
                  <th>LastName</th> 
                  <th>user</th> 
              </tr>
          </thead>
          <tbody>';

    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        **if( $i % 2 == 0 )** {  //line 20
            $class = " class='odd'";
        }
        else {
            $class = "";
        }
        // Print out the contents of each row into a table
        echo "<tr" . $class . "><td>"; 
        echo $row['FirstName'];
        echo "</td><td>"; 
        echo $row['LastName'];
        echo "</td><td>"; 
        echo $row['user'];
        echo "</td><td>";  
    }
    echo "</tbody></table>";
?>

But it shows me this error: Undefined variable: i in line 20 The line 20 error text is marked as bold in above code.

share|improve this question

closed as too localized by cryptic ツ, SztupY, PeeHaa, tereško, Baba Feb 25 at 22:18

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 1 down vote accepted

Try this

 $i=0;
 while($row = mysql_fetch_array( $result )) {
    if( $i % 2 == 0 ) {
        $class = " class='odd'";
    } else {
        $class = "";

    }
    // Print out the contents of each row into a table
    echo "<tr" . $class . "><td>"; 
    echo $row['FirstName'];
    echo "</td><td>"; 
    echo $row['LastName'];
    echo "</td><td>"; 
    echo $row['user'];
    echo "</td><td>";  
  $i++;
} 
share|improve this answer
1  
Thanks Vims, thank you very very very much – Entire Ict Feb 24 at 7:41

add $i=0; before the while loop starts, and also increment it $i++ before while loop ends.

$i = 0;

while($row = mysql_fetch_array( $result )) {

//your code

  $i++;
}
share|improve this answer
Thank you Vora, thanks very very much – Entire Ict Feb 24 at 7:38
Accept an answer that solved your problem by ticking the mark under the score – codingbiz Feb 24 at 7:57

Add

$i = 0;

before the while where you are getting the error and use $i++ on you evaluation

share|improve this answer
Thanks a lot ares – Entire Ict Feb 24 at 7:40

You were never incrementing your $i

$i = 0; 

while($row = mysql_fetch_array( $result )) {
  if($i % 2 == 0)
  ....

  $i++;  //You need to increment your $i
}
share|improve this answer
Thanks for answer, thanks a lot – Entire Ict Feb 24 at 7:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.