PHP Query For Each

My query below works fine on the first foreach loop but does not work on any additional iterations.  Where it is breaking is at the if(wavelength)

	$laser_query = ee()->db->select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1141 AS image, t1.field_id_3781 AS wavelength, t2.url_title AS url_title')
		->from('exp_channel_data t1')
		->where('t1.field_id_3731', $cw)
		->or_where('t1.field_id_3731', $pulsed)
		->or_where('t1.field_id_3731', $pulsednp)
		->or_where('t1.field_id_3731', $quasi)
		->or_where('t1.field_id_3731', $ultrafastfp)
		->or_where('t1.field_id_3731', $ultrafastf)
		->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id')
		->get();
		
		$lb = $laser_query->result_array();
		
		foreach($lb as $row){
   			$id = $row['id'];
   			$image = $row['image'];
   			$img  = end(explode('{filedir_51}', $image));
   			$title = $row['title'];
   			$url_title = $row['url_title'];
   			$wave = $row['wavelength'];
   			$wave = explode(', ', $wave);
   			echo'<br />'.$wave[0].'<br />'.$wave[1].'<br />';

   			$a = false;
   			$b = false; 

   			if($wave[0] <= $wave_max) $a = true; 
   			if($wave[1] >= $wave_min) $b = true; 
   			
   			if($a && $b) $wavelength = true;
   			
   			if($wavelength) {
echo "HELLO";
}
}

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.

Ray PaseurCommented:
What is the reason you think it is "breaking?"  What is the var_dump() output that tells us the contents of $lb?
0
Dave BaldwinFixer of ProblemsCommented:
You're probably getting an 'undefined variable' because you didn't give $wavelength a default value before you tried to use it in the 'if' statement.  If line 30 isn't true then $wavelength has no value.
0
rgranlundAuthor Commented:
Here is the entire code.  It works as it should the first time it loops, the rest of the time it automatically sets $wavelength to true, no matter what.  I also need it to work if $wave[0] or $wave[1] are empty.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$cw ='';
			$pulsed = '';
			$pulsednp = '';
			$quasi = '';
			$ultrafastfp = '';
			$ultrafastf = '';
			$wave_max = '';
			$wave_min = '';
			$a = '';
   			$b = ''; 
			$wavelength = '';

$action=$_GET["action"];
	if($action=="laser_finder") {
		if(isset($_GET['cw'])) {
			$cw = $_GET['cw'];
		} else {
			$cw='NONE';
		}
		if(isset($_GET['pulsed'])) {
			$pulsed = $_GET['pulsed'];
		} else {
			$pulsed = 'NONE';
		}
		if(isset($_GET['pulsednp'])) {
			$pulsednp = $_GET['pulsednp'];
		} else {
			$pulsednp = 'NONE';
		}
		if(isset($_GET['quasi'])) {
			$quasi = $_GET['quasi'];
		} else {
			$quasi = 'NONE';
		}
		if(isset($_GET['ultrafastfp'])) {
			$ultrafastfp = $_GET['ultrafastfp'];
		} else {
			$ultrafastfp = 'NONE';
		}
		if(isset($_GET['ultrafastf'])) {
			$ultrafastf = $_GET['ultrafastf'];
		} else {
			$ultrafastf = 'NONE';
		}
		if(isset($_GET['wave_max'])) {
			$wave_max = $_GET['wave_max'];
		} else {
			$wave_max = "";
		}
		if(isset($_GET['wave_min'])) {
			$wave_min = $_GET['wave_min'];
		} else {
			$wave_min = "";
		}
		
echo '<h2>Wave Max:'. $wave_max.'<br />Wave Min: '.$wave_min.'</h2><br /><br />';

	$laser_query = ee()->db->select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1141 AS image, t1.field_id_3781 AS wavelength, t2.url_title AS url_title')
		->from('exp_channel_data t1')
		->where('t1.field_id_3731', $cw)
		->or_where('t1.field_id_3731', $pulsed)
		->or_where('t1.field_id_3731', $pulsednp)
		->or_where('t1.field_id_3731', $quasi)
		->or_where('t1.field_id_3731', $ultrafastfp)
		->or_where('t1.field_id_3731', $ultrafastf)
		->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id')
		->get();
		
		$lb = $laser_query->result_array();
		
		foreach($lb as $row){
   			$id = $row['id'];
   			$image = $row['image'];
   			$img  = end(explode('{filedir_51}', $image));
   			$title = $row['title'];
   			$url_title = $row['url_title'];
   			$wave = $row['wavelength'];
   			$wave = preg_replace('[\s]',',', $wave);
   			$wave = preg_replace('/[^0-9,]/','', $wave);
   			$wv = array_filter(explode(",", $wave));
   			$wave = implode(', ', $wv);
   						   		
   		 		$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'];	
   		 	
   		 	
   		 	
   		 	//  START Loop 
   		 			if ($title !="") {
   		 				$wave = explode(', ', $wave);
   			echo'<br />'.$wave[0].'<br />'.$wave[1].'<br />';

			
   			if($wave[0] <= $wave_max) $a = true; 
   			if($wave[1] >= $wave_min) $b = true; 
   			
   			if($a && $b) $wavelength = true;
   			
   			if($wavelength == true) {
   					
   			echo'<h2>HELLO</h2>';
   		 				echo '<div class="result-box" >
   		   				<div class="image">';
 						echo '<img src="{site_url}/assets/product_images/'.$img .'" /><br />';
						echo'</div>
								<div class="title">
									<a href="{site_url}/lasers/laser/'.$cat_url.'/'.$url_title.'">'.$title.'</a> 
								</div>
							</div>';
						}	//  END Loop
					}  //  END If Wavelength
				}	
			}
   		} //  END IF Form has been submitted  
