警告:在485 496处除以零

I get this error:

Warning: Division by zero in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\B2\seats.php on line 485

Warning: Division by zero in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\B2\seats.php on line 496

php code like this:

for ($i = 0; $i < $num_rows; $i++) {
  $row = mysql_fetch_array($result);
  if ($i % $columns == 0) {                 // line 485

  }
  if ($row['status']=='0') {    
    echo "<td><input type='checkbox' name='service[]' id='services' value='$row[rowid]' />&nbsp;&nbsp;".$row['rowid']."</td>";
  } else {  
    echo "<td>".$row['rowid'].".Sudah di pesan</td>";
  }

  if (($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //line 496
    echo "
</TR>";
  }
}

Please help how I can fix this?

In accordance to what @Brian-Ray said, your columns are 0. The modulus operator (%) performs a division operation (To determine the "remainder" value). If this encounters a division by 0, then this error occurs.

A common fix for this is to check for 0, and short circuit the logic in your IF statement. This would look like:

 if( ($columns != 0) and ($i % $columns == 0) )

Since the AND (&&) logic operator is used, when the if statement encounters the first set of parenthesis (columns not equal to zero) and fails, it will not attempt to execute the if statement any further (since the outcome cannot possible be true, with an AND operator.

To fix your error on line 496 you will need to perform the same. You can find more information about if statement short circuiting here:

https://en.wikipedia.org/wiki/Short-circuit_evaluation

To summarise the problem:

  • Your error says "Division by zero ... on line 485".
  • On line 485 is a division $i % columns.

This implies that columns is zero at this point and you're dividing by it.

To fix the problem, you need to decide:

  • Should the program be allowed to reach line 485 with columns set to zero?
    • No: Find out why this situation was reached and stop that from happening.
    • Yes: What should the program do when it gets to this point with columns set to zero?
      • Make the program do that instead of what it's currently doing.