-1

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.

NOtherDev
  • 9,542
  • 2
  • 36
  • 47
  • 1
    The most likely problem is spaces in the field values that make them non-unique – Pekka Nov 20 '11 at 10:49
  • Unrelated to your question, but if your data were more normalized, you would not have to process it like this. Store comma separated data in a relational database is almost always a bad idea. – Corbin Nov 20 '11 at 10:49

2 Answers2

2

There is a logical problem.
Strictly speaking you cannot say that "array_unique doesn't work".
Just because there are a dozen other functions involved.
And you have no idea if this function is provided with correct data.

You have to learn to debug your code.
Add debugging output to your code and verify every variable's state to see if it contains desired data.

start from var_dump($metro); inside of the loop

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thanks for advice, it returns bool(false) – user1056252 Nov 20 '11 at 10:50
  • **inside** of the loop, not outside! – Your Common Sense Nov 20 '11 at 10:51
  • oops, inside of the loop nothing returns – user1056252 Nov 20 '11 at 10:55
  • that's impossible. you have some output from this variable – Your Common Sense Nov 20 '11 at 11:02
  • array(2) { [0]=> string(76) "S 1, S 2, S 3" ["metro"]=> string(76) "S 1, S 2, S 3" } array(2) { [0]=> string(76) "S 1, S 2, S 3" ["metro"]=> string(76) "S 1, S 2, S 3" } array(2) { [0]=> string(76) "S 1, S 2, S 3" ["metro"]=> string(76) "S 1, S 2, S 3" } array(2) { [0]=> string(68) "S 4, S 3" ["metro"]=> string(68) "S 4, S 3" } array(2) { [0]=> string(68) "S 4, S 3" ["metro"]=> string(68) "S 4, S 3" } array(2) { [0]=> string(64) "S 5, S 6" ["metro"]=> string(64) "S 5, S 6" } array(2) { [0]=> string(64) "S 5, S 6" ["metro"]=> string(64) "S 5, S 6" } – user1056252 Nov 20 '11 at 11:14
  • well, now get a string that wrongs your array_unique and incestigate what's wrong with it. But better follow Gustav's advise already. – Your Common Sense Nov 20 '11 at 11:15
1

I would suggest a better database design.

CREATE TABLE school (
  school_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255)
);

CREATE TABLE station (
  station_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255)
);

CREATE TABLE metro (
  school_id INT NOT NULL,
  station_id INT NOT NULL,
  UNIQUE INDEX (school_id, station_id)
);

You create schools in school and stations in station. When you need to link a school to a station, you add a record to metro. This makes the database normalized.

When you then need to get the stations, you simply do a:

SELECT station.* 
FROM metro
INNER JOIN station
ON metro.station_id = station.station_id
WHERE metro.school_id = $school_id
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
  • Yes, it's much more better, than I have, but, unfortunately, i've already wrote a code for view, edit and update forms for schools information. It will take a lot of time for me to rewrite all of this. – user1056252 Nov 20 '11 at 11:19
  • It will take more time when you have to fix it later. It is easier to fix a problem when you design, harder to fix it when you code, much harder when you have deployed, and really hard when you have to come into the code fresh. It is always better to fix the error as soon as possible - the longer you leave it, the more expensive it is in time and money. – Gustav Bertram Nov 20 '11 at 17:10