上午,
我有一个名为wp_cam_rules
的表格,其中有两个字段(我感兴趣的两个字段),它们是rule_exclude_values
和rule_include_values
。 wp_cam_rules
中有大约100个条目。 rule_exclude_values
和rule_include_values
包含以逗号分隔的列表。
我想要做的是,使用php循环遍历每一行并检查一些标准。
基本上该函数将采用字符串($ x),检查此字符串是否包含任何排除字(rule_exclude_values
)。如果没有,则检查它是否包含任何包含的单词(rule_include_values
)并相应地打印一些文本。
我现在遇到的问题是代码似乎在第一次进入后停止而没有循环,请任何人帮忙吗?
以下是代码:
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;
}
}
由于 克里斯
答案 0 :(得分:1)
您的代码中的break
太早了
此外,您无需使用此标记$skip_word
...
尝试使用此代码
<?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;
}
答案 1 :(得分:1)
似乎您需要使用explode
功能
$exclude_words = explode(',', $row['rule_exclude_values']);