dennismonsewicz Posted August 20, 2008 Share Posted August 20, 2008 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? Quote Link to comment Share on other sites More sharing options...
revraz Posted August 20, 2008 Share Posted August 20, 2008 Don't you want to echo in within your loop? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Author Share Posted August 20, 2008 didn't work updated 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; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2008 Share Posted August 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpcodec Posted August 20, 2008 Share Posted August 20, 2008 i this what you mean $sql = mysql_query("SELECT * FROM board") or die(mysql_error()); while($row = mysql_fetch_array($sql)) { $img = "images/$row[filename].jpg"; echo $img; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Author Share Posted August 20, 2008 what I am trying to accomplish is this: I have a mysql table and I am wanting my script to look through each row of the table and wherever it finds an "x" replace it with an image. Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Author Share Posted August 20, 2008 bump Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 20, 2008 Share Posted August 20, 2008 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; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Author Share Posted August 20, 2008 so there is no way to just search all rows? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted August 20, 2008 Share Posted August 20, 2008 try this $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, $sql); echo $replace; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted August 20, 2008 Author Share Posted August 20, 2008 I will definitely give it a shot, thanks! Quote Link to comment Share on other sites More sharing options...
Maq Posted August 20, 2008 Share Posted August 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.