Jump to content

Recommended Posts

Hi,

My first post here as I am exploring the php world.

I am building a database mysql database filter and have run into a roadblock.

I have built a form that has multiple input options.

I would like to have the form return all table information if all of the inputs are left blank.

I would also like to have the table filter via any combination of responses the user inputs

I have been able to write a query in myphpadmin that returns the proper values, but have been unable to replicate it in the php environment.

I believe it ultimately has to do with my variable definitions, as I am trying to understand how to allow blank input sections:

 
	if($_GET) {
//Define Variables		
		$firstname="";
			if(!empty($firstname = FALSE));
			elseif(isset($_GET['first'])){ $firstname = $_GET['first'];
			}
		$lastname="";
			if(!empty($lastname = FALSE));
			elseif(isset($_GET['last'])){ $lastname = $_GET['last']; 
			}
		$bibsearch="";
			if(!empty($bibsearch = FALSE));
			elseif(isset($_GET['bib'])){ $bibsearch = $_GET['bib']; 
			}
		$divsearch="";
			if(!empty($division = FALSE));
			elseif(isset($_GET['division'])){ $divsearch = $_GET['division']; 
			}
		$sex="";
			if(!empty($sex = FALSE));
			elseif(isset($_GET['sex'])){ $sex = $_GET['sex']; 
			}

the query is:

$query = "SELECT * FROM `table 1` WHERE ( 
					`first` = '" . $firstname . "' || 
					`last` = '" . $lastname . "' || 
					`bib` = '" . $bibsearch . "' || 
					`division` = '" . $divsearch . "' || 
					`sex` = '" . $sex . "' )";

Any insight would be quite helpful.

Thanks for your time.

Best, 

 

Link to comment
https://forums.phpfreaks.com/topic/287655-multiple-filters/
Share on other sites

There are quite a few syntax errors in the PHP code. Are you getting any errors?

 

For what it's worth, the if statements should look more like this:

if(isset($_GET['first'])) { $firstname = $_GET['first']; }
else                      { $firstname = '';             }
 
You could also consider using the Ternary Operator:
if($_GET) {
     //Define Variables
     $firstname = (isset($_GET['first']))    ? $_GET['first']    : '';
     $lastname  = (isset($_GET['last']))     ? $_GET['last']     : '';
     $bibsearch = (isset($_GET['bib']))      ? $_GET['bib']      : '';
     $divsearch = (isset($_GET['division'])) ? $_GET['division'] : '';
     $sex       = (isset($_GET['sex']))      ? $_GET['sex']      : '';
}

 

Link to comment
https://forums.phpfreaks.com/topic/287655-multiple-filters/#findComment-1475669
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.