multiple values in a field of a record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andycruickshank
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 6:12 am
Location: Aberdeen

multiple values in a field of a record

Post by andycruickshank »

I have a table with information on plants, with a field in it called state.

A plant can be in many states, so i have a value in the field 'CA LA WI' and so on, for example. Then, if i'm looking for plants in the area, i select where state like %CA% and get the plants that way, displaying the results with my php page in a table.

Is there a way to delete a value from the field, say, delete LA from the value above?

Thanks
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

You could dick around with array functions in PHP for a while, but what you really need to do is re-design your database structure so that it uses only atomic (ie, singular) values per field. That's just about the #1 rule of good database design, for reasons precisely like this one.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

you might try this

Code: Select all

<?PHP

// get the value from the database
$old = "CA LA MI OR WA ";

// enter the Abbreviation for the state to be removed 
// into a variable -- make sure you include a space
$state = "MI ";
$new = str_replace($state, "", $old);
echo $old;
echo "<br>";
echo $new;
// update the record in the database

?>
be sure to keep a space after EVERY state INCLUDING the last one.

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not use a set column type? Frees people from mistyping one, or forgetting things like "always needs a <enter delimiter here> at this spot" kinda stuff.
Post Reply