Group with PHP not in MySQL query
I have table like this
id | type
--- -----
1 | fh
开发者_JAVA技巧--- -----
2 | fhs
--- -----
3 | so
--- -----
4 | sos
while($row = mysql_fetch_array($res)){
if($row['type'] == "fh" OR $row['type'] == "fhs"){
$name = "Flush Head Studs";
}else if($row['type'] == "so" OR $row['type'] == "sos"){
$name = "Through Standoffs";
}
$category .= '<option value="'.$row['type'].'" name="'.$row['type'].'">' . $name . '</option>';
}
code above is displayng names twice. How can i do that names are only once in list.
your result is correct! if you change your code like this, you can see why ...
while($row = mysql_fetch_array($res)){
if($row['type'] == "fh" OR $row['type'] == "fhs"){
$name = "Flush Head Studs ".$row['type'];
}else if($row['type'] == "so" OR $row['type'] == "sos"){
$name = "Through Standoffs ".$row['type'];
}
$category .= '<option value="'.$row['type'].'" name="'.$row['type'].'">' . $name . '</option>';
}
if you want select all types, i suggest you to add a description or name column to your table ... because you youse all of your types as value in the select-field.
In the code that you are showing, you got 4 diferent value
atributes (coming from $row['type']) for 2 diferent (and duplicated) option
content visible to the user (coming from $name)
So you must choose for one of this ways:
1- abandon two of the value
atributes (the simplest way is remove one side of the ORs
and the OR
itself) (this is perhaps what you have requested)
2- make a visible diference on option
content using also $row['type'] (but this gives 4 options not 2)
$category .= '<option value="'.$row['type'].'" name="'.$row['type'].'">' .$row['type'].' - '. $name . '</option>';
3- give us more context to understud the problem.
精彩评论