Reputation: 443
Morning,
I have a table called wp_cam_rules
which has two fields in it (two that i'm interested in) they are rule_exclude_values
and rule_include_values
. There are approx 100 entries in wp_cam_rules
. rule_exclude_values
and rule_include_values
contain a comma separated list.
What I want to do is, using php, is loop through each of the rows and check some criteria.
Basically the function will take a string ($x), check if this string contains any of the exclude words (rule_exclude_values
). If it doesn't then check if it contains any of the include words (rule_include_values
) and print some text accordingly.
The problem I'm having at the minute is that code seems to be stopping after the first entry and not looping, please can anyone help?
Here's the code:
function get_attributes($x) {
$sql = "SELECT * FROM wp_wcam_rules";
$result = mysql_query($sql) or die(mysql_error());
$attribute_results= array();
while($row = mysql_fetch_assoc($result)) {
$attribute_results[] = $row;
}
foreach ($attribute_results as $row) {
$exclude_words = $row['rule_exclude_values'];
$include_words = $row['rule_include_values'];
foreach ($exclude_words as $exclude_value) {
if ( stripos($x,$exclude_value)===false) {
$skip_word = false;
} else {
$y.= 'Excluded: '.$exclude_value;
$skip_word = true;
break;
}
}
if ($skip_word ===false) {
foreach ($include_words as $include_value) {
if (stripos($x,$include_value) !== false) {
$y .= 'Attribute Found: '.$include_value;
continue;
}
}
}
return $y;
}
}
Thanks Chris
Upvotes: 0
Views: 1324
Reputation: 1520
Seems you need to use explode
function
$exclude_words = explode(',', $row['rule_exclude_values']);
Upvotes: 1
Reputation: 7805
You have a too early break
in your code
Also, you do not need to use this flag $skip_word
...
Give a try with this code
<?php
function get_attributes($x) {
$sql = "SELECT * FROM wp_wcam_rules";
$result = mysql_query($sql) or die(mysql_error());
$attribute_results= array();
while($row = mysql_fetch_assoc($result)) {
$attribute_results[] = $row;
}
foreach ($attribute_results as $row) {
$exclude_words = $row['rule_exclude_values'];
$include_words = $row['rule_include_values'];
$y = '';
foreach ($exclude_words as $exclude_value) {
$y.= 'Excluded: '.$exclude_value;
foreach ($include_words as $include_value) {
if (stripos($x,$include_value) !== false) {
$y .= 'Attribute Found: '.$include_value;
}
}
}
}
return $y;
}
Upvotes: 1