Validating Multiple-Choice Input
Checkboxes and drop-down lists are an important component of web forms, and it's often necessary to include validation for these controls in your PHP applications. Normally, the user's selections from these controls are submitted to the form processor in the form of an array, and it's necessary to use PHP's array functions to validate them. To see this in action, consider the following script, which requires the user to fill out a brief user profile and select at least three hobbies and two...
Using the UNIQUE Modifier
Using MySQL's built-in validation mechanisms has an important advantage it makes it easy to perform certain types of validation that would be lengthy and time-consuming to write code for. Consider, for example, the situation of ensuring that a particular field contains only unique values. MySQL makes it possible to do this, simply by attaching a UNIQUE modifier to the field, as in the following example - gt username VARCHAR 50 NOT NULL UNIQUE Query OK, 0 rows affected 0.06 sec mysql gt INSERT...
Using Decimal and Comma Separators
When it comes to formatting numeric values in PHP, there are only two functions number_format and sprintf . Of these, the former is easier to understand and use, so let's begin with that function. The number_format function is used to display large numbers with comma and decimal separators. It can be used to control both the visibility and the appearance of the decimal digits, as well as the character used as the thousands separator. To see how this works, consider the following table mysql gt...
Rounding Off
If you have a decimal value that you need to round up or down, you can do it using either PHP or MySQL. MySQL offers the CEIL and FLOOR functions, while PHP offers the round , ceil , and floor functions. Take a look at the following examples to see how these functions work ceil 12.052 floor 12.052 -------------- -------------- 13 12 -------------- -------------- the second argument specifiesJ the number of decimals to round to echo round 12.052, 1 Formatting numbers with the printf function...
