PHP - Division By Zero
Well, I am currently working on a Kill/Death Table when the players insert there kills and deaths along with a screenshot of proof.
I have got most of it down except for a Warning: Division by zero in /home/a2186214/public_html/table/index.php on line 43 error. This is the code I am using to calculate the kill/deaths: Code: [Select] if ($row['kills'] >= 1) { $kdr = $row['kill']/$row['death']; } elseif ($row['death'] >= 1) { $kdr = $row['kill']/$row['death']; } else $kdr = '0';How would I fix this? Similar TutorialsSo, this is something that I've been wondering for a while now... say you have two variables that you need to divide to find a certain percentage and one happens to be zero... why does PHP feel the need to return a warning saying there was a division by zero? why not just return the number zero since anything that's divided by zero always results in zero anyway? Just a random thought. hey guys i'm trying out this ajax rating script and i get this division by zero: Warning: Division by zero in /home/open2sha/public_html/Rate/index.php on line 37 <?php // very quick & dirty code $sql = mysql_query("SELECT * FROM tut_starRating"); $rows = mysql_num_rows($sql); if ($rows == 0) { echo "no rows found"; } else { while($row = mysql_fetch_array($sql)) { $tmp_rating = $row['rating'] / $row['votes']; $rating = round(($tmp_rating*2), 0)/2; // round up or down to nearest half print("<li><div class='rating' id='rating_".$row['articleId']."'>".$rating."</div><div id='starRatingFeedback_".$row['articleId']."'></div></li>"); } } ?> </ol> if($ns_cb->targetCurrency == 'USD') { $usd_base_rate = '1'/$ns_cb->value; } $eq = number_format($usd_base_rate*$ns_cb->value,2); echo $eq $ns_cb->value is currently = 0.9940. But this gets updated every day. I keep getting division by zero for this problem here, any help? Hi can any body give me an idea why i am receiving the following errors from my script? Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 29 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 44 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 29 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 44 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 29 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 44 Warning: Division by zero in C:\xampp\htdocs\Practice\page.php on line 50 script is: <?php include_once "include/globals.php"; if (isset($_GET['cat-id'])) { $id = $_GET['cat-id']; } ?> <?php //Get database Results $result = mysql_query("SELECT * FROM Products WHERE is_active = 1 AND catagory_id = $id ") or die(mysql_error()); if(mysql_num_rows($result)===0) { $output = "No records found.\n"; } else { $output = "<table>\n"; //keeps getting the next row until no more records $recNo = 0; while($row = mysql_fetch_array($result)) { $recNo++; //Start new row when needed if($recNo%$max_columns==1) { $output .= "<tr>\n"; } //Create TD for record $output .= "<td><div class=\"table-bg\">"; $output .= "<div class=\"title\">{$row['Name']}</div>"; $output .= "<div class=\"image\">"; $output .= "<a href=\"product_detail.php?id=\"><img src=\"{$row['image']}\" width=\"100\" alt=\"\" border=\"0\"></a>"; $output .= "</div>"; $output .= "<div class=\"tag_line\">{$row['Tag_Line']}</div>"; $output .= "<div class=\"price\">Now Only: £{$row['Website_Price']}</div>"; $output .= "<div class=\"prod-footer\"><a href=\"product_detail.php?id={$row['ID']}\">more info</a></div>"; $output .= "</div></td>\n"; //Close row when needed if($recNo%$max_columns==0) { $output .= "</tr>\n"; } } //Close final row if needed if($recNo%$max_columns!=0) { $output .= "</tr>\n"; } $output .= "<table>\n"; } ?> <div id="main"> <?php echo $output; ?> </div> <div class="table-bg"></div> I am getting Warning: Division by zero in /rate.php on line 64 <?php function rateinstar($value) { preg_match_all('#([0-9]+)([0.0-9.9]+)|([0-9]+)#',$value,$tek); if($tek[0][1] > 0) { $rate=$tek[0][0]/$tek[0][1]; } else { $rate=0; } preg_match_all('#([0-9]+).([0-9]+)#',round($rate,1),$decm); if($decm[2][0] != "") { $no=($decm[1][0]*2)+1; for($i=1;$i<=10;$i++) { if($i<=$no){ if($i%2!=0) echo '<span class="starin piece1"></span>'; else echo '<span class="starin piece2"></span>'; }else{ if($i%2!=0) echo '<span class="starin piece3"></span>'; else echo '<span class="starin piece4"></span>'; } } }else{ for($i=1;$i<=10;$i++) { if($i<=$rate*2){ if($i%2!=0) echo '<span class="starin piece1"></span>'; else echo '<span class="starin piece2"></span>'; }else{ if($i%2!=0) echo '<span class="starin piece3"></span>'; else echo '<span class="starin piece4"></span>'; } } } } function rateintext($value) { preg_match_all('#([0-9]+)([0.0-9.9]+)|([0-9]+)#',$value,$tek); $rate=$tek[0][0]/$tek[0][1]; preg_match_all('#([0-9]+).([0-9]+)#',round($rate,1),$decm); return round($rate,1); } ?> Hi guys I get this warning when testing out code in xampp Warning: Division by zero in C:\xampp\htdocs then it continues; Warning: Division by zero in C:\xampp\htdocs\TFN\index.php on line 25 Warning: include(C:\xampp\php\PEAR\PHP) [function.include]: failed to open stream: Permission denied in C:\xampp\htdocs\TFN\index.php on line 25 Warning: include() [function.include]: Failed opening 'php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\TFN\index.php on line 25 Warning: Division by zero in C:\xampp\htdocs\TFN\index.php on line 27 Warning: include(C:\xampp\php\PEAR\PHP) [function.include]: failed to open stream: Permission denied in C:\xampp\htdocs\TFN\index.php on line 27 Warning: include() [function.include]: Failed opening 'php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\TFN\index.php on line 27 ...... I have used the search function but all the results are similar but i cant find one that fits/ solves my issue hence the new thread, so apologies in advance. Please note im trying to get to grips with php and i havent done any wed design in 10 yrs or so, so bear with me if i seem idiotic at times lol i followed this basic tutorial from he http://www.1stwebdesigner.com/css/how-to-create-php-website-template/ Then was going to edit / add as needed but after getting to the final stage and testing it... The above error appeared. so u can see what i see here is a link to my site: http://tfn.site11.com/ here is my folder/file structure root css folder/ css file includes folder/ footer.php/header.php/nav.php/sidebar.php variables folder/ variables.php index.php If you need to know anything else just ask.. Thanks for your time Code: [Select] <?php class Point { public $x; public $y; } // Random values 0 - 1 function random_float ($min,$max) { return ($min+lcg_value()*(abs($max-$min))); } // Fuzzy C Means Algorithm function distributeOverMatrixU($num_of_clusters, $arr,$m) { global $MatrixCentroids; $num_of_data = sizeof($arr); $MatrixPointX = $arr; // Fill the $MatrixU table with random values from 0 to 1 for($j = 0; $j < $num_of_data; $j++) { $sum = 0; for($i = 0; $i < $num_of_clusters; $i++) { $MatrixU[$j][$i] = random_float(0,1); $sum += $MatrixU[$j][$i]; } // Normalize Data for($i = 0; $i < $num_of_clusters; $i++) { $MatrixU[$j][$i] = $MatrixU[$j][$i]/$sum; } } // repeat 200 for($a = 0; $a<200; $a++) { for($i = 0; $i < $num_of_clusters; $i++) { $tempAx = 0; $tempBx = 0; $tempAy = 0; $tempBy = 0; for($j = 0 ; $j < $num_of_data; $j++) { $tempAx = $tempAx + pow($MatrixU[$j][$i],$m); $tempBx += pow($MatrixU[$j][$i],$m) * $MatrixPointX[$j]->x; $tempAy = $tempAy + pow($MatrixU[$j][$i],$m); $tempBy += pow($MatrixU[$j][$i],$m) * $MatrixPointX[$j]->y; } $MatrixCentroids[$i] = new Point(); $MatrixCentroids[$i]->x = $tempBx / $tempAx; $MatrixCentroids[$i]->y = $tempBy / $tempAy; } // Cluster Centers for($j = 0 ; $j < $num_of_data; $j++) { $tempSum = 0; for($i = 0; $i < $num_of_clusters; $i++) { // Distance between 2 points $distance1 = pow(($MatrixPointX[$j]->x - $MatrixCentroids[$i]->x),2); $distance2 = pow(($MatrixPointX[$j]->y - $MatrixCentroids[$i]->y),2); $distance = $distance1 + $distance2; $distance = sqrt($distance); $MatrixU[$j][$i] = pow(1/$distance , 2/($m-1)); $tempSum += pow(1/$distance, 2/($m-1)); } for($i = 0; $i < $num_of_clusters; $i++) { $MatrixU[$j][$i] = ($MatrixU[$j][$i]/$tempSum); } $sum = 0; for($i = 0; $i < $num_of_clusters; $i++) { $sum += $MatrixU[$j][$i]; } // Norm Data for($i = 0; $i < $num_of_clusters; $i++) { $MatrixU[$j][$i] = $MatrixU[$j][$i]/$sum; } } } //print_r($MatrixCentroids); return $MatrixU ; } // Connect to DB $my_num = $_GET["cluster_number"]; $fuzz = $_GET["fuzz"]; require('db_connection.php'); $resultx=mysql_query("SELECT * FROM xdata"); $result=mysql_query("SELECT * FROM ydata"); // &#916;&#953;&#945;&#946;&#940;&#950;&#959;&#965;&#956;&#949; &#972;&#955;&#945; &#964;&#945; &#948;&#949;&#948;&#959;&#956;&#941;&#957;&#945; while ($a_row = mysql_fetch_array($result)) { for($j = 1 ; $j < 17; $j++) { $tmp_data[$j] = $a_row[ay.$j]; } } while ($x_row = mysql_fetch_array($resultx)) { for($j = 1; $j <17; $j++) { $p = new Point(); $p->x = $x_row[ax.$j]; $p->y = $tmp_data[$j]; $arr[] = $p; } } // Execute algorithm $data_in_clusters = distributeOverMatrixU($my_num, $arr,$fuzz); $size = sizeof($arr); $result=@mysql_query('select * from cluster0 limit 1', $link); if ($result) { echo "O &#960;&#943;&#957;&#945;&#954;&#945;&#962; &#965;&#960;&#940;&#961;&#967;&#949;&#953; &#954;&#945;&#953; &#960;&#961;&#941;&#960;&#949;&#953; &#957;&#945; &#948;&#953;&#945;&#947;&#961;&#945;&#966;&#959;&#973;&#957; &#959;&#953; &#960;&#961;&#959;&#951;&#947;&#959;&#973;&#956;&#949;&#957;&#949;&#962; &#949;&#947;&#947;&#961;&#945;&#966;&#941;&#962;...<BR>"; echo "&#916;&#953;&#945;&#947;&#961;&#945;&#966;&#942;...<BR>"; for($i = 0; $i <100; $i++) { $query="DROP TABLE cluster$i"; mysql_query($query,$link); } echo "H &#948;&#953;&#945;&#947;&#961;&#945;&#966;&#942; &#941;&#947;&#953;&#957;&#949; &#956;&#949; &#949;&#960;&#953;&#964;&#965;&#967;&#943;&#945;..."; } for($j = 0; $j < $my_num; $j++) { $query_c = "CREATE TABLE cluster$j(data_points_x FLOAT NOT NULL,data_points_y FLOAT NOT NULL)"; mysql_query($query_c,$link); } for($i = 0; $i < $size ; $i++) { $max = 0; $pos = 0; for($j = 0; $j < $my_num; $j++) { $tmp_point = $data_in_clusters[$i][$j]; if($tmp_point > $max) { $max = $tmp_point; $pos = $j; } } $xxxx = $arr[$i]->x; $yyyy = $arr[$i]->y; $query="INSERT INTO cluster$pos(data_points_x,data_points_y) values('$xxxx','$yyyy')";; mysql_query($query,$link); } echo "<br>"; echo "<img src='db_scatter.php' border=0 align=center width =600 height=400>"; // &#917;&#956;&#966;&#940;&#957;&#953;&#963;&#951; &#928;&#943;&#957;&#945;&#954;&#945; &#956;&#949; &#964;&#945; &#945;&#960;&#959;&#964;&#949;&#955;&#941;&#963;&#956;&#945;&#964;&#945; echo "<table width='500' border='2'>"; echo "<TR><TD><B>Points</B><BR></TD>"; for($i = 0; $i < $my_num; $i++){ $xxx = $MatrixCentroids[$i]->x; $yyy = $MatrixCentroids[$i]->y; echo "<TD>Cluster $i Centoid:<br><b>X:$xxx Y:$yyy</b></TD>"; } echo "</tr>"; for($i = 0; $i < $size ; $i++) { $x = $arr[$i]->x; $y = $arr[$i]->y; echo "<TR>"; echo "<TD><b>x:$x</b><br><b>y:$y</b></TD>"; for($j = 0; $j < $my_num; $j++) { $tmp_point = $data_in_clusters[$i][$j]; echo "<TD> $tmp_point</TD>"; } echo "</TR>"; } echo "</TABLE>"; ?> I am adding line items in my invoice script, while adding i validate the data. if validation fails, it will display error message with entered data filled in the form. If it clears the validation, data gets submitted to database and displays the same . Here i can add many line items, so this process should keep repeating. Everything is working fine. But when the form submits it should display error in one place and display the submitted data in other place. Here is my form
<form action="" method="post"> <div class="form-row"> <div class="col-md-4 mb-30"> <label for="validationDefault01">Select Customer</label> <select name="customer" class="form-control" id="validationDefault01" required> <option value=""></option> <?php $c1 = mysqli_query($con, "SELECT * FROM customers WHERE status='Active'") or die (mysqli_error($con)); while($c2 = mysqli_fetch_array($c1)) { ?> <option value="<?php echo $c2["cid"]; ?>" <?php if($c2["cid"] == $_POST['customer'] ) { echo "selected"; } ?> ><?php echo $c2["name"]; ?></option> <?php } ?> </select> </div> <div class="col-md-4 mb-30"> <label for="validationDefault02">Date</label> <input type="text" class="form-control" name="edate" id="datepicker" value="<?php echo isset($_POST["edate"]) ? $_POST["edate"] : $today; ?>" required /> </div> </div> <!-- line item --> <div class="table-responsive"> <table class="table table-active table-bordered table-sm"> <thead class="thead-active"> <tr> <th>Name</th> <th>Description</th> <th>UOM</th> <th>Price</th> <th>Stock</th> <th>Qty</th> </tr> </thead> <tr> <td><input type="text" id="productname" name="productname" value="<?php echo isset($_POST["productname"]) ? $_POST["productname"] : ''; ?>" required ></td> <input type="hidden" id="productcode" name="productcode" value="<?php echo isset($_POST["productcode"]) ? $_POST["productcode"] : ''; ?>" /> <td><textarea id="description" name="description"><?php echo isset($_POST["description"]) ? $_POST["description"] : ''; ?></textarea></td> <td><select name="uom" id="uom"> <?php $su1 = mysqli_query($con, "select * from uom"); while($su2 = mysqli_fetch_array($su1)) { ?> <option value="<?php echo $su2["uom_name"]; ?>" <?php if($su2["uom_name"] == $_POST['uom'] ) { echo "selected"; } ?> ><?php echo $su2["uom_name"]; ?></option> <?php } ?> </select> </td> <td><input type="text" required id="price" name="price" value="<?php echo isset($_POST["price"]) ? $_POST["price"] : ''; ?>" /></td> <td><input type="text" readonly id="stock" name="stock" value="<?php echo isset($_POST["stock"]) ? $_POST["stock"] : ''; ?>" /></td> <td><input type="text" required id="quantity" name="quantity" value="<?php echo isset($_POST["quantity"]) ? $_POST["quantity"] : ''; ?>" /></td> </tr> </table> <!-- line item ends---> <div class="form-row"> <div class="col-md-4 mb-30"> <input name="add" class="btn btn-success" type="submit" value="Add" /> </div> </div> </form> form submission <?php if(isset($_POST['add'])) { $customer = $_POST['customer']; $edate1 = $_POST['edate']; $edate = date('Y-m-d', strtotime((str_replace('/','-',$edate1)))); $pname = $_POST['productname']; $pcode = $_POST['productcode']; $uom = $_POST['uom']; $price = $_POST['price']; $quantity = $_POST['quantity']; $pc = mysqli_query($con, "SELECT min_price FROM items WHERE item_id=".$pcode."") or die (mysqli_error($con)); $prow = mysqli_fetch_array($pc); // This error part should be displayed inside <div id="error"></div> which is above the form if($price<$prow['min_price']) { echo '<div class="alert alert-inv alert-inv-danger alert-wth-icon alert-dismissible fade show" role="alert"> <span class="alert-icon-wrap"><i class="zmdi zmdi-bug"></i></span> Price should not be lesser than minimum price defined. <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div>'; } else { //data gets inserted into invoice table and displays submited data in table format // this part should be displayed below the form inside <div id="success"></div> } }
Hello, I have links at above of the page. if i click a link a new page will appear. The menu links above are same for all pages. Which is better reloading the whole page with all menu links or keeping the above menu link permanent/unchange reloading bottom division with ajax? I mean only one page will contain the menu.Is it better? Or every page will contain the menu above.Is it better then the first one? I say many web site they have link menus above if i click a link from above it seems the whole page reloads or chages. For example:http://www.sitepoint.com/ Thank you. |