Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Looping problem

  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Looping problem

    Hi,

    I have about 50 messages which I want to check for
    the presence of swear words ( I know I'm a bit old fashioned, but I don't want them accepted in my system )


    So I put the swear words in a table and I loop through them
    like this:

    PHP Code:
    $sql "SELECT * FROM swords"
    $result mysql_query($sql) or die("could not execute GET BAD WORDS"mysql_error());    
    while (
    $row mysql_fetch_assoc($result)) {
        if (
    stripos($Db_mes_1,$row['badword'])){
             
    $err_msg "Please check your spelling as bad language was detected in the message no 1.";
              require_once (
    "write_camp_fm.php");
              exit();
         } 
    // end if
    }  // end while 
    Now this is fine for one message, but how do I check
    all 50 messages ?

    The messages are in variables like this:

    $Db_mes_1
    $Db_mes_2
    $Db_mes_3
    $Db_mes_4

    Any help much appreciated.

    Thanks


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    You could make an array of such words and loop through the set of messages as strpos() accepts a mixed needle.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,
    Thanks for reply.

    You are no doubt correct.

    But because I have used the bad word test table in several other places,
    I would rather use that bit of code and somehow add a loop it so that I can apply it to all 50 variables rather than just the one it is doing now.

    If not I will just have to write that test check 50 times

    Thanks


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    use the db query to get the bad words and build an array of them... then use abduraooft's suggestion

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    use the db query to get the bad words and build an array of them

    OK,

    I have tried something but it is not quite working.

    Can you see what I have done wrong ?
    Code:
    $baddies = array(  );
    
    $sql = "SELECT * FROM swords"; 
    $result = mysql_query($sql) or die("could not execute GET BAD WORDS". mysql_error());    
    while ($row = mysql_fetch_assoc($result)) {
      $new_bad = $row['badword']
      $baddies [] = $new_bad;
      }  // end while  
    
    $cnt= count($baddies);
    
    for ($i=1; $i<=50; i++) {
     for ($j=0; $j<=$cnt; j++) {
     if (stripos( ${'Db_mes_'.$i}, $baddies['$j'])){
             $err_msg = "Please check your spelling as bad language was detected in the message no 1.";
              require_once ("write_camp_fm.php");
              exit();
        } // end if    
      } // end for loop j   
     }  // end for loop i
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    From where you get ${'Db_mes_'.$i}?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    maybe you could define 'not quite working'

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Wad ya mean ...

    From where you get ${'Db_mes_'.$i}?
    Do you mean that this is not valid code ?

    I am trying to check my variables which are like this:

    $Db_mes_1
    $Db_mes_2
    $Db_mes_3
    $Db_mes_4


    Is my looping logic OK ?

    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #9
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Do you mean that this is not valid code ?
    Where's your code to assign values to that variable?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #10
    Senior Coder
    Join Date
    May 2006
    Posts
    1,680
    Thanks
    28
    Thanked 4 Times in 4 Posts
    They are already assigned.

    They come in from a form so I get them from the POST array
    like this:

    $N_mes_1 = $_POST['x_mes_1'];
    $N_mes_2 = $_POST['x_mes_2'];
    $N_mes_3 = $_POST['x_mes_3'];
    $N_mes_4 = $_POST['x_mes_4'];
    ...
    $N_mes_50 = $_POST['x_mes_50'];

    then:

    $Db_mes_1 = safe_sql($N_mes_1);
    $Db_mes_2 = safe_sql($N_mes_2);
    $Db_mes_3 = safe_sql($N_mes_3);
    $Db_mes_4 = safe_sql($N_mes_4);
    ...
    $Db_mes_50 = safe_sql($N_mes_50);

    So that after getting them I want to test them
    all for bad language and if any messages contain
    stuff I don't like, then I send the user back to the
    input form to make the change.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •