0

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

1
  • Why do you make a while loop, put all rows inside it, and then loop through all rows again? Why don't you just take the while loop straight away? Commented Mar 27, 2015 at 8:22

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

(If u have some time to improve u can delete the outer foreach loop :).
1

Seems you need to use explode function

$exclude_words = explode(',', $row['rule_exclude_values']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.