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
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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.