maweber98 Posted March 2, 2006 Share Posted March 2, 2006 I'm having trouble with the code below. What I'm trying to do is query a database to get a list of colors that a particular item comes in. The problem I'm having is I'm a little confused on how to output the list of colors with a comma in between each one. Example"Red, Blue, BlackThe way I have it now in the code below is:print ("$variable2, ");which of course prints an extra comma at the end of the list of colors.[code]<? //Color Query//error message (not found message)begins $XX = "Colors Not Found\n"; //query details table begins$query = mysql_query("SELECT coas50.pro_color_look_up.Color_ID, coas50.pro_color_look_up.Item_ID, coas50.pro_item.Description, coas50.pro_item.Item_Number, coas50.pro_color.ColorFROM coas50.pro_color_look_up, coas50.pro_item, coas50.pro_colorWHERE (coas50.pro_item.Item_Number = '$Item_Number') AND (coas50.pro_item.ID = coas50.pro_color_look_up.Item_ID) AND (coas50.pro_color_look_up.Color_ID = coas50.pro_color.ID) ORDER BY Color"); if ($query == FALSE){ die ('Query not working! ' . mysql_error());} while ($row = @mysql_fetch_array($query)) { $variable2=$row['Color']; //table layout for results //prints extra comma at the end of resultsprint ("$variable2, ");}//below this is the function for no record!!if (!$variable2){ print ("$XX");} //end ?>[/code]Does anyone have an idea of how I can get this to work. It seems like it should be an easy solutions. Any help would be greatly appreciated. Thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/ Share on other sites More sharing options...
JonnoTheDev Posted March 2, 2006 Share Posted March 2, 2006 You could just remove the last commawhile ($row = @mysql_fetch_array($query)) { $variable2=$row['Color']; //table layout for results //prints extra comma at the end of results$str .= "$variable2, ";}print substr($str, strlen($str)-1); Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13603 Share on other sites More sharing options...
kenrbnsn Posted March 2, 2006 Share Posted March 2, 2006 It's much easier to use the implode() function:[code]<?phpwhile ($row = mysql_fetch_assoc($query)) $variable2=$row['Color'];echo implode(', ',$variable2);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13604 Share on other sites More sharing options...
maweber98 Posted March 2, 2006 Author Share Posted March 2, 2006 I tried both of these methods and couldn't get either to work. I figured the implode method would have worked, but I get a "Bad Argument to implode" error. Does this error give any clues as to why it doesn't work the way it should. The color table is as follows:ID Color0 Black1 Blue2 Redand so on.I don't think there is anything wrong in the way the table is set up.Does anyone have any idea way the implode function is not working? Thanks so much!!!! Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13618 Share on other sites More sharing options...
kenrbnsn Posted March 2, 2006 Share Posted March 2, 2006 Can you post the code that didn't work?Ken Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13624 Share on other sites More sharing options...
maweber98 Posted March 2, 2006 Author Share Posted March 2, 2006 This is the code I'm trying to get to work. I just can't seem to figure out why the implode function is not working.[code]<? //Color Query//error message (not found message)begins $XX = "Colors Not Found\n"; //query details table begins$query = mysql_query("SELECT coas50.pro_color_look_up.Color_ID, coas50.pro_color_look_up.Item_ID, coas50.pro_item.Description, coas50.pro_item.Item_Number, coas50.pro_color.ColorFROM coas50.pro_color_look_up, coas50.pro_item, coas50.pro_colorWHERE (coas50.pro_item.Item_Number = '$Item_Number') AND (coas50.pro_item.ID = coas50.pro_color_look_up.Item_ID) AND (coas50.pro_color_look_up.Color_ID = coas50.pro_color.ID) ORDER BY Color"); if ($query == FALSE){ die ('Query not working! ' . mysql_error());} while ($row = mysql_fetch_assoc($query)) $variable2=$row['Color'];echo implode(', ',$variable2);//below this is the function for no record!!if (!$variable2){ print ("$XX");} //end ?>[/code]This is the error message that I get: "Warning: Bad arguments to implode() in c:\apache\htdocs\products\product_output.php on line 239"Does anyone have any insight on this. Thanks so much!! I appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13627 Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 I must have been asleep when I posted the answer... the input to implode needs to be an array...Try these fixed lines:[code]<?php$variable2 = array(); //just to make sure we're using an empty array to startwhile ($row = mysql_fetch_assoc($query)) $variable2[] =$row['Color'];echo implode(', ',$variable2);?>[/code]Now it should work.Ken Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13828 Share on other sites More sharing options...
maweber98 Posted March 3, 2006 Author Share Posted March 3, 2006 It works perfectly now!! Thank you so much for all your help. I'm still a little new to PHP so any help is always greatly appreciated. This forum has been very helpful. Thanks again!!!! Quote Link to comment https://forums.phpfreaks.com/topic/3922-print-comma-between-query-results/#findComment-13897 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.