So I have db table>”schools” and a column in this table named “metro”.
In “metro” i have strings like that “Station Name 1, Station Name 2, Station-Name 5”
I’m doing now a search form with select metro stations, so my code look like this:
<select name="categoryID">
<?
$metro_sql=mysql_query("SELECT metro FROM schools");
while($metro=mysql_fetch_array($metro_sql)){
$metro_pieces = explode(", ", $metro['metro']);
foreach (array_unique($metro_pieces, SORT_REGULAR) as $metro_all) {
echo "<option value=\"".$metro_all."\">".$metro_all."</option>\n ";
}
}
And the result:
Metro Station 1
Metro Station 2
Metro Station 3
Metro Station 1
Metro Station 4
Metro Station 5
etc.
What I’m doing wrong?
p.s. sorry for my english.
I would suggest a better database design.
You create schools in
schooland stations instation. When you need to link a school to a station, you add a record tometro. This makes the database normalized.When you then need to get the stations, you simply do a: