Jump to content

Help with formatting data


mclamais

Recommended Posts

I have data in a table like

 

 

prod_id     Color
1      Blue
1      Red
2      Tan
3      Red
3      Black

 

And I need it to look like this

 

prod_id     Color
1      Blue, Red
2      Tan
3      Red, Black

 

So how can I format the data so that I have one prod_id, with the one or more associated color in a comma separate list in a single field.

 

Does anyone know how I can create a PHP page to query the database, and dump the data to a format like the one above.

 

Or if this can be done directly in MySQL that would be great.

 

 

Link to comment
Share on other sites

It doesn't matter how you put something into the Database, a text area a text input or whatever...

 

You can format the result though, if that's what you're asking - I'll show you.

 

Go set prod_id #1 to "Red, Blue, Green" for color, then do this...

 

<?php
// Connect to Database here...

$query = "SELECT * from `table` WHERE `prod_id` = '1'";
$result = mysql_query( $query );
$fetch = mysql_fetch_array( $result );

$id = $fetch['prod_id'];
$color = explode( ',', $fetch['color'] );

echo( $id . ' is available in the following colors: ');
echo( '<ul>' );
foreach( $color as $a => $b )
{
   echo( '<li><span style="color:' . strtolower( $b ) . ';">' . $b . '</span></li>' );
}
echo( '<ul>' );
?>

 

I'm just showing you that you can play with the commar-seperated list in the Database and format it in different ways, once you've retrieved it.

 

As for how you get it into the Database, it could be a lot of different ways and it wouldn't really matter... unless I misunderstood your question?

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.