This is my data
id cost cat foreign colours
--------------------------------------------------------
385_white 99 swarovski 12 black;blue
386_white 99 swarovski 12 black;blue;green
387_white 99 swarovski 12 yellow;green
389_white 99 swarovski 12 white;silver
385_white 99 swarovski 12 silver
This is my query
$checkSQL=mysql_query("SELECT * FROM `products`");
while($r = mysql_fetch_assoc($checkSQL)) {
echo '<ul class="ColourList">';
$cols = explode( ';', $r['colours'] );
foreach ( $cols as $col ){
echo '<li class="' . $col . '">' . $col . '</li>';
}
echo '</ul>';
}
This basically returns a list of colors for each record
<ul>
<li>black</li>
<li>blue</li>
</ul>
However I want to return a single list, for every color i the entire database and alphabetize it. So basically get all the colors, in all rows, split them in a list in ABC order but without them repeating – is this possible with my clients setup? Just need some direction? Thinking explode with replace and then us the ORDER BY ASC command?
This is a nice example of what happens if you not normalize your database. At first it seems easy, but ones runs into trouble later.
Change your tables like this:
Products:
ProductColors:
Colors:
If you have a structure like this, it is very easy ro query the database for what you want.
Example:
To query all available colors:
To query all available colors for
385_white: