Jump to content

[SOLVED] How to get rid of "Warning:Divide by zero"


divadiva

Recommended Posts

Experts

 

I am trying to debug a page which is calculating salesMultiple(colc) of a product and it is  using some formula in it.When I run the page it shows me the right result but I get this

Warning: Division by zero in /var/www/html/ on line 495

 

Here is the piece of code

 

$rp = 1 ;
$rn = -1 ;
$widths[$rp] = array(20,20,20);
$results[$rp][++$rn] = array($reptitles[$rp] . ' - ' . $fstring,'','','');
$styles[$rp][$rn] = array('b');
$colspan[$rp][$rn] = array("$rn|0|$rn|2");
$results[$rp][++$rn] = array('','','');

$results[$rp][++$rn] = array('Tool','Days from Acquisition','Sales Multiple');

$styles[$rp][$rn] = array('b','b','b');
$results[$rp][++$rn] = array('','','');

// use same search results as report[0]
mysql_data_seek($query,0);

while($a_row = mysql_fetch_array($query)) {
	if($a_row['cost_basis'] > 0 && $a_row['purchase_date'] != '0000-00-00' && $a_row['purchase_date'] != '0000-00-00') {
		// must be cost basis greater than 0
		$colb = floor( ( strtotime( $a_row['order_date'] ) - strtotime( $a_row['purchase_date'] ) ) / ( 24 * 60 * 60 ) ) ;
		if( ( $colb > 0 ) && ( $a_row['contract_price'] > 0 ) ) {
			// must be days from acquisition greater than 0 and paid value
			$cola = $a_row['tool_id'];

			$colc =  round(( $a_row['contract_price'] - $a_row['commission'] - $a_row['crating'] - $a_row['shipping'] - $a_row['refurb_lab'] - $a_row['refurb_parts'] - $a_row['refurb_te'] - $a_row['refurb_other'] - $a_row['deinstall'] - $a_row['insurance'] - $a_row['loss_damage'] - $a_row['storage'] - $a_row['unrec_tax'] ) / $a_row['cost_basis'] , 2 ) ;

			$results[$rp][++$rn] = array($cola,$colb,$colc);
			$styles[$rp][$rn] = array('','n','d');

 

 

Please help me with this problem.I will be grateful to you.

 

Thanks in advance.

Go to line 495 and check which variable is a divisor. Then introduce a non-zero check for that variable before doing division.

 

 

I've got a feeling, that $a_row['cost_basis'] is empty (some problems with your query)

Thanks fo rreplying.I got what you saying but still not sure if I am on same page as you.

 

 

 

This is line where I get that warning: $sm = round( ( $r_bit[5] - $r_bit[7] ) / $r_bit[6] , 2 ) ;

I am not good with arrays.Do I need to set $sm to some variable?

 

Please see my code if possible:

 

$rp = 3 ;
$rn = -1 ;
$widths[$rp] = array(20,25,40,12,30,12,12,12,12,6,15,15);
$results[$rp][++$rn] = array($reptitles[$rp] . ' - ' . $fstring,'','','','','','','','','','','');
$styles[$rp][$rn] = array('b','','','','','','','','','','','');
$colspan[$rp][$rn] = array("$rn|0|$rn|3");
$results[$rp][++$rn] = array('','','','','','','','','','','','');

$results[$rp][++$rn] = array('Tool', 'Manufacturer', 'Description', 'Sales Lead', 'Customer', 'Contr Sales', 'Cost Basis', 'Refurb Etc.', 'Sales Mult', 'Fund', 'Status', 'Tech Control');
$styles[$rp][$rn] = array('b','b','b','b','b','b','b','b','b','b','b','b');

$results[$rp][++$rn] = array('','','','','','','','','','','','');

// use same search results as report[0]
mysql_data_seek($query,0);

// loop inventory
while($a_row = mysql_fetch_array($query)) {
	$id = $a_row[0];

	$refurb = $a_row['commission'] + $a_row['crating'] + $a_row['shipping'] + $a_row['refurb_lab'] + $a_row['refurb_parts'] + $a_row['refurb_te'] + $a_row['refurb_other'] + $a_row['deinstall'] + $a_row['insurance'] + $a_row['loss_damage'] + $a_row['storage'] + $a_row['unrec_tax'] ;

	if( $a_row['status_inv'] > 0 && $a_row['status_inv'] < 9 )  {
		// NOT unsold or closed ie. ongoing sales
		$r_arr[$id] = array($a_row['tool_id'], $a_row['manufacturer'], $a_row['model'], '', '', '', $a_row['cost_basis'], $refurb, '', $a_row['fund'], $a_row['status_desc'],'');
	}
	else {
		// unsold or closed
		$r_alt[$id] = array($a_row['tool_id'], $a_row['manufacturer'], $a_row['model'], '', '', '', $a_row['cost_basis'], $refurb, '', $a_row['fund'], $a_row['status_desc'],'');
	}
}

// quotes items not completed
$string1 = "SELECT * FROM `quotes_items` 
	LEFT JOIN `quotes_main` ON `quotes_items`.`quote_id` = `quotes_main`.`quote_id` 
	WHERE `quotes_main`.`status_quote` < 9 ";

// print $string1 ;

$query1 = mysql_query("$string1");

while($a_row = mysql_fetch_array($query1)) {
	$id = $a_row['inventory_id'];

	if( isset($r_arr[$id]) ) {

		// record already added
		$r_arr[$id] = array($r_arr[$id][0], $r_arr[$id][1], $r_arr[$id][2], $a_row['username'], $a_row['to_company'], $a_row['price_quoted'], $r_arr[$id][6], $r_arr[$id][7], '', $r_arr[$id][9], $r_arr[$id][10] ,$r_arr[$id][11]);

	}
	elseif( isset($r_alt[$id]) ) {

		// record not already added and exists in inventory search
		$r_arr[$id] = array($r_alt[$id][0], $r_alt[$id][1], $r_alt[$id][2], $a_row['username'], $a_row['to_company'], $a_row['price_quoted'], $r_alt[$id][6], $r_alt[$id][7], '', $r_alt[$id][9], $r_alt[$id][10] ,$r_alt[$id][11]);

	}
}

if(is_array($r_arr)) {
	asort($r_arr) ;
	// print_arr($r_arr);

	foreach ($r_arr as $r_key => $r_bit) {
		$sm = '' ;
		$smf = '' ;
		if($r_bit[6]) {
			$sm = round( ( $r_bit[5] - $r_bit[7] ) / $r_bit[6] , 2 ) ;   // This is where I get a warning
			$smf = '=((F' . ($rn + 2) . '-H' .($rn + 2) . ')/G' . ($rn + 2) . ')' ;
		}

		$results[$rp][++$rn] = array($r_bit[0], $r_bit[1], $r_bit[2], $r_bit[3], $r_bit[4], $r_bit[5], $r_bit[6], $r_bit[7], $sm, $r_bit[9], $r_bit[10], $r_bit[11]);

		$styles[$rp][$rn] = array($r_key,'','','','','n','n','n','d','','','') ;
		$formula[$rp][$rn] = array('','','','','','','','',$smf,'','','') ;
	}
}

 

 

You must make sure, that $r_bit[6] is not zero before you do a division.

 

I'm a bit confused, because if($r_bit[6]) should take care of that...

 

You can change

if($r_bit[6]) {

 

to

if($r_bit[6] != 0) {

and see what happens...

Thanks for replying once again.I have already tried that .It takes away the warning .But messes up with the result .

 

Can I skip the value of $sm variable?

 

Somthing like:

if($r_bit[6]!=0 ) {
			$sm = round( ( $r_bit[5] - $r_bit[7] ) / $r_bit[6] , 2 ) ;
			$smf = '=((F' . ($rn + 2) . '-H' .($rn + 2) . ')/G' . ($rn + 2) . ')' ;
		}
		else
			{
			//Here I Want to skip this $ sm value
			$sm = round( ( $r_bit[5] - $r_bit[7] ) / $r_bit[6] , 2 ) ;

			}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.