pocobueno1388 Posted June 12, 2007 Share Posted June 12, 2007 Hello =] I am creating a function for my horse site that will choose the color of the horse. I have a database full of horse breeds and all the possible colors they can be. Here is what the "color" column might hold for one of the horse breeds: solid,paint The word "solid" can be a bunch of different colors...so I am trying to make my script add all these colors to an array I am populating that holds all the possible colors for that breed. Here is my current code for the function with comments to let you know what is going on <?php function color($breed){ $getColors = mysql_query("SELECT colors FROM i_horses WHERE breed='$breed'"); $color = mysql_fetch_assoc($getColors); //put colors into array $colors = explode(',',$color['colors']); //<----This now holds an array with the values "solid" and "paint". foreach ($colors as $key => $val){ //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array. if ($val == 'solid'){ $colors[] = "Bay, Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut, Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel"; continue; } echo $key.' - '.$val.'<br>'; } } ?> What happens is it ends up just deleting the word "solid" from the array, and doesn't add the colors. What I would like it to do is replace the word "solid" with all the other colors in the code....I can't seem to get it to do this though. Any help is greatly appreciated xD Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 12, 2007 Share Posted June 12, 2007 You will always get the wrong result in that code because you are echoing $val to the page after you have already set it as "solid". Ok, not sure if you want to replace "solid" with that long string or if you want to create array elements for each color in that string. So, I'll give you both solutions: Replace "solid" with one long string: foreach ($colors as $key => $val){ //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array. if ($val == 'solid'){ $colors[$key] = "Bay, Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut, Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel"; } echo "$key - $colors[$key]<br>"; } Replace solid with the colors as array elements: foreach ($colors as $key => $val){ //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array. if ($val == 'solid'){ //Add first color $colors[$key] = "Bay"; $colorList = "Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut, Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel"; //Merge the other colors on the end of the array $colors = array_merge($colors, explode(',', $colorList)); } } foreach ($colors as $key => $val){ echo "$key - $colors[$key]<br>"; } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 12, 2007 Author Share Posted June 12, 2007 Thank you so much mjdamato, worked perfect =D 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.