PHP - How Do I Explode A Round?
Code: [Select]
$newrating = round($imageinfo['rating'],2); this outputs: 4.49 in my db rating is 4.4884 all i need is the 4.... would i use some type of preg_replace or something just to grab the first 1 before the decimal? (all i need is the first number before the decimal) Similar TutorialsIf I explode a string well using a substring when I implode that substring is now missing. for example. Code: [Select] $exploded = explode("[MAC]",$data); $data = implode($exploded); Now [MAC] would be missing in the $data string. any way to get around this? hello guys, im creating a search result page that uses pagination, and im trying to figure out how many total pages their are going to be in my links with the page numbers. this is probably something really simple, ive been writing scripts for the last few hours and have a bit of a headache, any help would be appreciated here's the coding and the results i want: <?php $per_page = 10; $rows = mysql_query("SELECT COUNT(*) FROM articles WHERE text LIKE $string"); // this will return 31 $pages = $rows / $per_page; // returns 3.1 $total_pages = round($pages); // returns 3, want to round 3.1 up to 4 here ?> hi know who to round a number to the nearest whole number, if i use echo round(9.5); it echo's 10 and if i use echo round(9.2); it echo's 9 i want it so it always rounds up? can anyone help? cheers matt HOw to get the AVERAGE of my entire database? $query = "SELECT ROUND(AVG(scores)) FROM table"; Thanks! Bickey This works echo $data3['value']; But this doesn't echo round($data3['value'], 2); Can someone please advise me as to what I am doing wrong? I am trying to output only 40 of the latest files uploaded on the database server. But when I am trying to see the files uploaded on the server I am getting this error: Warning: round() expects parameter 2 to be long, string given in C:\wamp\www\mshare\lista.php on line 8 Call Stack The code: Code: [Select] <?php function bytestostring($size, $precision = 0) { $sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'kB', 'B'); $total = count($sizes); while($total-- && $size > 1024) $size /= 1024; return round($size, $precision)." ".$sizes[$total]; } echo '<div class="header">40 last uploaded files:</div>'; $sql = "SELECT * FROM filer order by id desc limit 40"; $q = mysql_query($sql); echo '<table style="width: 100%;">'; while ($r = mysql_fetch_array($q)) { $i++; $id = $r['id']; $filnamn = $r['name']; $filtyp = explode("/", $r['type']); $filtyp = ucwords($filtyp[0]); if($filtyp == "") { $filtyp = "Unkown"; } $filstorlek = bytestostring($r['size'], ""); $a = $i%2; echo'<tr><td class="lista">'; echo ''.$i.'. <td class="lista">'.$filnamn.'<td class="lista">'.$filtyp.' <td class="lista">'.$filstorlek.' <td class="lista"><a href="download.php?id='.$id.'"'.$a.'"><img src="download-icon.jpg" class="imagedow"></a>'; } echo "</table>"; ?> I am new to PHP coding so you guys just know. i have a price i want to add 10%-15% to the base price and that the result will be rounded to 9 in the ones digit place. for example 85 +15%=97.7 $result=99 117+15%=134.55 $result = 139 played with round() and ceil() I need to take a time and convert so it is rounded up or down to the nearest 0 or 5. Code: [Select] function roundMe ($time, $upDown) { if($upDown==1) { //round up } else { //round down } return $x; } echo roundMe("16:22", 1); // returns 16:25 echo roundMe("16:22", 0); // returns 16:20 echo roundMe("16:25", 1); // returns 16:25 echo roundMe("16:59", 1); // returns 17:00 echo roundMe("16:59", 0); // returns 16:55 Hi.. I encountered problem in rounding of numbers into two decimal places. here is my sample code: if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) { $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); $TAX = number_format($TAX, 2, '.', ''); } for example from this: $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); the output is: 1417.615 using this: $TAX = number_format($TAX, 2, '.', ''); the output was : 1417.61 but it should be : 1417.62 Thank you hello, if i have the following, how would i display it? Code: [Select] function minutes_round ($hour = "$signintime", $minutes = '5', $format = "H:i") { $seconds = strtotime($hour); $rounded = round($seconds / ($minutes * 60)) * ($minutes * 60); return date($format, $rounded); } Hey guys, I use the code below to add a corners to an image and it works great but when i add a border to the image it add a square.... any ideas how i can add a border to the rounded image? <?php $image_file = $_GET['src']; $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == "no") ? false : true; // Top-left rounded corner is shown by default $bottomleft = (isset($_GET['bottomleft']) and $_GET['bottomleft'] == "no") ? false : true; // Bottom-left rounded corner is shown by default $bottomright = (isset($_GET['bottomright']) and $_GET['bottomright'] == "no") ? false : true; // Bottom-right rounded corner is shown by default $topright = (isset($_GET['topright']) and $_GET['topright'] == "no") ? false : true; // Top-right rounded corner is shown by default $imagetype=strtolower($_GET['imagetype']); $backcolor=$_GET['backcolor']; $endsize=$corner_radius; $startsize=$endsize*3-1; $arcsize=$startsize*2+1; if (($imagetype=='jpeg') or ($imagetype=='jpg')) { $image = imagecreatefromjpeg($image_file); } else { if (($imagetype=='GIF') or ($imagetype=='gif')) { $image = imagecreatefromgif($image_file); } else { $image = imagecreatefrompng($image_file); } } $size = getimagesize($image_file); // Top-left corner $background = imagecreatetruecolor($size[0],$size[1]); imagecopymerge($background, $image, 0, 0, 0, 0, $size[0], $size[1], 100); $startx=$size[0]*2-1; $starty=$size[1]*2-1; $im_temp = imagecreatetruecolor($startx,$starty); imagecopyresampled($im_temp, $background, 0, 0, 0, 0, $startx, $starty, $size[0], $size[1]); $bg = imagecolorallocate($im_temp, hexdec(substr($backcolor,0,2)),hexdec(substr($backcolor,2,2)),hexdec(substr($backcolor,4,2))); $fg = imagecolorallocate($im_temp, hexdec(substr($forecolor,0,2)),hexdec(substr($forecolor,2,2)),hexdec(substr($forecolor,4,2))); if ($topleft == true) { imagearc($im_temp, $startsize, $startsize, $arcsize, $arcsize, 180,270,$bg); imagefilltoborder($im_temp,0,0,$bg,$bg); } // Bottom-left corner if ($bottomleft == true) { imagearc($im_temp, $startsize, $starty-$startsize,$arcsize, $arcsize, 90,180,$bg); imagefilltoborder($im_temp,0,$starty,$bg,$bg); } // Bottom-right corner if ($bottomright == true) { imagearc($im_temp, $startx-$startsize, $starty-$startsize,$arcsize, $arcsize, 0,90,$bg); imagefilltoborder($im_temp,$startx,$starty,$bg,$bg); } // Top-right corner if ($topright == true) { imagearc($im_temp, $startx-$startsize, $startsize,$arcsize, $arcsize, 270,360,$bg); imagefilltoborder($im_temp,$startx,0,$bg,$bg); } $newimage = imagecreatetruecolor($size[0],$size[1]); imagecopyresampled($image, $im_temp, 0, 0, 0, 0, $size[0],$size[1],$startx, $starty); // Output final image header("Content-type: image/png"); imagepng($image); imagedestroy($image); imagedestroy($background); imagedestroy($im_temp); ?> 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 Ive got a text area and i want to seperate each link that is placed inside the text area,. Is there a way using php exlode or somthing to reconise that the link has finished? Each of the links in the text area are on a new line. Thanks in advance. i want to explode a text field on the fly ? for example city,region.country and to turn it to city_id,region_id,country_id 1.textfield from autocomplete 2.get post variable 3.explode 4 get ids i played around with some ajax with php Code: [Select] <html> <body> <input name="searchField" id="searchField" type="text" value="<?php echo $_POST['searchField'];?>"/> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; var t=setTimeout('Ajax()',1000); clearTimeout(t); } var searchfieldvalue=encodeURIComponent(document.getElementById("searchField").value) xmlHttp.open("GET","getid.php?searchField="+searchFieldvalue+"",true); xmlHttp.send(null); } window.onload=function(){ var t= setTimeout('Ajax()',1000); } </script> <?php /*explode searchField and generate ids $searchField=$_POST['searchField']; $p = explode(" , ", $searchField, 3); $cityname=mysql_real_escape_string($p['0']); $regionname=mysql_real_escape_string($p['1']); $countryname=mysql_real_escape_string($p['2']); */ ?> <div id="ReloadThis">Default text</div> </body> </html> //getid <?php $p=array(); $p = explode(" , ", $_GET['searchField'], 3); echo $cityname=$p['0']; echo $regionname=$p['1']; echo $countryname=$p['2']; echo'<input type="hidden" name="place" id="place" value="'.$cityname.'"/> <input type="hidden" name="region" id="region" value="'.$regionname.'"/> <input type="hidden" name="country" id="country" value="'.$countryname.'"/>'; here ill add function to get city_id() etc ?> Hi, I have an array for prices : forfait="158|Group Session,95|Group half session,45|Private Course,90|zumba course" which is used in a dropdown list where it shows as Group Session (158$) Group half session (95$) etc... The prices need to be used as an 'amount' field on the gift certificate as the descriptions must show as a 'label' on the gift certificate. So, I need to ' explode ' both the prices and the descriptions separately. I can 'explode' de prices using $tmp_forfait=explode("|", $value) and I get 158 95 45 and 90 But, how do I get the descriptions separately, without the prices meaning I need to 'explode' the digits and '|' ... Anyone can help me with that cope please? Thank you Hi all, I posted a similar question before and got some of the way but still need a little help. I have a product database with a description column where there are strings broken into lines. I need to find a way to explode() by line break. Currently explode("/n") doesn't work - it only returns one array item. Can anyone help me out? Thanks! I store date data in MySql in this format DD-MM-YYYY , and now I need to explode this format when get information from database to get this: $day = DD , $month = MM , $year = YYYY. $result = $db->query("SELECT user_birthday FROM users WHERE user_id = " . mysql_real_escape_string($_SESSION['user_id']) . " LIMIT 1"); $row = mysql_fetch_assoc($result); $birthday = $row['user_birthday']; and I get this date format: 12-07-1980 How to explode this 3 values in day, month, year? I have a script that connects to another server to pull data... When it pulls the data it comes back in the following format: Code: [Select] t1.sh t2.sh t3.sh test.sh However using the code bellow does not separate it like it should, it should put one file per line but I think I did something wrong... Code: [Select] // Pulls the output from the server: $var_test = $ssh->exec('./test.sh'); // Explodes it... $var_test2 = explode(" ", $var_test); // Echos it one per line... foreach ($var_test2 as $variable) { echo '<br>'.$variable; } With a database of synonyms, how do I compare a fragment of a texarea sentence split by the php explode function to return only five synonyms out of say 100 synonyms of the same word for each fragment. The Sql statement is queried to return 5 results Code: [Select] <?php if (isset($_GET['submit'])) { $sentence = $_GET['sentence']; // break $sentence using the space character as the delimiter $words = explode(' ', $sentence); //tell the amount of words using the size of the array echo 'The sentence has ' . count($words) . ' words.<br />'; // loop through and print all the words for ($i = 0; $i < count($words); $i++) { echo "Piece $i = $words[$i] <br />"; //echo 'Word ' . $i . ' - ' . $words[$i] . '<br />'; //$words=$_GET['words']; $sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3"; $data=mysqli_query($dbc,$sel); $num= mysqli_num_rows($data); if (!$data) { die('<p>Error Retrieving<br/>'. 'Error: ' .mysql_error() . '</p>');} $datalist=mysqli_fetch_assoc($data); $synonym=$datalist['synonym']; ?> I intend to use a 2 texareas: 1. Code: [Select] <textarea name="split_sentence"><?php echo "sentence" ?></textarea> the 1st <texarea> has the original sentence to be split by the explode function. 2. Code: [Select] <textarea name="split_sentence2"><?php echo ("$words[$i] {{$synonym}}?></textarea> the second <texarea> should take the explode fragments compare it with the query above and return five synonyms per word on a SINGLE <textarea> tag. 3. Besides, I keep getting this offset error on the 2nd <texarea> refering to the $words[Si] array element in the 2nd <textarea> Below is the full code: Code: [Select] `<?php //session_start(); // Define database connection constants define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_NAME', 'moby_thesaurus'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>compare_words</title> </head> <body> <table><p> PLEASE ENTER INITIAL SENTENCE </p> <form name="sentences" method="get" action="" > <tr> <td> <textarea name="sentence" id="elm1"></textarea> </td> </tr> <tr> <td> <input name="submit" type="submit" value="Create_Spinable Content" /> </td> </tr> </form> </table> <?php if (isset($_GET['submit'])) { $sentence = $_GET['sentence']; // break $sentence using the space character as the delimiter $words = explode(' ', $sentence); //tell the amount of words using the size of the array echo 'The sentence has ' . count($words) . ' words.<br />'; // loop through and print all the words for ($i = 0; $i < count($words); $i++) { echo "Piece $i = $words[$i] <br />"; //echo 'Word ' . $i . ' - ' . $words[$i] . '<br />'; //$words=$_GET['words']; $sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3"; $data=mysqli_query($dbc,$sel); $num= mysqli_num_rows($data); if (!$data) { die('<p>Error Retrieving<br/>'. 'Error: ' .mysql_error() . '</p>');} $datalist=mysqli_fetch_assoc($data); $synonym=$datalist['synonym']; ?> <table><p> SPINABLE CONTENT </p> <form name="sentences" method="get" action="" > <tr> <td> <?php if ($datalist=mysqli_fetch_array($data)) { do{ ?> <textarea cols="80" id="editor1" name="sentence" rows="10"><?php echo ("$words[$i] {{$synonym}}||{{$synonym}}||{{$synonym}}"); ?></textarea> <?php } while ($datalist=mysqli_fetch_array($data)); echo("</table> </p>"); } ?> <?php } } ?> </td> </tr> <tr> <td> <input name="submit" type="submit" value="Spin This Now" /> </td> </tr> </form> </table> </body> </html> Please any suggestions would be highly appreciated. I'm trying to add options to products. Currently, I have them in this format: Leather reinforced hilt,5.00::Stabbing tip,3.00::Logo,10.00 Code: [Select] $option_results = mysql_query("SELECT product_options FROM products WHERE product_id='".$product."'"); while($option_row = mysql_fetch_array($option_results)){ $option = explode("::", $option_row[0]); $option_part = explode(",", $option[0]); $option_part[0] = $option_name; $option_part[1] = $option_price; echo "<li><a href=\"#\">".$option_name.", Add $".$option_price."</a></li>"; } I would like the output to be: <li><a href="#">Leather reinforced hilt, Add $5.00</a></li> <li><a href="#">Stabbing tip, Add $3.00</a></li> <li><a href="#">Logo, Add $10.00</a></li> |