Help setting a default value

Discussion in 'PHP' started by Qal, May 24, 2008.

  1. #1
    Hi,

    Currently all products have no/0 value, but I want to modify it to have 10 as default value.

    This is the code currently:

    	/**
    	 * Sets the product rate field
    	 *
    	 * @param string $val the value to be put in product_rate
    	 */
    	function setProductRate($val) { $this->set('product_rate', $val); } 
    
    Code (markup):
    what do I need to change to set the default value as 10?

    Any help appreciated.

    Thanks
     
    Qal, May 24, 2008 IP
  2. wpc8mry73q

    wpc8mry73q Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi.

    Are these products you speak of instances of a class? Anyway, you could do the following:

    
    function setProductRate($val) {
    	if (!$val) $val = 10;
    	$this->set('product_rate', $val);
    }
    Code (markup):
    .. but you would still have to call the function for each product. If a product is a class, it usually has a constructor function where you can set the default product_rate.
    You can also call the original function by passing 10 as argument:

    
    setProductRate(10);
    Code (markup):
    /LTar
     
    wpc8mry73q, May 24, 2008 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function setProductRate($val = 10) { $this->set('product_rate', $val); }
    PHP:
     
    zerxer, May 24, 2008 IP