PHP - Problem Rounding Result.
Hi,
Having trouble rounding the output. I would need to be sure the output is not longer than 2 spaces after comma. Any ideas? My code Code: [Select] <script type="text/javascript"> $(document).ready(function() { $("<?php echo "#".$input_end_time; ?>").blur(function(){ var start_time = $("<?php echo "#".$input_start_time; ?>").val(); var end_time = $("<?php echo "#".$input_end_time; ?>").val(); $.getJSON("include/timedifferencecalculation.php?s="+start_time+"&e="+end_time, function(data){ var success = data.success; if (success == 1) { $("<?php echo "#".$input_difference; ?>" ).val(data.hours + " h " + data.minutes + " m "); if (data.hours + data.minutes/60 >= 6){ $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); }else { $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60); } }else { $("#message").text("Viga! " + data.reason); } }) }); }); </script> Code: [Select] This is before $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); This is what im trying to get but its not working. $("<?php echo "#".$hours_difference; ?>").val( round(data.hours + data.minutes/60 - 0.5), 2 ); Similar TutorialsHere 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? i cannot figure out why my first result is not displaying properly it successfully shows all results but me first myurl.com/other.php?id=2 now showing any thing kindly help to solved this problem! it seems like problem is in variable $http$ids but dunno how to combine them to get result $Ids always b numeric number <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $http = "myurl.com/other.php?id=" ; $conn=odbc_connect('greeting','',''); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT * FROM mytable"; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} echo "<table border= 1><tr>"; echo "<th>ID</th>"; echo "<th>Like</th>"; echo "<th>title</th></tr>"; while (odbc_fetch_row($rs)) { $ids=odbc_result($rs,"ID"); $title=odbc_result($rs,"title"); echo "<tr><td>$ids</td>"; echo "<td><fb:like href=\"$http$ids\" layout=\"button_count\" show_faces=\"false\" width=\"100\" font=\"tahoma\" colorscheme=\"dark\"></fb:like> </td>"; echo "<td>$title</td></tr>"; } echo "</table>"; odbc_close($conn); ?> Hello guys I have here my array result Array ( => Array ( => 1 [1] => [2] => 123456789 [3] => [4] => 2012-02-02 [5] => 09:12:21 [6] => Test Array ) ) I would like to view this result into a string so that it will remove the unnecessary array elements like brackets etc. Can some tell me how to handle this properly? The result would be : 1 123456789 2012-02-02 09:12:21 thanks This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306966.0 Hi,
Well, I've been wondering if I can get answer to my problem here...
I'd used curl to fetch information from other site and then I need to parse the data to be displayed in our own. However I found something odd...I can't parse it to get what I want...however should I put the result into a html file, n parse the file itself...it's fine.
Basically I just need to find whether a word/phrase is there or not...n I'm using preg_match to do it so far. Since the time it first arises...I'd been googling n saw some posts...something similar to how regex is doing poorly in doing such thing...
However it does work when I just make a php file just to do that regex from the curl-result html file, it just not work on the actual php file which handle the process even if it's from the same curl-result html file. FYI, I'm using codeigniter.
I wonder if it got something to do with codeigniter or something...or is there any other better way to parse a html file from a curl result. In my opinion it's just the same...be it a curl result or just a normal html...so I really can't figure out where it went wrong.
I appreciate if there's anyone who can shed a light on this problem, even a little bit is a great help to me
Thanks in advance,
Hey everyone I've been working on converting my old mysql code to to the mysqli version and am running into some trouble. I am pulling posts form phpbb and displaying them in a script. My first script below works just fine. Code: [Select] <?php require("connect.php"); $query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14') AND topic_status = '0'"; $query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16') AND topic_status = '0'"; $result=mysql_query($query); $result2=mysql_query($query2); echo "<table border=0 cellpadding=2>"; echo "<tr><td>Lost</td><td>Found</td></tr>"; while($row=(mysql_fetch_array($result)) || $row2=(mysql_fetch_array($result2))) { $forum_id=mysql_result($result,$k,"forum_id"); $topic_id=mysql_result($result,$k,"topic_id"); $topic_title=mysql_result($result,$k,"topic_title"); $forum_id2=mysql_result($result2,$k,"forum_id"); $topic_id2=mysql_result($result2,$k,"topic_id"); $topic_title2=mysql_result($result2,$k,"topic_title"); echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id&t=$topic_id>$topic_title</a></td> <td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id2&t=$topic_id2>$topic_title2</a></td> </tr>"; $k++; } echo "</table>"; mysql_close($connect); ?> however its not very clean or memory efficient so i have converted it to the following. Code: [Select] <?php require("includes_database/mysqli_connect.php"); $query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')"; $query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')"; $result=mysqli_query($dbc, $query); $result2=mysqli_query($dbc, $query2); echo "<table border=1 cellpadding=2>"; echo "<tr><td>Lost</td><td>Found</td></tr>"; while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) || ($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC))) { echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td> <td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td> </tr>"; } echo "</table>"; mysqli_free_result($result); mysqli_free_result($result2); mysqli_close($dbc); ?> I know the error is in my while loop but i cant seem to figure out exactly where. The new code displays all of the first query in the table and then below that it displays the next query insted of displaying them side by side below would be an example of what is happening [table Lost Found Lost, Australian Sheppard Lost German Shepherded / Collie Mix Lost Golden Retriever/Cocker Mix Lost Female Grey Tiger Lost Pitbull Mix Lost 2 Chihuahua Lost Black Lab Lost - German Shepard Mix and Lab Mix Lost - Female German Sheperd Aprox 8 Week Old Kitten Found! 2 Pugs FOUND Found 1 Corgi female/ 1 Shih Tzu male Found Tan and Black Australian Shepard Found Australian Shepard Found Intact Male Pitbull/Lab Mix Any help that any of you could provide would be greatly appreciated! Hi, it's my first post so hello to you and i hope you will be forgiving:) I've made a basic script, which takes a urls from the form(each of them is the new row) and then search each of those urls for an email. The problem is that only the email from the last url is returned. print_r("show link:". $urls[$i]."<br>"); shows all urls in the form, but the print_r("show current_url:". $current_url); shows only the last one. Code: [Select] <?php $urls = explode("\n", $_POST['urls']); $db = new mysqli('localhost', 'root', 'root', 'urls'); if (mysqli_connect_errno()) { echo 'Błąd: '; exit; } for ($i=0; $i<count($urls); $i++){ print_r("show link:". $urls[$i]."<br>"); $current_url = file_get_contents($urls[$i]); print_r("show current_url:". $current_url); preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $current_url, $email);//email print_r ("show email:".$email[0]); $zapytanie = "INSERT INTO urle set adres = '$email[0]' "; $wynik = $db->query($zapytanie); } if ($wynik) { echo $db->affected_rows ."pozycji dodano."; } else { echo mysql_errno() . ":" . mysql_error() . "Wystąpił błąd przy dodawaniu urli "; } $db->close(); ?> Hi there, hope one of you can help me with a problem im just having. Ok, lets start with the explanation what i want to to: I'd like to collect headlines from a html site, and get the test results in an array, so that the array structure represents the dom-levels of the site. Small Example: <h1>test1.1<h1/> <h2>test2.1</h2> <h2>test2.2</h2> <h3>test3</h3> <h1>test1.2<h1/> Shoul end in a array structure like: Code: [Select] array('level 1' => array ( 'sibble1' => array ( 'headline' => 'test1.1', 'level2' => array( 'sibble1' => array ( 'headline' => 'test2.1', 'level3' => array(), // empty data, needs to be processed anyway to find gaps, to maybe a h4 headline would be existing ), 'sibble2' => array ( 'headline' => 'test2.2', 'level3' => array ( 'sibble1' => array ( 'headline' => 'test3.1', 'level4' => array(), // empty data, needs to be processed anyway to find gaps, to maybe a h4 headline would be existing ), ), ), ), ), 'sibble2' => array ( 'headline' => 'test1.1', 'level2' => array(), ), ), ); So i hope out of this example you can see what i want to do. level represents the healdine level 1-9, sibblin is as name for the childs on the headline level. Ok, so to extract the herefore needed data out of the html, i build a class with a recursive function, that filters the html by a regex from one headline to the next, first iteratin all childs, if there are no more childs i go tho ne nextsibbling element. as an running example code look he Code: [Select] <?php class Application_Model_DomParser { const PREG_MATCH_ONE_ELEMENT_TO_MUCH_LENGTH = 4; private $slicedFields = array(); public function __construct() { } public function sliceFirstSectionToDataFields($level = 1, $haystack) { preg_match_all("#(.*?)<h$level>(.*?)</h$level>(.*?)(<h$level>|$)#s", $haystack, $data); // prepare the chunkData $pageText = ''; if (isset($data[1][0])) { $pageText = $data[1][0]; } $headline = ''; if (isset($data[2][0])) { $headline = $data[2][0]; } $dataToProcessNextLevel =''; if (isset($data[3][0])) { $dataToProcessNextLevel = $data[3][0]; } // @todo dirty warnings compression, search why warning occures @$posOfNextChild = strlen($data[0][0]) - self::PREG_MATCH_ONE_ELEMENT_TO_MUCH_LENGTH; // from here goes the debug... echo $headline ."::" .strlen($dataToProcessNextLevel). "<br>"; if (strlen($dataToProcessNextLevel) <= self::PREG_MATCH_ONE_ELEMENT_TO_MUCH_LENGTH) { if ($level < 9) { $dataToProcessNextLevel = $haystack; } else { return; } } //recursive check for next level in $dataToProcessNextLevel $nextLevel = $level + 1; $this->sliceFirstSectionToDataFields($nextLevel, $dataToProcessNextLevel); $haystack = substr($haystack, $posOfNextChild); // slized To The End if (self::PREG_MATCH_ONE_ELEMENT_TO_MUCH_LENGTH == strlen($haystack)) { // die("slized To The End"); return; } // recursive check for other childs in actuall level... $this->sliceFirstSectionToDataFields($level, $haystack); } } $htmlString = 'test<h1>headkline1.1</h1> <p>test test</p> <h2>headline 2.1</h2>test test <h2>headline 2.2</h2> <p>tes test test</p> <h3>headline 3.1</h3>test <h3>headline 3.2</h3>test <h2>headline 2.3</h2> <p> </p> <p>test</p> <h1>headline 1.2</h1> <h2>headline 2.4</h2> <p>11111111111112222222222222222222222</p> <p> ewfwrefg upowmdg w3q09umq09wrt n3q089ty 3q0898943ty -98 41</p> <h3>headline 3.3</h3> <p>test</p> <p>test</p> <p>test</p> <h1>1.3 testtest</h1> <p>test</p> <h3>head 3.4</h3> <p>sadfsadfsadf asdfsda f sdaas saf saddas</p> <h3>head 3.4</h3> <h3>test 1.3</h3> <p>test;</p> '; $model = new Application_Model_DomParser(); $result = $model->sliceFirstSectionToDataFields(1, $this->htmlString); So letting this piece of code run, you can se via the debug echo, every headline is found, even in the right order of its occurence. My problem now is dont get it how to return the extracted values, and collect them to get a result as my above shown structure shows. So i spent ours to solve this problem but didnt come to a result. I know its a hard problem, and to help me takes time, cause its a complex situation. Allthough i hope someone knows a anser. I need this problem solved, to get my mind rested! Thanks, and greetings Selma I am really lost here with this date issue of mine. The below code is the last part of a query: Code: [Select] $defendercheck_3 = $row_checkifattacked3['atdate']; $defendercheck1_3 = strtotime("$defendercheck_3"); $defendercheck2_3 = date("D", $defendercheck1_3); The query does not return any results as expected, but when echoing the various steps I get following: echo "$defendercheck3"; = nothing (as expected) echo "$defendercheck1_3"; = nothing (as expected) echo "$defendercheck2_3"; = result! (NOT expected) why does it return anything on "date("D", $defendercheck1_3)" when "$defendercheck1_3" is blank? How can I round this with 2 decimals: Code: [Select] echo " $row[team_treasure] "; I've tried round($row[team_treasure],2) but doesn't seem to work inside my echo tags :/ hi there all, I need some help for rounding currency issue.. example: 0.98 round to 0.95 0.95 when round = 0.95 0.91 roun to 0.95. mean. i wanna round the end of number 0.98. if the end of number is 0.01,0.02,0.03&0.04 it would be automatic round to nearest 0.05 *0.01 round = 0.05 *if the mineral water =$0.71 i should get $0.75 and if the end of number is 0.06,0.07,0.08,0.09 it would automatic round to nearest -0.05 *0.06 round = 0.05 *if the mineral water = $0.79 then i should get $0.75 i had googling this for 2months and no answer.. please help me.TQ Hi all, Can anyone please help me with my problem. I want to know how to round a decimal - eg. 12.34 to the nearest .10 so it would be 12.30 or if 12.35 go to 12.30 or 12.40. // my example: round(12.34, 2); // I want to to go to: 12.30 Any help is greatly appreciated. I want to round off an integer vaule to 10-6 grd from 52.71666666 to 52716666 0.926888888 to 926888 does anyone know a simple way to do this? So I just started working with php (not programming) a couple days ago. In this formula, I need to round something down to the nearest 5 OR 0. Whatever comes first. Any thoughts on how i might do this? Here's what I have so far. <?php $magicLevel = $_POST['magicLevel']; $playerLevel = $_POST['playerLevel']; $rune = $_POST['rune']; //gets the value "lmm" $maxDamage = 0; switch($rune) { case "lmm": $maxDamage = ($playerLevel*0.2)+($magicLevel*0.81)+4; break; } echo $maxDamage; ?> What I need to round down is this part: ($playerLevel*0.2) Thanks in advance! I am trying to take a number (three for example) and want to round it to five, or sixty-seven up to seventy. I looked at round and ceil, but those don't seem like they are the right things. Thanks, Joseph I am very new to PHP and programming in general. About 3 weeks into this. I have this simple expression and some echo in my code. $x1 = $lat_b + $lat_a1; echo "<br>lat b: ".$lat_b; echo "<br>lat a1: ".$lat_a1; echo "<br>x1: ".$x1; $lat_b and $lat_a1 are derived from some xml extractions from a yahoo geocode api (that part works fine). The results of the expression and echos above are as follows: lat b: 40.402866 lat a1: 40.252590 x1: 80 Why is $x1 rounding automatically on me? Later, I substituted the expression above with $x1 = 40.402866 + 40.252590 ; The new result (shown below) makes sense to me. lat b: 40.402866 lat a1: 40.252590 x1: 80.655456 Hey Everybody, I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused. Is there an easier/faster/better way to achieve this?? My Object: <?php /*GHETTO NUMBER FORMATTER v1.0*/ class ghettoNumber { private $input; private $output; public $whole; public $decimal; public $number; function __construct($input){ $this->input = $input; $this->output = $output; $this->number = $number; $this->whole = $whole; $this->decimal = $decimal; } private function ghettoFormat(){ //Split number at the .(decimal) $this->number = explode('.', $this->input); //Define the whole number $this->whole = $this->number[0]; //Format the whole number $this->whole = number_format($this->whole, 0,'', ','); //Define the decimal $this->decimal = $this->number[1]; //Format the decimal $this->decimal = rtrim($this->decimal, '0'); if(is_string($this->input)){ $this->output = $this->decimal != '' ? $this->whole.'.'.$this->decimal : $this->whole; } else{ $this->output = '<strong>ERROR:</strong> Input arg must be passed as type: <em>string</em>'; } //Return result return 'Original - '.$this->input.'<br> GhettoFormat - '.$this->output.' <br><br>'; } public function makeItGhetto(){ return $this->ghettoFormat(); } } ?> Sample Instantiation: $ghettoFormat = new ghettoNumber('12565456565.123401201001210000'); print $ghettoFormat->makeItGhetto(); I will reiterate my question once more... Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)? Thanks People, E I need to round a number based on it's decimal. 3 or below should round down, 4 should round up to 5, 6 and 7 should round down to 5; 8 and 9 should round up to the next whole number. The below is returning 3 from 2.16. Code: [Select] <?php $a=2.16; $i=round($a, 1); $b=$i; $i=explode(".", $a); $decimal=$i[1]; if($decimal < 5){ $j=floor($b); $i= $decimal > 3 ? $j + .5 : $j; echo $i; } else{ $j=ceil($b); $i= $decimal < 8 ? $j - .5 : $j; echo $i; } ?> EDIT: The explode should have been on $i not $a. Hi fellas, Can anyone show me why my script is still rounding up / down? printf ("<tr> %s Avg gun Ahit %%</td> %s %.2f %%</td></tr>\n",$tdo,$tdor,number_format($ahit/$fired*100, 3, '.', '')); Do I have a syntax error as I cannot get my math equation to display more than 2 decimal places even though it's set to 3? Any help or advice would be really great. |