Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
Donovan
Client



Joined: Oct 07, 2003
Posts: 735
Location: Ohio

PostPosted: Thu Nov 29, 2007 9:28 am Reply with quote

Is there any preferred way to validate decimal values before writing them to the db?

Currently I have..

$irat_wt = $_POST['irat_wt'];
$grat_wt = $_POST['grat_wt'];
$appex_wt = $_POST['appex_wt'];

but all _POST values are a string aren't they?

These variable will hold the values of what weights of test results for different exam.

irat = individual readiness test
grat = group readiness test
appex = application exercise

Right now they are all data type decimal (3,2).

They will vote at the beginning of each year and decide how much the irat, grat, and appex is worth to their overall grade.

I was using values such as irat = 0.20, grat = 0.40, appex = 0.40

If I edit these values I want to ensure I validate the data before I update the table.

I guess I can decrease the length of the data type to 2,2 so I only have values such as .20 and .40.

Will intval() suffice?
 
View user's profile Send private message Visit poster's website ICQ Number
gotcha
Regular
Regular



Joined: Mar 14, 2005
Posts: 91

PostPosted: Thu Nov 29, 2007 10:06 am Reply with quote

intval will turn it into a whole number, removing the decimal point. I think the function you want to look at is number_format().
 
View user's profile Send private message Visit poster's website
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Thu Nov 29, 2007 2:04 pm Reply with quote

floatval may also be a function to take a look at, depending on what you want to do

_________________
- Star Wars Rebellion Network -

Need help? Nuke Patched Core, Coding Services, Webmaster Services 
View user's profile Send private message Visit poster's website
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Fri Nov 30, 2007 10:08 pm Reply with quote

Raven tends to recommend the PHP ctype functions. Some reasons why:

"It should be noted that ctype functions are always preferred over regular expressions, and even to some equivalent str_* and is_* functions. This is because of the fact that ctype uses a native C library and thus processes significantly faster."

I could have sworn too that they are "rock solid", but I cannot recall the reference.

_________________
Where Do YOU Stand?
HTML Newsletter::ShortLinks::Mailer::Downloads and more... 
View user's profile Send private message Visit poster's website
Gremmie
Former Moderator in Good Standing



Joined: Apr 06, 2006
Posts: 2415
Location: Iowa, USA

PostPosted: Fri Nov 30, 2007 10:19 pm Reply with quote

Unfortunately there is no ctype function for floating point numbers. Sad

If you are trying to prevent SQL injection and you know the value should be a float, I would use floatval() like evaders suggests. That will turn the string into a float, or 0 if it isn't a float. Then you could use sprintf to format it the way you want for the SQL query.

Code:


$x = sprintf('%5.2f', floatval($x));
$sql = "UPDATE ........... SET something = $x";

_________________
GCalendar - An Event Calendar for PHP-Nuke
Member_Map - A Google Maps Nuke Module 
View user's profile Send private message
montego







PostPosted: Sat Dec 01, 2007 9:07 am Reply with quote

Ah, yes, sorry. Embarassed That is a bummer Sad
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©