dth54864 2015-03-27 08:13 采纳率: 0%
浏览 42

PHP - 遍历表中的所有记录

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

  • 写回答

2条回答 默认 最新

  • drhs13583567608 2015-03-27 08:27
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?