Jump to content


Photo

Print comma between query results


  • Please log in to reply
7 replies to this topic

#1 maweber98

maweber98
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 March 2006 - 04:56 PM

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.

<? 
//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 
?>

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

#2 JonnoTheDev

JonnoTheDev
  • Moderators
  • Advanced Member
  • 3,587 posts
  • LocationLiverpool, UK

Posted 02 March 2006 - 05:13 PM

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);

To start, press any key. Where's the 'Any' key?


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 March 2006 - 05:16 PM

It's much easier to use the implode() function:
<?php
while ($row = mysql_fetch_assoc($query))
    $variable2=$row['Color'];

echo implode(', ',$variable2);
?>

Ken

#4 maweber98

maweber98
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 March 2006 - 06:24 PM

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

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 March 2006 - 06:46 PM

Can you post the code that didn't work?

Ken

#6 maweber98

maweber98
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 March 2006 - 06:54 PM

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.

<? 
//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 
?>

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!

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 05:18 AM

I must have been asleep when I posted the answer... the input to implode needs to be an array...

Try these fixed lines:
<?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);
?>

Now it should work.

Ken


#8 maweber98

maweber98
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 03 March 2006 - 01:54 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users