Jump to content

replacing a character


dennismonsewicz

Recommended Posts

I have this code:

 

$sql = mysql_query("SELECT * FROM board") or die(mysql_error());
			while($row = mysql_fetch_array($sql)) {
				$arr[] = $row;
				$img = "images/x.jpg";
				$replace = str_replace("x", $img, $arr);
				}

			echo $replace;

 

I am trying to search for X within the array and replace it with an image. But all I am getting echoed out is the word Array. Any help?

Link to comment
Share on other sites

You code makes no sense.

 

You iterrate through each record in the result set (as an array) and add it to another array - creating a mutidimensional array. Then you attempt to search for "x" in the multidimentional array $arr and replace it with the variable $img (which always contains "images/x.jpg").

 

Also in the loop you are always redefining the value of $replace, so when you echo the value you will only get the value from the last iterration through the loop.

 

To be honest I have no clue what you are trying to accomplish. Explain what results you are getting from your query and how you want to display those results.

Link to comment
Share on other sites

You will need to specify the field from your database you want to search. something like this

 

$img = "images/x.jpg";
$sql = mysql_query("SELECT * FROM board") or die(mysql_error());
			while($row = mysql_fetch_array($sql)) 
{
				$x = $row['name of the field from the database'];

				$replace = str_replace("x", $img, $x);
				echo $replace;
				}

Link to comment
Share on other sites

If adam291086's doesn't work you could try this if your DB is small and there too that many rows.  Basically it's searching each row manually...

 

$img = "images/x.jpg";
$sql = mysql_query("SELECT * FROM board") or die(mysql_error());
			while($row = mysql_fetch_array($sql)) 
{

                                        $replace = str_replace("x", $img, $row['row1']);
                                        $replace = str_replace("x", $img, $row['row2']);
                                        $replace = str_replace("x", $img, $row['row3']);
				$replace = str_replace("x", $img, $row['row4']);

				print_r($replace);
}

 

This generally isn't good practice but if you're only using this script once with a small DB I don't see a problem with it.  Hope this helps.

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.