SitePoint Sponsor |
|
User Tag List
Results 1 to 2 of 2
-
Nov 12, 2004, 07:37 #1
- Join Date
- Oct 2002
- Posts
- 311
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Simple variable check in function not making any sense...
Hey,
Ok I am creating a shopping cart and I am having a problem with something. When a user goes to their basket, they can edit their quantity for items, I need a way of checking that what they enter in this textbox is a numeric/integer value.
For example, if they type in t4, I want it to strip the t and put 4 in the value...
If they type ta or anything that is not numeric, I want it to leave the value alone.
This is my current code for modifying products:
PHP Code:// Modify quantity of cart if done in checkout
function modify_quantity($product, $quantity) {
// Make sure quantity is an int
$quantity = $this->check_int($quantity);
// Quantity is less than 0, delete item from cart
if($quantity <= 0) return $this->delete_item($product);
// Limit amount of products to be ordered at one time
if($quantity > 30) return false;
// Check if quantity selected is greater than available
$chk = $this->check_quan($product, $quantity);
// Quantity selected is lower than available, ok
if($chk == 1) {
// No problems, update users cart
$query= "UPDATE tbl_basket
SET fld_amount='".$quantity."'
WHERE fld_cartid='".$this->cart_id."' AND fld_pid='".$product."'";
mysql_query($query, $this->dblink) or die ("Cannot select required information. "
// .mysql_error()
);
return true;
}
} // end function
This is the function I am currently trying out:
PHP Code:function check_int($value) {
// use this function to make sure a value is of the correct format (int)
// value is an integer, use it
if (is_numeric($value))
{
// Make sure value is numeric and change into an integer
$f_value = settype($value, 'integer');
return $f_value;
// value not an integer, do not use
} else {
return false;
}
} // end function
Any ideas?
Cheers,
Chris
-
Nov 12, 2004, 08:01 #2
You might try using a regular expression.
Look here for examples: http://www.regexlib.com/
Bookmarks