Jump to content

Print comma between query results


maweber98

Recommended Posts

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, Black

The 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.Color
FROM
  coas50.pro_color_look_up,
  coas50.pro_item,
  coas50.pro_color
WHERE
  (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 results

print ("$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!!!
Link to comment
Share on other sites

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 Color
0 Black
1 Blue
2 Red
and 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!!!!
Link to comment
Share on other sites

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.Color
FROM
  coas50.pro_color_look_up,
  coas50.pro_item,
  coas50.pro_color
WHERE
  (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!
Link to comment
Share on other sites

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 start
while ($row = mysql_fetch_assoc($query))
    $variable2[] =$row['Color'];

echo implode(', ',$variable2);
?>[/code]

Now it should work.

Ken
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.