PHP Query changes query to filter

I have the following query that works 100%.  I have a form where you can pick a Material and a Process.  The form is submitted via AJAX.  The query as is SELECTS If the Materiel Field IS LIKE OR if the Process field IS LIKE.

Right now it returns any entries with either or.  I want to keep that so if a user only picks a material or if they only pick a process.  However, if they choose both, I want to filter the entries to only those that have the two variables in common.  Make sense?

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$material = '';
$process = '';

$action = $_GET["action"];
if ($action == "app_finder") {

	if (!empty($_GET['material'])) {
		$material = $_GET['material'];
	} else {
		$material = 'NONE';
	}
	if (!empty($_GET['process'])) {
		$process = $_GET['process'];
	} else {
		$process = 'NONE';
	}
	

	 //  echo '<h2>Material:' . $material . '</h2><br />';

	//  echo '<h2>Process:' . $process . '</h2><br /><br />';


	$app_query =  ee() -> db -> select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1101 AS description, t1.field_id_1141 AS image, t1.field_id_3801 AS material, t1.field_id_3821 AS process, t2.url_title AS url_title') 
	->from('exp_channel_data t1') 
	->like('t1.field_id_3801', $material) 
	->or_like('t1.field_id_3821', $process)
	->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id') 
	->get();

if ($app_query->num_rows() > 0) 
    { 

	$lb = $app_query -> result_array();

	foreach ($lb as $row) {
		$id = $row['id'];
		$material = $row['material'];
		$process = $row['process'];
		$image = $row['image'];
		$description = $row['description'];
		$img = end(explode('{filedir_51}', $image));
		$title = $row['title'];
		$url_title = $row['url_title'];

		$laser_cat_query =  ee() -> db -> select('t3.entry_id AS eid, t3.cat_id AS cat_id, t4.cat_url_title AS cat_url') 
		-> from('exp_category_posts t3') 
		-> where('t3.entry_id', $id) 
		-> join('exp_categories t4', 't4.cat_id = t3.cat_id') 
		-> limit(1) 
		-> get();

		foreach ($laser_cat_query->result_array() as $row_b) {
			$cat_url = $row_b['cat_url'];
			$cat_id = $row_b['cat_id'];

			
					echo '<div class="result-box" >
							<div class="app-content">
								<div class="title">
									<a href="{site_url}/' . $cat_url . '/laser/' . $url_title . '">' . $title . '</a> 
								</div>
								{exp:excerpt limit="50"}'.$description.'{/exp:excerpt} &nbsp;...
							</div>
   		   						<div class="image">';
									if (!empty($image)) {
										echo '<img src="{site_url}/assets/product_images/' . $img . '" /><br />';
									} else {
									echo '<img src="{site_url}/assets/site_images/image_coming_soon.png" alt="Coherent Lasers" />';
									}
					echo '</div></div>';
				} //  END Loop
			}
		} else {
    	echo "<h3>Your search returned no results.</h3>";
    }

Open in new window

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
So you want something like this ?
if (!empty($material) && !empty($quantity)) {
   $filter = "material LIKE '{$material}' AND quantity LIKE '{$quantity}'";
}
else {
   $fileter = "material LIKE '{$material}' OR quantity LIKE '{$quantity}'";
}

Open in new window

0
Julian HansenCommented:
In the context of your code you can basically use the if to determine if you should do an or_like or an and_like on line 31.

if ($material != 'NONE' && $quantity != 'NONE') {
$app_query =  ee() -> db -> select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1101 AS description, t1.field_id_1141 AS image, t1.field_id_3801 AS material, t1.field_id_3821 AS process, t2.url_title AS url_title') 
	->from('exp_channel_data t1') 
	->like('t1.field_id_3801', $material) 
	->and_like('t1.field_id_3821', $process)
	->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id') 
	->get();
}
else {
$app_query =  ee() -> db -> select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1101 AS description, t1.field_id_1141 AS image, t1.field_id_3801 AS material, t1.field_id_3821 AS process, t2.url_title AS url_title') 
	->from('exp_channel_data t1') 
	->like('t1.field_id_3801', $material) 
	->or_like('t1.field_id_3821', $process)
	->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id') 
	->get();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
Why did I not think of that!
0
Julian HansenCommented:
We can't all think of everything which is why we have scrums and forums and Experts Exchange :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.