...

View Full Version : Looping problem



jeddi
02-01-2012, 02:50 PM
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:


$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


.

abduraooft
02-01-2012, 03:31 PM
You could make an array of such words and loop through the set of messages as strpos() accepts a mixed needle.

jeddi
02-02-2012, 08:06 AM
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 :eek:

Thanks


.

jmj001
02-02-2012, 02:17 PM
use the db query to get the bad words and build an array of them... then use abduraooft's suggestion

jeddi
02-02-2012, 03:05 PM
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 ?


$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

abduraooft
02-03-2012, 05:33 AM
From where you get ${'Db_mes_'.$i}?

jmj001
02-03-2012, 07:57 AM
maybe you could define 'not quite working'

jeddi
02-03-2012, 08:07 AM
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.


.

abduraooft
02-03-2012, 09:42 AM
Do you mean that this is not valid code ?

Where's your code to assign values to that variable?

jeddi
02-03-2012, 11:31 AM
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.


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum