Why is this code not working?

Discussion in 'PHP' started by dherald, Sep 8, 2009.

  1. #1
    "prices" => array
    (
    if((date("H") < 10))
    (
    "7200" => "0.02",)


    "86400" => "0.95",
    "432000" => "2.22",
    "2592000" => "4.13"


    ),

    __________________________

    It works without this part

    if((date("H") < 10))
    (
    "7200" => "0.02",)

    Am I doing something wrong? I basically want it to display the 0.02 price from 00:00 to 10:00
     
    dherald, Sep 8, 2009 IP
  2. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #2
    It's has syntax error, you can't write if statement into array definition. (Or these are slices?)
     
    SubZtep, Sep 8, 2009 IP
  3. dherald

    dherald Active Member

    Messages:
    408
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    This is a part of a huge code and it works without the HOUR part for some reason.
     
    dherald, Sep 8, 2009 IP
  4. icyshout

    icyshout Peon

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hard to tell from just this little snippet
     
    icyshout, Sep 8, 2009 IP
  5. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #5
    Do you mean similar like this?
    
    $prices = array(
    	"86400" => "0.95",
    	"432000" => "2.22",
    	"2592000" => "4.13"
    );
    if((date("H") < 10)) {
    	$prices["7200"] = "0.02";
    	asort($prices);
    }
    
    Code (markup):
     
    SubZtep, Sep 8, 2009 IP
  6. dherald

    dherald Active Member

    Messages:
    408
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #6
    	"prices" => array
    				(
    
    						
    					"7200" => "0.17",
    					"18000" => "0.35",
    					"86400" => "0.95",
    					"432000" => "1.49",
    					"2592000" => "2.99"
    				
    
    				),
    Code (markup):
    This is what I have right now and it works.

    But when I try to do something like this:

    	"prices" => array
    				(
    if((date("H") < 10))
    (
    "7200" => "0.02",)
    						
    					"7200" => "0.17",
    					"18000" => "0.35",
    					"86400" => "0.95",
    					"432000" => "1.49",
    					"2592000" => "2.99"
    				
    
    				),
    Code (markup):
    It doesn't work. I just want to add the "7200" => "0.02", field when the time is < 10h.
     
    dherald, Sep 8, 2009 IP
  7. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #7
    You can't enter any code into an array definition. You can add your value after the definition. Check my previously post.
     
    SubZtep, Sep 8, 2009 IP
  8. dherald

    dherald Active Member

    Messages:
    408
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #8
    Ok thanks a lot.

    But how can I apply this without changing the array into the $prices = array(-style code that you mentioned?
     
    dherald, Sep 8, 2009 IP
  9. SubZtep

    SubZtep Member

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    48
    #9
    Not easy, because I can't see the code, but I think you want to show smallest value before 10h.
    Your array is this:
    
    $stuff = array(
    	"prices" => array(
    		"7200" => "0.17",
    		"18000" => "0.35",
    		"86400" => "0.95",
    		"432000" => "1.49",
    		"2592000" => "2.99"
    	)
    );
    
    Code (markup):
    For replace the value, just put this code after array:
    
    if((date("H") < 10)) {
    	$stuff["prices"]["7200"] = "0.02";
    }
    
    Code (markup):
    You can check finally like this:
    
    print_r($stuff);
    
    Code (markup):
     
    Last edited: Sep 8, 2009
    SubZtep, Sep 8, 2009 IP