Jump to content

Recommended Posts

I've got a form that a user fills out, selects a start date, end date, IP, username, url, and action. They can also choose ALL for start date and end date, along with action. The other fields are optional and not required.

 

So I want to create the query to get the data they want from a database. There is a chance that the data could be ALL, so I can't just send the variables directly in. This is where my question about logic comes on. Right now, I have an insane block of if statements, and I'm not sure if it is right. Firstly I check to see if the start and end date are ALL, in which case, two booleans get set to true. Then I check if the optional fields (IP, username, url) are set, and switch 3 more booleans to true. Then I check if action is ALL, or another value. Another boolean. Here comes the checking.

 

The only way I see it is that I'd have to check a few dozen times for each possible combination. For example, if startIsAll and endIsAll, then check if I have an IP, username, and url. If not, check what the action type is = if ALL, I can generate a query. If something else, generate that different query.

 

Then move on to the next combinations - if I have say an IP, but no username or URL. Then I've got to check again if the 'action' is ALL, or something else. Redundancy. I just copy-paste the statements over. When that's done, I have to check the other combinations: if IP exists and username exists but no URL; if all 3 exist, or any other flavor.

 

Finally after that's done, I've got to go back and check if the start date is ALL and maybe the end date is a specific. Then, do all that again.

 

Am I approaching this correctly? Is it just this convoluted by nature?

Link to comment
https://forums.phpfreaks.com/topic/56436-new-to-php-question-about-logic/
Share on other sites

Here it is, in all of its convoluted glory. I don't think there is an easier way to do it.

 

$ip = $username = "";

$start_year = $_POST['startyear'];
$start_month = $_POST['startmonth'];
$start_day = $_POST['startday'];

$end_year = $_POST['endyear'];
$end_month = $_POST['endmonth'];
$end_day = $_POST['endday'];

$startdate = $start_year . "-" . $start_month . "-" . $start_day;
$enddate = $end_year . "-" . $end_month . "-" . $end_day;

$startIsAll = false;
$endIsAll = false;

if( $startdate == "ALL-ALL-ALL" ) 
$startIsAll = true;
if( $enddate == "ALL-ALL-ALL" ) 
$endIsAll = true;

$hasIP = false;
$hasUsername = false;

$ip = trim($_POST['ip']);
$username = trim($_POST['username']);

if(!empty($ip)) 
$hasIP = true;
if(!empty($username)) 
$hasUsername = true;

$action = $_POST['action'];

$query = "";

if( $startIsAll && $endIsAll ) {
if( !$hasIP && !$hasUsername ) {
	if( $action == "ALL" )
		$query = "SELECT * FROM logs";
	else if( $action == "exception" )
		$query = "SELECT * FROM logs WHERE message='*EXCEPTION*'";
	else if( $action == "denied" )
		$query = "SELECT * FROM logs WHERE message='*DENIED*'";
} else {
	if( $hasIP && !$hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "')";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "')";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "')";
	} else if( $hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND user = '" . $username . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND user = '" . username . "'";
	} else if( !$hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE user = '" . $username . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND user = '" . $username . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND user = '" . $username . "'";
	} 
}
} else if( !$startIsAll && $endIsAll ) {
if( !$hasIP && !$hasUsername ) {
	if( $action == "ALL" )
		$query = "SELECT * FROM logs WHERE AND date>='" . $startdate . "'";
	else if( $action == "exception" )
		$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND date>='" . $startdate . "'";
	else if( $action == "denied" )
		$query = "SELECT * FROM logs WHERE message='*DENIED*' AND date>='" . $startdate . "'";
} else {
	if( $hasIP && !$hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND date>='" . $startdate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND date>='" . $startdate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND date>='" . $startdate . "'";
	} else if( $hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date>='" . $startdate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date>='" . $startdate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date>='" . $startdate . "'";
	} else if( !$hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE user = '" . $username . "' AND date>='" . $startdate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' WHERE user = '" . $username . "' AND date>='" . $startdate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' WHERE user = '" . $username . "' AND date>='" . $startdate . "'";
	} 
}
} else if( $startIsAll && !$endIsAll ) {
if( !$hasIP && !$hasUsername ) {
	if( $action == "ALL" )
		$query = "SELECT * FROM logs WHERE AND date<='" . $enddate . "'";
	else if( $action == "exception" )
		$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND date<='" . $enddate . "'";
	else if( $action == "denied" )
		$query = "SELECT * FROM logs WHERE message='*DENIED*' AND date<='" . $enddate . "'";
} else {
	if( $hasIP && !$hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND date<='" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND date<='" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND date<='" . $enddate . "'";
	} else if( $hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date<='" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date<='" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date<='" . $enddate . "'";
	} else if( !$hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE user = '" . $username . "' AND date<='" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' WHERE user = '" . $username . "' AND date<='" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' WHERE user = '" . $username . "' AND date<='" . $enddate . "'";
	}
}
} else if( !$startIsAll && !$endIsAll ) {
if( !$hasIP && !$hasUsername ) {
	if( $action == "ALL" )
		$query = "SELECT * FROM logs WHERE date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
	else if( $action == "exception" )
		$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
	else if( $action == "denied" )
		$query = "SELECT * FROM logs WHERE message='*DENIED*' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
} else {
	if( $hasIP && !$hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
	} else if( $hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' AND ip=INET_ATON('" . $ip . "') AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' AND ip=INET_ATON('" . $ip . "') AND user = '" . $username . "' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
	} else if( !$hasIP && $hasUsername ) {
		if( $action == "ALL" )
			$query = "SELECT * FROM logs WHERE user = '" . $username . "' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "exception" )
			$query = "SELECT * FROM logs WHERE message='*EXCEPTION*' WHERE user = '" . $username . "' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
		else if( $action == "denied" )
			$query = "SELECT * FROM logs WHERE message='*DENIED*' WHERE user = '" . $username . "' AND date BETWEEN '" . $startdate . "' AND '" . $enddate . "'";
	}
}
} else {
echo "You did something bad.";
}

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.