Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Currently on a project where the user search data by date in database.

My form has 2 options, either the user choose to search by select box or input fields from a date to another date.

After written my code I figure out that my function is getting to long.
Have a look here, any idea that I can shorten the function?
Also, this function is only for the first options, if I do the second one it will be much longer. Sorry I am still new to programming.

PHP
// Search Form
		if(isset($_POST['go']))
		{
			$myRadio = $_POST['myRadio'];
			$selectedOption = $_POST['mySelect'];
			if($myRadio == 'choose') {				
				$this->view->getOption = $selectedOption . ': ';
				if($selectedOption == 'Today') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)');
					$this->view->getTodayData = $getDateData;
				} elseif($selectedOption == 'Yesterday') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 2 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last 7 days') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'This week') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where WEEKOFYEAR(created_at) = WEEKOFYEAR(NOW())');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last week') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where WEEKOFYEAR(created_at) = WEEKOFYEAR(NOW())-1');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last 30 days') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where created_at > DATE_SUB(NOW(), INTERVAL 30 DAY)');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'This month') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where MONTH(created_at) = MONTH(NOW())');
					$this->view->getDateData = $getDateData;
				} elseif($selectedOption == 'Last month') {
					$getDateData = $db->fetchOne('select count(id) as total_users from users where MONTH(created_at) = MONTH(NOW())-1');
					$this->view->getDateData = $getDateData;
				}
				
				
			} elseif($myRadio == 'choose2') {
				//second choice
			} else {
				$this->view->emptySelect = 'Select one of the following options...';
			}
		}
	}
Posted
Comments
DinoRondelly 15-Apr-13 11:21am    
Try a switch statement,

switch($selectedOption)
{
case 'Last month':
break;
}
Yafa Su 15-Apr-13 11:31am    
Oh yea, thanks it looks more cleaner using this one.

1 solution

Here is a solution that might help.

You can make a common generalized getSearchDateRange function it will return you an array having search start date and end date and use these values in your query.

In this way you can use this search range function anywhere you want to use.
You can get the "From and To" date using $_POST.

PHP
function getSearchDateRange() {

	Then get the date box value 
	//gettting all the "from "date information from the filter
	$searchBeginDay = $_POST['search_begin_date'];
	$searchBeginMonth = $_POST['search_begin_month'];
	$searchBeginYear = $_POST['search_begin_year']; 

	//gettting all the "to" date information from the filter
	$searchEndDay = $_POST['search_end_date'];
	$searchEndMonth = $_POST['search_end_month'];
	$searchEndYear = $_POST['search_end_year'];

	//getting the value from the date range selecter
	$selectedOption = $_POST['mySelect'];

	//getting the start date
	if($selectedOption == '') {

                // to take a default date range if no start date is given in search
    
        	$date = date_create(date('Y-m-d'));
        	$searchRange = '90 days';
        	date_sub($date, date_interval_create_from_date_string($searchRange));
        	$searchStartDate = date_format($date, 'Y-m-d');
		
        	$searchBeginDay = 'dd';
        	$searchBeginMonth = 'mm';
        	$searchBeginYear = 'yyyy';
		
	} else if($selectedOption == '30') {

		// to take a default date range if no start date is given in search

		$date = date_create(date('Y-m-d'));
		$searchRange = '30 days';
		date_sub($date, date_interval_create_from_date_string($searchRange));
		$searchStartDate = date_format($date, 'Y-m-d');
		
		$searchBeginDay = 'dd';
		$searchBeginMonth = 'mm';
		$searchBeginYear = 'yyyy';
	} else if($selectedOption == '3') { 
                //here 3 is the value when previous year is the search start date
			
		//previous year
		$thisYear =  date('Y');
		$lastYear = $thisYear - 1;
		$searchStartDate = date($lastYear."-01-01");
		
		$searchBeginDay = 'dd';
		$searchBeginMonth = 'mm';
		$searchBeginYear = 'yyyy';
		
	}else {
		//put default search range
	}

	/* put as many condition as you want*/

	//geting the end date

	if($selectedOption == '3') { 
               //here 3 is the value when previous year last date is the search end date
			
		//previous year
		$thisYear =  date('Y');
		$lastYear = $thisYear - 1;
		$searchEndDate  = date($lastYear."-12-31");
		
		$searchEndDay = 'dd';
		$searchEndMonth = 'mm';
		$searchEndYear = 'yyyy';
	}else {

		//checking if the date/month/year has numeric value				
		if(is_numeric($searchEndDay)&& is_numeric($searchEndMonth) && is_numeric($searchEndYear)) {

		$searchEndDate = $searchEndYear.'-'.$searchEndMonth.'-'.$searchEndDay; 
		}else{

	// to take today as default date if no end date is given in search
			
			$date = date_create(date('Y-m-d'));	
			$searchEndDate = date_format($date, 'Y-m-d');

			$searchEndDay = 'dd';
			$searchEndMonth = 'mm';
			$searchEndYear = 'yyyy';
		}
	} /* put as many condition as you want*/

	$searchDateRange['startDate'] = $searchStartDate;
	$searchDateRange['endDate'] = $searchEndDate;
	return $searchDateRange;
}

By calling this function you get start and end date for search .So no need to write the query again and again.
PHP
$getDateData = $db->fetchOne("select count(id) as total_users from users where created_at > $searchDateRange['startDate'] AND created_at < $searchDateRange['endDate']");
 
Share this answer
 
v2
Comments
Yafa Su 16-Apr-13 9:35am    
Wow, that what I am looking for XD thanks man. I will try your example.
debasis behera 8-May-13 5:58am    
Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...

Thanks,
Debasis
Nice Answer. +5 ed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900