Robert Granlund
asked on
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";
}
}
What is the reason you think it is "breaking?" What is the var_dump() output that tells us the contents of $lb?
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.
ASKER
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
?>
line 107 had no else statement. Once $wavelength is true, it is never again false.
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.
Any particular reason you are using or_where() rather than where_in()?
ASKER
@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?
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.
At the risk of taking a slight deviation ...
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
Then for your $wavelength use
I would therefore do this
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.
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;
OR you could doWHERE id in (5,7,9,21)
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();
Then for your $wavelength use
if($a && $b) $wavelength = true;
if($wavelength) {
echo "HELLO";
}
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);
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.
ASKER
@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:
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.
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
?>
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.
One thing - line 49 of your html source you have
In addition there is another error that is occurring - fix those first.
event.preventDefault();
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.
ASKER
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:
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 = '';
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
There does not need to have a value it only needs to work if there is a value.
ASKER
This lead me to the answer so far. I have opened a new question to finish this up.