Jump to content

Recommended Posts

Hi Guys

 

I have created a function, well i think i have

 

function datedropdown_funct()
        {
                    switch ($date_filter) 
               {
                    
			    case "Today":
				    $retval = '<option value="">Select</option><option value="Today" SELECTED>Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option>';
			    break;
			    case "Last 48hrs":
				   $day = date("d")-1;
				   $retval = '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs" SELECTED>Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option>';
                    break;	
			    case "Last week":
    					$day = date("d")-7;
    					$retval =  '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week" SELECTED>Last Week</option><option value="last month">Last Month</option>';
                	break;
    				case "last month":
    					$month = date("m")-1;
    					$retval =  '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month" SELECTED>Last Month</option>';
                    break;
    				case "last 3 months":
    					$month = date("m")-3;
    					$retval = '<option value="">Select</option><option value="Today">Today</option><option value="Last 48hrs">Last 48hrs</option><option value="Last week">Last Week</option><option value="last month">Last Month</option><option value="last 3 months" SELECTED>Last 3 Month</option>';
                    break;
    				default:
    					// Default is the last 48hrs
    					$day = date("d")-1;
		        }  
                    return $retval;  
            }        

 

it gets called like so

 

$date_filter_options= datedropdown_funct(); 

 

The variable $date_filter_options should take on the $retval value, but it doesnt appear to be calling the function

 

Any ideas or correction where i have screwed up ?

 

regards

 

Link to comment
https://forums.phpfreaks.com/topic/218048-function-issue/
Share on other sites

Yeh you need to pass in that variable and any other non-global variables.

 

Also, I should add that what you return is very important when dealing with functions.

 

You are not return $month or $day etc which means you cannot access them outside of that function properly.

 

Ideally, because this is part of your other sql queries, it would be best to do a class, and then bundle a few functions together. That way you can easily manage variables between functions (or methods) by using it as an object.

 


class mynewclass{

  function datedropdown_funct(){
    $this->retval = 'Im retval!';  
    $this->saysomething_funct();
  }

  function saysomething_funct(){
    echo $this->retval;
  }


}


// USE

$class = mynewclass();
$class->datedropdown_funct();

 

You should be able to see the power there. This single class can very effectively manage all your needs for that page in the other thread.

 

Finally, if you ever find yourself repeating lots of stuff an awful lot, you should probably try to reduce to something abstract. In this piece, it would be the $retval html code.

Link to comment
https://forums.phpfreaks.com/topic/218048-function-issue/#findComment-1131550
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.