PHP Warning: Division by zero error
I getting this error and not sure how to fix it. Line 162 is the first line in the php code. I looked at some of the related questions and all my quote marks seem to be in the right place
Per page:
<input type=text name=per_page value="<?php echo $en['per_page']; ?>" size=6>
Go to page:
<select name=page size=1>
<?php
$pages = ceil(sql_num_rows($result)/$en['per_page']);
for ($k=1;$k<=$pages;$k++)
echo开发者_如何学JAVA '<option value='.$k.($en[page] == $k ? ' selected':'').'>'.
$k.'</option>';
echo '</select>';
if ($en[page]<$pages)
echo '<input type=submit name=next value="Next page">';
?>
Try to echo $en['per_page']
and make sure it's got the correct value. (I bet it doesnt).
Unrelated: $en[page]
Should probably be $en['page']
(assuming page is really not a constant)
What's the value of $en['per_page']
(I'm guessing zero)
Because you cannot have a denominator value of 0.
Check the value of $en['per_page'] > 0 prior to applying it to a division.
Any time you divide by a variable, if the variable is 0 you will get that error.
The way to prevent it would be to check to ensure that $en['per_page'] is not zero, since that appears to be your only division and is probably where your problem is.
If you're getting a divide by zero error, then $en['per_page']
may not be correct. It's probably a zero value.
Also, all your HTML attributes need to be in quotes.
For example:
<input type=text name=per_page
Should be:
<input type="text" name="per_page"
Your array indexes should be quoted too.
$en[page]
Should be:
$en['page']
Here is the solution:
Per page:
<input type=text name=per_page value="<?php echo $en['per_page']; ?>" size=6>
Go to page:
<?php
$pages = @ceil(sql_num_rows($result)/$en['per_page']);
for ($k=1;$k<=$pages;$k++)
echo '<option value='.$k.($en[page] == $k ? ' selected':'').'>'.
$k.'</option>';
echo '</select>';
if ($en[page]<$pages)
echo '<input type=submit name=next value="Next page">';
?>
Just used @ before ceil / division.
精彩评论