Morning,
我有一个名为 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;
}
}
谢谢克里斯
你的
代码中有一个太早的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;
}
似乎你需要使用explode
函数
$exclude_words = explode(',', $row['rule_exclude_values']);