PHP - Maths Difficulties
Hi if ive made a variable how do i tell it to be less than or equal to another variable ?
currently im using $lowhigh_price = ( <= 0.75 * $av_price); but it tells me there is something wrong hlep please Similar TutorialsHi, I am using PHP to perform some area calculations. I have created this form to collect the values: Code: [Select] <form name="form1" method="post" action="<?php htmlentities($_SERVER['PHP_SELF']); ?>"> <table width="100%" border="0" cellspacing="0" cellpadding="8"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr> <td bgcolor="#666666"><table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td align="center" bgcolor="#3471C6" class="fondoHogartec"><?=CNT_TXT_CALCULATORTITLE?></td> </tr> <tr> <td align="center" bgcolor="#EEEEDD"><label> <span class="CarritoProducto"><?=CNT_TXT_WATERLINE?><br> </span> <input name="waterline" type="text" class="CalculatorFieldTXT" id="waterline" value="<?php echo((isset($_POST["waterline"]))?$_POST["waterline"]:"") ?>"> </label></td> </tr> <tr> <td align="center" bgcolor="#EEEEDD"><label> <span class="CarritoProducto"><?=CNT_TXT_BEAM?></span><br> <input name="beam" type="text" class="CalculatorFieldTXT" id="beam" value="<?php echo((isset($_POST["beam"]))?$_POST["beam"]:"") ?>"> </label></td> </tr> <tr> <td align="center" bgcolor="#EEEEDD"><label> <span class="CarritoProducto"><?=CNT_TXT_DRAUGHT?></span><br> <input name="draught" type="text" class="CalculatorFieldTXT" id="draught" value="<?php echo((isset($_POST["draught"]))?$_POST["draught"]:"") ?>"> </label></td> </tr> <tr> <td align="center" bgcolor="#EEEEDD"><label> <span class="CarritoPrecio"><?=CNT_TXT_SURFACE?><br> </span> <input name="surface" type="text" class="CalculatorFieldTXT" id="surface" value="<?php echo $total_surface ?>" size="10" > </label></td> </tr> <tr> <td align="center" nowrap bgcolor="#EEEEDD"><label class="CarritoPrecio"><?=CNT_TXT_TOTALCANS?><br> </label> <input name="cans_needed_2coats" type="text" class="CalculatorFieldTXT" id="cans_needed_2coats" value="<?php echo $total_cans ?>"> <br></td> </tr> <tr> <td align="center" nowrap bgcolor="#EEEEDD"><table width="100%" border="0" cellspacing="0" cellpadding="2"> <tr> <td colspan="3" align="center" class="TitulosTXT2"><?=CNT_TXT_KEELTYPE?></td> </tr> <tr> <td align="center" class="TitulosTXT2"><label> <input type="submit" name="keel_type" id="keel_type" value="<?=CNT_TXT_LONG?>"> </label></td> <td align="center"><input type="submit" name="keel_type" id="keel_type" value="<?=CNT_TXT_BILGE?>"></td> <td align="center"><input type="submit" name="keel_type" id="keel_type" value="<?=CNT_TXT_FIN?>"></td> </form>and this is the PHP to perform the calculations: Code: [Select] // INICIATE CALCULATOR $keeltype = $_POST["keel_type"]; $waterline_length = $_POST["waterline"]; $beam = $_POST["beam"]; $draught = $_POST["draught"]; if ($keeltype == CNT_TXT_LONG) { $total_surface = $waterline_length*($beam+$draught) ; } if ($keeltype == CNT_TXT_BILGE) { $total_surface = ($waterline_length*($beam+$draught))* 0.75 ; } if ($keeltype == CNT_TXT_FIN) { $total_surface = ($waterline_length*($beam+$draught))* 0.50 ; } $total_cans = $total_surface / 25 ; // END CALCULATOR It works fine when the values entered in the form contain decimals separated by "." but it doesn't calculate properly when the decimals are separated by a comma. Please help. Thanks. Hi All, I am trying to work out some simple maths between 2 numbers, that are returned in variables - say $v1 and $v2. $v2 = 126 and $v1 = 318 I can do the working out - $total = $v2 / $v1 * 100 However is there any way that I can set total to only display the first 2 numbers? Just that I am getting a long result cause of the decimal places. (39.62264150943396..........) Can it be done? Thanks Matt I've been at this now for a while but I'm struggling to understand this code. I understand the bigger picture of what the code is doing but I can't seem to graps the logic behind the code and the steps it's taking. Here is part of the code:
class Validate{ private $_passed = false, $_errors = array(), $_db = null; if($rule === 'required' && empty($value)){ $this->addError("{$item} is required"); } else if(!empty($value)){ switch($rule){ case 'min': if(strlen($value) < $rule_value){ $this->addError("{$item} must be a minimum of {$rule_value}"); } private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; }I'm struggling to understand how the error methods and properties are being used. I can't see what is being used within the class or outside of it. Here is the code outside the class: $validate = new Validate();{ $validate = new Validate(); $validation = $validate->check($_POST, array( 'username' => array( 'required' => true, 'min' => 2, 'max' => 20, 'unique' => 'users' ), 'password' => array( 'required' => true, 'min' => 6 ), 'password_again' => array( 'required'=> true, 'matches'=> 'password' ), 'name' => array( 'required' => true, 'min' => 2, 'max' => 50 ) )); if($validation->passed()){ echo 'passed'; } else{ foreach ($validation->errors() as $error){ echo $error, '<br>'; }Looking at the 2nd part I cannot see why the programmer has put errors() as $error. Why not just create a method so you don't need to say "as $error". Also looking at the first part: private function addError($error){ $this->_errors[] = $error; } public function errors(){ return $this->_errors; }I don't understand why he has created two methods. Why not just create one? How is the public errors method able to display the errors within the class because _errors doesn't look like it contains anything. Can someone please break this down and help me to understand this because I've been at this for a while and I'm not really getting anywhere. Your help would be much appreciated. Our disc golf league uses a little PHP script that I developed (stealing lots of code in the process) that sucks in a list of league players from a text file, lets you select the ones that show up on league day, and randomly assigns the selections into foursomes and threesomes (Demo: http://edge.byethost18.com/demo4/foursomes.php)
Here's a modified example using letters of the alphabet:
. . . and the code: The actual version uses the shuffle() function to randomize the assignments and has a refresh button to reshuffle the assignments if a grouping contains bitter enemies. I disabled it in the example in order to show how the "A-B-C"s are assigned to the groups. As you can see, it assigns "A" to the first group, "B" to the second group, "C" to the third group and so on until it reaches the last group and then starts back at group 1 with the next letter. One of the our players wants a modification. He wants to be able to assign players into foursomes/threesomes according to their handicaps (best to worst). In other words, Group 1 would contain A, B, C, D. Group 2 would contain E, F, G, H and so on. Naturally, this would depend on which player checkboxes are selected, but the order is maintained. What I am having trouble with is getting the "A-B-C"s assigned into groups in the order they are listed in the text file. I tried using array_chunk() in the following script: https://neth.roe3.org/mdrone/ABCs/foursomes2.php
. . . and the code: In a nutshell, here's the code that's doing the work . . .
$numgroups = $d; $rows = array_chunk($players,$numgroups);
$x=0; I'm sure there's a simple solution. I'm just not seeing it. I'm really just a hobbyist . . . not a real programmer by any stretch of the imagination. Thanks for any help anyone can send my way. BTW . . . if anyone is interested in the foursomes generator source, I'm happy to share it. It could probably use improvement. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=350117.0 |