Jump to content

[SOLVED] Results from db into an array


papaface

Recommended Posts

Hello,

How can I put the results from the db:

$selectbadwords = mysql_query("select bad_word,bad_replace from filter where `type`='bad'");

 

in an array like this:

$bad_words = array("badword1fromdb","badword2fromdb","badword3fromdb","badword4fromdb")

 

My memory seems to have gone these last few days lol

Link to comment
https://forums.phpfreaks.com/topic/39948-solved-results-from-db-into-an-array/
Share on other sites

Thats what I thought it was. Im trying to put it into this bad word filter. But it doesnt quite work the way i want.

 

This is my code

$str = "hello i wish you would crap off!";

$selectbadwords = mysql_query("select filter_word,filter_replace from filter where `type`='bad'");

   while ($row = mysql_fetch_assoc($selectbadwords)) 
  	{
       $badword[] = $row["filter_word"];
   $replacement[] = $row["filter_replace"];
}
foreach ($badword AS $badwords)
{
foreach ($replacement AS $replacements)
	{
		$str = eregi_replace($badwords, $replacements, $str);
	}
 }
echo $str;

 

For the sake of keeping this forum clean the words I have in my db are:

filter_word: hello

filter_replace: [was hello]

 

filter_word: crap

filter_replace: [was crap]

 

but it is outputting:

 

[was crap] i wish you would [was [was [was crap]]] off!

 

 

Any suggestions?

Okay I changed the DB entries abit to:

 

$str = "hello i wish you would crap off!";

 

filter_word: hello

filter_replace: [removed]

 

filter_word: crap

filter_replace: [snipped]

 

but I get this outputted:

 

[snipped] i wish you would [snipped] off!

Try this:

<?php
while ($row = mysql_fetch_assoc($selectbadwords)){
      $badwords[] = $row["filter_word"];
      $replacements[] = $row["filter_replace"];
}
foreach ($badwords AS $k=>$badword){
      $str = str_replace($badword, $replacements[$k], $str);
}
echo $str;
?>

 

I also changed the variable names to make more sense - an array is plural so it should be $badwords, not the other way round :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.