?>

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Daniel WilsonCommented:
line 107 had no else statement.  Once $wavelength is true, it is never again false.
2
Dave BaldwinFixer of ProblemsCommented:
Daniel is right.  You could put $wavelength = ''; as line 106 just before the 'if' so it always gets reset each time thru the loop.  Then it will only be set 'true' if line 107 is true.
2
Julian HansenCommented:
Any particular reason you are using or_where() rather than where_in()?
0
rgranlundAuthor Commented:
@Julian I'm using or_where cause that is what the code igniter documentation said to use.  Any or None  of those fields can be chosen.  Is Or_Where not correct?
0
Ray PaseurCommented:
Thanks for posting the code, but more important than the code is the data and that is why I posted this comment asking for the data.  If you'll work with us here we may be able to understand the issues faster and get you closer to a good solution.
0
Julian HansenCommented:
At the risk of taking a slight deviation ...
Is Or_Where not correct?
It is not incorrect - just verbose for this implementation. In sql you can say
WHERE id = 5 or id = 7 or id = 9 or id = 21;

Open in new window

OR you could do
WHERE id in (5,7,9,21)

Open in new window

CI makes provision for both - refer documentation here (https://ellislab.com/codeigniter/user-guide/database/active_record.html)

Your or_where is basically using the same field repeatedly in each of the or_where's so you could do it something like this
$ids = array($cw, $pulsed, $pulsednp, $quasi, $ultrafastfp, $ultrafastf);
$laser_query = ee()->db->select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1141 AS image, t1.field_id_3781 AS wavelength, t2.url_title AS url_title')
		->from('exp_channel_data t1')
		->where_in('t1.field_id_3731', $ids)
		->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id')
		->get();

Open in new window


Then for your $wavelength use
 if($a && $b) $wavelength = true;
 if($wavelength) {
  echo "HELLO";
 }

Open in new window

As Dave mentioned if either $a is false or $b is false then $wavelength will be undefined. However if $a and $b are true on the first iteration subsequent iterations $wavelength will be defined and will carry over from the previous iteration - resulting in an unwanted side effect.

I would therefore do this

$wavelength = ($a && $b);

Open in new window


This has the following benefit
a) $wavelength is assigned a value on each iteration - no side effects
b) Not as verbose as using the if statement.
0
rgranlundAuthor Commented:
@All.  Thanks for the help. I tried to organize the data for Ray but figured it would be better to show the link so you can see it in action.

The only three fields that should work right now is CW Continuous Wave and Min-Max Wavelength.  I have echoed out, above the image what the min and max is for each item.

One of the things I'm not sure is clear or not is that the Min and Max do not both have to be set.  A max can be set or a min can be set or both.  Also, the query should not be dependent on field_id_3731 being set.  It should fire if any of the parameters are set.  Meaning none are mandatory but the AJAX does not fire until at least one of them is set.

My Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$cw ='';
			$pulsed = '';
			$pulsednp = '';
			$quasi = '';
			$ultrafastfp = '';
			$ultrafastf = '';
			$wave_max = '';
			$wave_min = '';
			$wavelength = '';

$action=$_GET["action"];
	if($action=="laser_finder") {
		if(isset($_GET['cw'])) {
			$cw = $_GET['cw'];
		} else {
			$cw='NONE';
		}
		if(isset($_GET['pulsed'])) {
			$pulsed = $_GET['pulsed'];
		} else {
			$pulsed = 'NONE';
		}
		if(isset($_GET['pulsednp'])) {
			$pulsednp = $_GET['pulsednp'];
		} else {
			$pulsednp = 'NONE';
		}
		if(isset($_GET['quasi'])) {
			$quasi = $_GET['quasi'];
		} else {
			$quasi = 'NONE';
		}
		if(isset($_GET['ultrafastfp'])) {
			$ultrafastfp = $_GET['ultrafastfp'];
		} else {
			$ultrafastfp = 'NONE';
		}
		if(isset($_GET['ultrafastf'])) {
			$ultrafastf = $_GET['ultrafastf'];
		} else {
			$ultrafastf = 'NONE';
		}
		if(isset($_GET['wave_max'])) {
			$wave_max = $_GET['wave_max'];
		} else {
			$wave_max = "";
		}
		if(isset($_GET['wave_min'])) {
			$wave_min = $_GET['wave_min'];
		} else {
			$wave_min = "";
		}
		
echo '<h2>Wave Max:'. $wave_max.'<br />Wave Min: '.$wave_min.'</h2><br /><br />';

	$ids = array($cw, $pulsed, $pulsednp, $quasi, $ultrafastfp, $ultrafastf);
	
	$laser_query = ee()->db->select('t1.entry_id AS id, t1.field_id_1091 AS title, t1.field_id_1141 AS image, t1.field_id_3781 AS wavelength, t2.url_title AS url_title')
		->from('exp_channel_data t1')
		->where_in('t1.field_id_3731', $ids)
		->join('exp_channel_titles t2', 't2.entry_id = t1.entry_id')
		->get();
		
		$lb = $laser_query->result_array();
		
		foreach($lb as $row){
   			$id = $row['id'];
   			$image = $row['image'];
   			$img  = end(explode('{filedir_51}', $image));
   			$title = $row['title'];
   			$url_title = $row['url_title'];
   			$wave = $row['wavelength'];
   			$wave = preg_replace('[\s]',',', $wave);
   			$wave = preg_replace('/[^0-9,]/','', $wave);
   			$wv = array_filter(explode(",", $wave));
   			$wave = implode(', ', $wv);
   				
   		 			   		
   		 		$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'];	
   		 	
   		 	
   		 	//  START Loop 
   		 			if ($title !="") {
   		 				$wave = explode(', ', $wave);
   			echo'<hr><br />'.$wave[0].'<br />'.$wave[1].'<br />';

			$a = false;
   			$b = false; 
   			if($wave[0] <= $wave_max) $a = true; 
   			if($wave[1] >= $wave_min) $b = true; 
   			
   			$wavelength = '';
   				
   			if($a && $b) $wavelength = true;
   			
   			if($wavelength == true) {
   					
   			echo'<h2>HELLO</h2>';
   		 				echo '<div class="result-box" >
   		   				<div class="image">';
 						echo '<img src="{site_url}/assets/product_images/'.$img .'" /><br />';
						echo'</div>
								<div class="title">
									<a href="{site_url}/lasers/laser/'.$cat_url.'/'.$url_title.'">'.$title.'</a> 
								</div>
							</div>';
						}	//  END Loop
					}  //  END If Wavelength
				}	
			}
   		} //  END IF Form has been submitted  
?>

Open in new window


LINK:
http://cohr-dev-ee01.azurewebsites.net/lasers

The form is down toward the bottom on the left.
I added an <hr> to the code so you can see on the page where one stops and another starts.
0
Julian HansenCommented:
One thing - line 49 of your html source you have
event.preventDefault();

Open in new window

Looks like some orphan code - refers to event that is not defined anywhere.

In addition there is another error that is occurring - fix those first.
0
rgranlundAuthor Commented:
I fixed the first one, however, I'm not sure about the second one:
A PHP Error was encountered

Severity: Notice
Message: Undefined offset: 1
Line Number: 96
I believe Line number 96 has something to do with these lines.
$wave = explode(', ', $wave);
                  $a = false;
                     $b = false;
                     if($wave[0] <= $wave_max) $a = true;
                     if($wave[1] >= $wave_min) $b = true;
                     
                     $wavelength = '';

As I mentioned, they are not mandatory.  I'm not really sure how to fix it.  I have done some updates but I think I am spinning my wheels.


Updated PHP:
$cw ='';
			$pulsed = '';
			$pulsednp = '';
			$quasi = '';
			$ultrafastfp = '';
			$ultrafastf = '';
			$wave_max = '';
			$wave_min = '';
			$wavelength = '';

Open in new window

0
Julian HansenCommented:
                   if($wave[1] >= $wave_min) $b = true; 

Open in new window

The error is saying that t here was no index $wave[1] - you need to dump the $wave var to see what is in it. Then you need to look at your data - is it always the case you should have at least 2 values in $wave - if so then what is causing only 1 value to be created.

You can dump your variable with either var_dump or print_r.
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:
I have dumped it and you can see it.  There is not always a value for $wave[1]
There does not need to have a value it only needs to work if there is a value.
0
rgranlundAuthor Commented:
This lead me to the answer so far.  I have opened a new question to finish this up.
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.