PHP - Sort Decimals Least To Greatest And Vice Versa?
Ok so say I have php do some calculations for me
Code: [Select] $Value1 = 300; $Value2 = 100000; $FirstRatio = $Value1 / $Value2; $Value3 = 8000; $Value4 = 6000000; $SecondRatio = $Value3 / $Value4; $FirstRatio = 0.003 $SecondRatio = 0.0013 How would I go about doing these calculations and then sorting $FirstRatio and $SecondRatio and then it would take the highest ratio ($FirstRatio) and it would echo the value? Similar TutorialsApparently it's bad to use floats when working with bitcoin. But the API i'm using doesn't really give an option to convert Bitcoin to Satoshie or vice versa. https://www.block.io/api/simple/php So I'm trying to figure out a way to do that function with PHP. Here is the code I found that converts from Satoshie to Bitcoin. My question is, how do I convert from Bitcoin to Satoshie. function convertToBTCFromSatoshi($value) { $BTC = $value / 100000000 ; return $BTC; } function formatBTC($value) { $value = sprintf('%.8f', $value); $value = rtrim($value, '0') . ' BTC'; return $value; } echo formatBTC(convertToBTCFromSatoshi(5000));
Hello everyone... My following code is supposed to order rank by from Greatest covertaction but for some reason it is ranking the greatest last eg. 900,000 Rank 1 900,001 Rank 2 900,002 Rank 3 Here is the code: Code: [Select] <?php $q = "SELECT * FROM `accountinfo_db` ORDER BY `covertaction`, `anticovertaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $i = 1; while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $time = time(); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `covertrank` = '$i', `lastTurnTime` = '$time' WHERE `id` = '$id'"; if(mysql_query($q)){ echo "Covert & Anti Covert Rank set to $user as ".number_format($i)."!<br />"; } else { // query failed with an error // put your error reporting/logging code here... } $i++; } ?> Any help will be appreciated! Brian Hi, I'm very new to PHP and i'm trying to delevop an app using flash as the front end via PHP and a mySQL database on my server/host. I currently have two php scripts that I am referencing. The first is used as a login and the second is used to retrieve table data. Both scripts work fine when tested in flash but when I upload the flash file to my server and test online the first (login) refuses to work. The php files are already uploaded to the server and the mySql database is created on my server. I was wondering if there was anything in the script that might be causing this. I can't see why and my brain is starting to degrade rapidly. Can someone have a quick look and tell me if there's something in this script that's preventing this from working online please? my flash variables a user and pass This is the login script that won't work online: <? //this pulls the variables from the flash movie when the user //hits submit. Use this when your global variables are off. //I don't know how to toggle global variables, so I just put //it in all the time ;) $user=$_POST['user']; $pass=$_POST['pass']; //connect to database mysql_pconnect("*****","********","******") or die ("didn't connect to mysql"); mysql_select_db("******") or die ("no database"); //make query $query = "SELECT * FROM jingle WHERE recipient = '$user' AND password = '$pass' "; $result = mysql_query( $query ) or die ("didn't query"); //see if there's an EXACT match $num = mysql_num_rows( $result ); if ($num >=1){ print "status=You're in&checklog=1"; } else { print "status=Sorry, but your user name and password did not match a user name/password combination in our database. Usernames and passwords are entered in from a different file. Thank you for visiting test login script!!&checklog=2"; } ?> and this is the script the does work online: <? /* Retrieves score data from highscores table and returns data and status to Flash errorcode: 0: successful select 1: can't connect to server 2: can't connect to database 3: can't run query */ // fill this in with the right data for your server/database config $server = "*****"; $username = "******"; $password = "******"; $database = "*******"; // get current user $user=$_GET['user']; $pass=$_GET['pass']; // mysql_connect: Open a connection to a MySQL Server // Returns a MySQL link identifier on success, or FALSE on failure. if (!mysql_connect($server, $username, $password)) { $r_string = '&errorcode=1&'; // mysql_select_db: Sets the current active database on the server that's associated // with the specified link identifier. Every subsequent call to mysql_query() // will be made on the active database. // Returns TRUE on success or FALSE on failure. } elseif (!mysql_select_db($database)) { $r_string = '&errorcode=2&'; // mysql_query: Sends a query (to the currently active database // For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a // resource on success, or FALSE on error. // For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() // returns TRUE on success or FALSE on error. } else { $qr = mysql_query("SELECT * from jingle WHERE recipient = '$user' AND password = '$pass'"); if (!qr || mysql_num_rows($qr)==0) { $r_string = '&errorcode=3&msg='.mysql_error().'&'; } else { $r_string = '&errorcode=0&n='.mysql_num_rows ($qr); $i = 0; while ($row = mysql_fetch_assoc ($qr)) { while (list ($key, $val) = each ($row)) { $r_string .= '&' . $key . $i . '=' . stripslashes($val); } $i++; } // add extra & to prevent returning extra chars at the end $r_string .='&'; } } echo $r_string; ?> Thanks in advance Hi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? Code: [Select] $_POST['amount'] = intval($_POST['amount']); if ($_POST['amount'] < 0){ message($lang_common['Bad request']); } if (!is_numeric($_POST['amount'])){ message($lang_common['Bad request']); } echo $pun_user['gold']; echo '<br>'; echo $_POST['amount']; exit; if I enter 0.05 in my gold input textbox and submit it, it reads 0, it doesn't read the decimal values, i need it to read them, but I also need it to be secure and use intval.. help Hello all it is my 1st post so welcome me i need to make php check if the number is 3 decimals and the last digit is 0. if it is 0 it will echo something if it is not 0 then it will echo something else i came up with this code but i dont know how to make it check if the decimal has a trail of 0 (zero) Code: [Select] <?php if(strpos($Price,".090") !== false){ echo "there is 0 at the end of the price"; //here code to remove the 0 if it is possible }else{ echo "there is no 0 at the end of the price"; // echo the normal price } ?> this will work correctly for the price $4.090 , 12.090 etc but how can i do it in php so it will check if there is 0 at the end of the decimal? Here is the number: $GrandTotal = "68.835"; $Deposit = round($GrandTotal , 2) * .5; echo $Deposit; I am looking for: 68.84 but the function doesn't seem to work right. Any help? Hi guys, I've got a line of code that asks for a column called "distance_completed" to be displayed as part of a string. This column tells users how far people have run in a running race. I set up this field in my database to 11,3 so all distances appear with a decimal at the end, and then 3 digits after the decimal place. How would I alter the code so that the numbers that don't have any information in the places section just appear as integers Eg: 42 instead of 42.000 (for someone who ran exaclty 42km) and those that do have information in the decimal places appear correctly (eg: if a person ran 42km and 195m, it would appear as 42.195). I think this is probably the only line of code that would need modification: Code: [Select] echo ($row['time'] != null)?$row["time"]:"dnf at ".$row["distance_completed"]."km."; Hi, I have a page on which decimal values are entered. The problem is however that sql does not recognize the comma (the european decimal separator) as the decimal separator. How can I change the input to something that sql can accept? And what if someone enters a number in English format (a dot instead of a comma)? Or uses thousands-separators? Has anyone have experience with this? Greetz, Winelover Hi - To me, this doesn't make sense. My Order form uploads product weights in an array . If I echo out those weights they are fine ie 25.25 However, when I try to update the DB for some reason, the for loop strips out all but the first digit, so 25.25 becomes '2' or indeed if I had entered 9999999 it just '9' is entered in to the DB. I have tried using number_format to pre-process the decimals, - but I get an error saying you can not use it in a write format. Many Thanks to anyone who can help with this !! PS - I have abbreviated the below code for clarity: Code: [Select] $weight = $_POST['weight']; for ($i = 0; $i < $numloops ; $i++) { $sql =" UPDATE `confirmedorder` SET orderid = '$orderid[$i]', weight='$weight[$i]',status='finished', ordervalue='$ordervalue' WHERE customerid = $customerid "; $this->db->query($sql); } I want to find the first sentence in a string without counting decimals. Here is the code I have, but I still want it to display the decimals, but I don't want it to count as the end of a sentence. $content = "This is my test string which is 3.5% of what I'm trying to do. This is only a test."; Code: [Select] function first_sentence($content) { if(preg_match('/[0-9]+\.[0-9]/', $content, $num)) { $number = "$num[0]"; $content = ereg_replace("$number","", $content); } preg_match('/^.*[^\s](\.|\?|\!)/U', $content, $match); $string = $match[0]; $title_count = strlen($match[0]); if($title_count > "6"){ return "$match[0]"; } else { $content2 = ereg_replace("$match[0]","", $content); preg_match('/^.*[^\s](\.|\?|\!)/U', $content2, $step); return "$match[0] $step[0]"; }} Currently this is what it displays: This is my test string which is % of what I'm trying to do. This is what I want it to display: This is my test string which is 3.5% of what I'm trying to do. Just looking to know if this is the proper way to implement this: I need the below if to be true when any number between 100 and 1000 is the value of x, including say "105.55" if($x>= 100 && $x <= 1000){ } Basically I'm asking will a value of x with a decimal in it not cause the if statement to be true. Hi there. I am trying to display numbers if they have the numbers like: number_format($price,2) This gives us two decimal places i.e 7.23 Now thats fine, I would like to round the last two decimal places. I would like to make it 7.25, (rounding it from 7.23 to 7.25) Similarly making: 12.44 to 12.45 33.18 to 33.20 52.13 to 52.15 So actually rounding the last two places. Coz obviously customers won't be able to pay 52 dollars 13 cents. So therefore rounding the last cent's (decimal part) making it to 52.15 Thank you. All comments and feedback are welcomed. (d Hello!If i have a table let's say ( Name, Age, Date etc ...), how can i do to sort data table DESC or ASC by clicking in Name, Age and then retrive the same page ( index.php let's say ) with sorted data? Hello, I have a code which displays name and number. It is working now but I want to sort the content on the basis of number... The code is given below: while($rows = mysql_fetch_array($results)){ echo "". $rows['username'] ." ". $rows['COUNT(Completed_Sales)'] .""; echo "<br />"; } The result will look like this Name Prdoducts. hi, is there a way to sort by column a, then by column b? Hello. I have an object like this: object->object_id object->object_name object->object_number I have an array with many instances of the object, with different values. I want to sort that array to put first the object that the value of object->object_number is higher, and then descending. Please let me know if its not clear! thank you! ok so my problem is i need my data to be sorted by date which comes first.. how do i do it..can someone help me..if possible elaborate some codes? thxx.. so this is my program Code: [Select] <? $b=$_GET['b']; $m=$_GET['m']; $y=$_GET['y']; if ($b <> '' && $m <> '' && $y <> '') { $branch=$b; $leavemonth=$m; $leaveyear=$y; } else { $branch=$_POST['branch']; $leavemonth=$_POST['leavemonth']; $leaveyear=$_POST['leaveyear']; } $connection=mysql_connect("$server", "$username", "$password") or die("Could not establish connection"); mysql_select_db($database_name, $connection) or die ("Could not select database"); $query="SELECT * from tblworkgroup"; $result=mysql_query($query);?> Branch <select name="branch" > <option value="all">ALL Branches</option> <? while($row=mysql_fetch_array($result)) { ?> <option value= "<?=$row['WorkGroupID']?>" <? if ($branch==$row['WorkGroupID']){ echo 'selected'; } ?> ><?=$row['WorkGroupName']?></option> <? } ?> </select> </td> </tr> <br><br><br> Month <select name="leavemonth" > <option value = "1" <? if ($leavemonth==1){ echo 'selected'; } ?>>January</option> <option value = "2" <? if ($leavemonth==2){ echo 'selected'; } ?>>February</option> <option value = "3" <? if ($leavemonth==3){ echo 'selected'; } ?>>March</option> <option value = "4" <? if ($leavemonth==4){ echo 'selected'; } ?>>April</option> <option value = "5" <? if ($leavemonth==5){ echo 'selected'; } ?>>May</option> <option value = "6" <? if ($leavemonth==6){ echo 'selected'; } ?>>June</option> <option value = "7" <? if ($leavemonth==7){ echo 'selected'; } ?>>July</option> <option value = "8" <? if ($leavemonth==8){ echo 'selected'; } ?>>August</option> <option value = "9" <? if ($leavemonth==9){ echo 'selected'; } ?>>September</option> <option value = "10" <? if ($leavemonth==10){ echo 'selected'; } ?>>October</option> <option value = "11" <? if ($leavemonth==11){ echo 'selected'; } ?>>November</option> <option value = "12" <? if ($leavemonth==12){ echo 'selected'; } ?>>December</option> </select> Year <select name="leaveyear" > <option value = "2010" <? if ($leaveyear==2010) { echo 'selected';} ?>>2010</option> <option value = "2011" <? if ($leaveyear==2011) { echo 'selected';} ?>>2011</option> <option value = "2012" <? if ($leaveyear==2012) { echo 'selected';} ?>>2012</option> <option value = "2013" <? if ($leaveyear==2013) { echo 'selected';} ?>>2013</option> <option value = "2014" <? if ($leaveyear==2014) { echo 'selected';} ?>>2014</option> <option value = "2015" <? if ($leaveyear==2015) { echo 'selected';} ?>>2015</option> <option value = "2016" <? if ($leaveyear==2016) { echo 'selected';} ?>>2016</option> <option value = "2017" <? if ($leaveyear==2017) { echo 'selected';} ?>>2017</option> </select> <input type='submit' name='submit' value='Preview' Class="button" onclick='return validate()'> </div> </form> <br /><br /> <div id="box" valign="top"> <h3> <strong>Leave Application Details</strong>  </h3> <br><br> <table width="80%" align="center" > <thead> <tr> <th width="700px" align="left">Name</a></th> <th width="800px" align="center"></a>Application Date</th> <th width="500x" align="right"></a>Reason</th> </tr> </thead> <tbody> <? /*echo "branch:".$branch.'<br />'; echo "leavemonth:".$leavemonth.'<br />'; echo "leaveyear:".$leaveyear.'<br />';*/ //-------------------------------------------------- if ($leavemonth =='' || $leaveyear =='' || $branch == '') { // one value missing, so don't display } else { // start display data $branchtracker = ''; if ($branch=='all'){ $branch = '%'; } $rs = mysql_query( " call vwleavereport('$leavemonth', '$leaveyear', $branch, 0);"); $query="SELECT *, MONTH(tblleaveapplication.DateFrom) as DFMonth, MONTH(tblleaveapplication.DateTo) as DTMonth, YEAR(tblleaveapplication.DateFrom) as DFYear FROM `tblleaveapplication` LEFT JOIN `tblemployee` ON tblleaveapplication.employeeid = tblemployee.id WHERE WorkGroupID LIKE '".$branch."' AND ( (MONTH(tblleaveapplication.DateFrom)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") OR (MONTH(tblleaveapplication.DateTo)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.") ) ORDER BY WorkGroupID "; $result = mysql_query($query); if ((mysql_num_rows($result))>0){ ?> <FORM name="printbutton" method="post" align="left" action="report_print.php" target="_blank"> <input type="hidden" name="leavemonth" value="<?php echo $leavemonth; ?>"> <input type="hidden" name="leaveyear" value="<?php echo $leaveyear; ?>"> <input type="hidden" name="branch" value="<?php echo $branch;?>"> <Input type = "Submit" Name = "Submit1" Class="button" VALUE = "Print Leave Details"> </form> <? while($row=mysql_fetch_array($result)) { if ($branchtracker<>$row['WorkGroupID']){ echo '<tr><td colspan=4 style="background-color: #e8e8e8; font-weight: bold;">'.$row['WorkGroupID'].'</td></tr>'; $branchtracker = $row['WorkGroupID']; } echo '<tr>'; echo '<td>'.$row['EmployeeName'].'</td>'; echo '<td>'.$row['DateFrom'].' - '.$row['DateTo'].'</td>'; echo '<td>'.$row['Purpose'].'</td>'; echo '</tr>'; } } } ?> statez : I need to sort the results from the high score to the low score. $query = "SELECT statez, COUNT(statez)FROM distributors GROUP BY statez"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { $returnS.= $line['COUNT(statez)'].",,".$line['statez'].",,,"; } echo $returnS; mysql_close($link); ?> Is there a way to use sort($Array) on numbers but reverse it. So instead of 1 to 3 itll do 3 to 1 ? AKA descending order? Can't find a function that would do unless theres a second sort function ? Thanks |