PHP - Number Formatting
Hi there,
I need a PHP number format to match one and all of these sequence of numbers: 8500.00 9999.99 15.00 0.00 Similar TutorialsHi, I want to chenge the following statement so that numbers ending with ".00" are changed to blank. Many opinions but I haven't got any to work. Any help? <td align=right class="currency">$'.number_format($row['value1'],2).'</td> 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'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? Hello,
I have problem durring binding update query. I can't find what is causing problem.
public function Update(Entry $e) { try { $query = "update entry set string = $e->string,delimiter=$e->delimiter where entryid= $e->id"; $stmt = $this->db->mysqli->prepare($query); $stmt->bind_param('ssi',$e->string,$e->delimiter,$e->id); $stmt->close(); } catch(Exception $ex) { print 'Error: ' .$ex->getMessage(); } }When I run function update I'm getting next error:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Can you help me to solve this problem ? Edited by danchi, 17 October 2014 - 10:25 AM. I need to display a number(the number is retrieved from the db) in the form input field such that only the last 4 digits is visbile, the remaining can be masked as * or X or whatever is applicable. I know the last 4 can be obtained as follows: Code: [Select] $number=substr($number,-4,4); But when i hit the submit button the form validates the input field and checks if the number is a valid number of a specific format. Therefore when I click on the submit button then I should still be able to unmask the masked numbers or do something similar that would help me validate the whole number. Code: [Select] <input type="text" name="no" value="<?php if(!empty($number)){ echo $number;} ?>"> Hey I have a string that looks like the following: Quote top-php-tutorials-2.html I have a script that cycles through each page. The 2 in the quote above is the page number. How can I extract the number between the - and the .html and replace it with another number? I've tried Code: [Select] substr($engine->selectedcaturl, 0,-6).$v.".html"But then I realised this only works for numbers that are 1 digit long Any input would be appreciated hello, i have a start time and an end time (no dates). the following works great, except for when the start time is at night, and the end time is in the morning. im thinking if i can do an if the number is negative, add 24, that it will resolve my issue. any ideas? Code: [Select] $tbilled = $tfinish - $tstart; Hi everyone, I am having some odd issues with sending php generated emails. Essentially, what I'd like to know is how can I ensure they go into the inbox, and not get shunted to the junk mail folder. I assume this is s formatting issue, and it only happens sometimes. I am creating html emails. I have included my headers below: $headers = "From: <Real Money Help>\r\n"; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; Any help would be appreciated. Thanks. I'm going round in circles with this one, but I have a feeling that the answer is obvious. I have POST information that I want to use as a date and display it using date format, but I don't know how to The POST data outputs the following: 2011-9-23 Assuming I have defined $date as the posted data from the previous form, how do I write some code to tell it to output it as September 23, 2011, or just Sept 2011? Hi, Has anyone come across an issue when saving DOM to a file and Code: [Select] $document->preserveWhiteSpace = false; $document->formatOutout = true; has no effect? Having used unix timestamps in the past formatting the date used to be easy. But since converting to mysql DATETIME im struggling to format it properly. I basically pull the date from the database, convert it using strtotime(), then convert it to a date using date(). The problem is that when i use 'a' to display am/pm the time almost always says am. If the time is 11am then it will display 11am. If it's 12pm then it will display 12pm, but if it comes to 1pm and above it will revert back to 1am. This is the date string I use: date('jS M Y g:i a'); is this due to the fact that mysql DATETIME uses 24 hour clock ('H'), if so is there a way around this? I'm not sure what I'm doing wrong. I have a date in a table which is formatted as: 2010-08-13 00:00:00. I am trying to change the date through PHP to display as 08-13-2010. The date is being formatted as 12-31-1969. Here's my code. if($registrant->StatusDate != '0000-00-00 00:00:00') { $formattedDate = date('m-d-Y', $registrant->StatusDate); $statusDate = ' as of ' . $formattedDate; } Can anyone advise me what I am doing wrong? Thanks! I've been wracking my brain trying to figure this simple problem out and I can't for the life of me figure out the best way to do it. I have the following array: $n = array('81.0', '80.5', '70.5', '67.0', '65.5'); I only want to display the numbers to one decimal place if it's not a whole number, like so. 81 80.5 70.5 67 65.5 I've tried looking for a solution with sprintf(), rtrim() and number_format(), but what's the best way to do it? Regards Rich I am having a problem with what is probably encoding. I am pulling strings from a WordPress database. Below is a sample string: Code: [Select] Sed nec neque quis sapien scelerisque aliquet. In in est odio, ac auctor erat. Vestibulum fringilla dapibus sodales. Morbi facilisis egestas arcu, eu semper mi luctus vitae. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse sollicitudin, metus sed hendrerit tristique, enim ante venenatis metus, ac consectetur mauris lorem et felis. Phasellus ac purus massa. Aliquam ligula mauris, iaculis et posuere vitae, pellentesque sit amet leo. Praesent at eros et lacus scelerisque feugiat. Aliquam non facilisis libero. Ut blandit leo ut tortor convallis porttitor. Aliquam erat volutpat. Aliquam rhoncus rutrum metus, tempor sollicitudin ante laoreet in. Sed tincidunt nibh a augue euismod sagittis. Fusce ullamcorper ultricies nisi, sit amet elementum mi scelerisque eget. Cras sed elit ligula, sit amet aliquam lectus. Nulla vel mollis nibh. Pellentesque ultricies, dui non aliquam eleifend, tortor ligula blandit dolor, quis interdum diam augue non eros. Nullam id molestie nunc. Integer id tempus est. When WordPress displays this text, it is formatted correctly, with spaces between paragraphs, etc. When I pull the string from the database, and echo it onto a page, the spaces between paragraphs are replaced by spaces, so in the end the output is as follows: Code: [Select] Sed nec neque quis sapien scelerisque aliquet. In in est odio, ac auctor erat. Vestibulum fringilla dapibus sodales. Morbi facilisis egestas arcu, eu semper mi luctus vitae. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse sollicitudin, metus sed hendrerit tristique, enim ante venenatis metus, ac consectetur mauris lorem et felis. Phasellus ac purus massa. Aliquam ligula mauris, iaculis et posuere vitae, pellentesque sit amet leo. Praesent at eros et lacus scelerisque feugiat. Aliquam non facilisis libero. Ut blandit leo ut tortor convallis porttitor. Aliquam erat volutpat. Aliquam rhoncus rutrum metus, tempor sollicitudin ante laoreet in. Sed tincidunt nibh a augue euismod sagittis. Fusce ullamcorper ultricies nisi, sit amet elementum mi scelerisque eget. Cras sed elit ligula, sit amet aliquam lectus. Nulla vel mollis nibh. Pellentesque ultricies, dui non aliquam eleifend, tortor ligula blandit dolor, quis interdum diam augue non eros. Nullam id molestie nunc. Integer id tempus est. What can I do to follow WordPress' formatting? I'm presently formatting surnames like this: Code: [Select] $surname = ucfirst(strtolower($surname))) .. so that SMITH, and smith ... both look like: Smith. That's all fine, but how do I ensure that... O'BRIEN o'brien o' brien ... all look like O'Brien ? That is, where no spaces are allowed to appear in the surname and where the first letter, as well as the one following the apostrophe are both capitalised. TIA Hi, When I do this the result is a date of zeros? $DateAndTime = date('d-m-y', strtotime($DateAndTime)); All I want is a date in format of DD0MM-YYY and the Time in HH:MM::SS Any help would be great! I know it is basic, Thanks in advance I want a little online users list at the bottom of my page and the color of the username is to match the color defined in the database. but how do i get them to align side by side? if i add them to a <p> they will all appear under neath each other. Heres my current code: $sql=mysql_query("SELECT * FROM ".DB_PREFIX."members WHERE online = 1"); while ($row = mysql_fetch_object($sql)) { echo '<p style="color: #'.$row->username_color.';">'.$row->username.'</p>'; } also i want a comma at the end of each but how do i stop it adding a comma to the end of the last one? Hi all Been trying to get my head around formatting a string, and struggling. I have a serious of numbers held in a DB and I need these to display as 5 figure numbers. if the DB held the record "48" I would need this to be displayed as "00048". Can anyone give me a clue. I've tried reading the php manual but this just confuses me more. Hi everyone, this is quite a big one, and I am new to programming. In the code below you will see I have created a 'Lucky Lotto Dip'. The display function allows the user to choose how many draws they want then by way of the shuffle() 6 numbers are generated randomly for each line. I have two questions. (1) I have managed by way of an if statement to automatically check the first number in the array $draw, how do I get it to check for 2 match's, 3 match's and so on up to 6 match's. And secondly if the first number is matched I have managed to get it to repeat itself and if to show a match, but how do I get it to repeat itself on the line below the selected numbers? I know this is difficult but if anyone can help I will be very grateful! Code: [Select] <?php class Game { public $draw = array(10,25,36,3,12,13); public $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49); public $age; public $count; function __construct($sub1, $sub2) { $this->name = $sub1; $this->age = $sub2; return $this->name;age; } function checkAge() { if ($this->age <18) { return "Sorry $this->name, you are too young to gamble"; } else { return "Hi $this->name welcome to Lucky Lotto Dip, how many draws would you like?"; } } function display($sub3) { shuffle ($this->numbers); $this->count = $sub3; echo "<pre>"; echo "*********************"; echo "<br />"; echo "* Lucky Lotto *"; echo "<br />"; echo "*********************"; while ($this->count >0) { echo "<br />"; echo "* "; for ($j =0; $j <6; $j++) // For loop if ($this->numbers[$j] <10) { echo " " . $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0]) { echo $this->numbers[$j] = $this->draw[0]; } } else { echo $this->numbers[$j] . " "; if ($this->numbers[$j] == $this->draw[0]) { echo $this->numbers[$j] = $this->draw[0]; } } echo "* "; echo "<br />"; echo "*********************"; echo "<br />"; echo " "; echo "<br />"; echo "*********************"; $this->count --; shuffle ($this->numbers); } } } $object2 = new Game("Chris", 36); echo $object2->checkAge(); echo $object2->display(4); ?> |