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]' /> ".$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:
To summarise the problem:
$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:
columns
set to zero?columns
set to zero?