Jump to content

Archived

This topic is now archived and is closed to further replies.

maweber98

Print comma between query results

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!!!

Share this post


Link to post
Share on other sites
You could just remove the last comma

while ($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);

Share this post


Link to post
Share on other sites
It's much easier to use the implode() function:
[code]<?php
while ($row = mysql_fetch_assoc($query))
    $variable2=$row['Color'];

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

Ken

Share this post


Link to post
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!!!!

Share this post


Link to post
Share on other sites
Can you post the code that didn't work?

Ken

Share this post


Link to post
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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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!!!!

Share this post


Link to post
Share on other sites

×

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.