Adding a string into an array

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
leyen
Forum Newbie
Posts: 5
Joined: Sat Mar 25, 2006 4:45 am

Adding a string into an array

Post by leyen »

I know, I tend to glance over the basics, but I just can't seem to solve this problem.

The following db/table/record names have been modified for security reasons.

My database has a table called 'necklace'. One of the fields is color_id in which you can list an infinite number of 'color_id's

So for example:

Necklace Table

Code: Select all

Necklace_id	| Name	| Color_id
	5	| Oxyde	| 2,5,34,83,23
Now I have a php script that extracts this information from a database and adds an extra color_id depending on what color the user wants to add.

Code: Select all

// Get the necklace details
	$sql = "SELECT * FROM necklace WHERE necklace_id='5';
	$result = mysql_query ($sql);
		
	$color_array = explode(",", $result['color_id']);

// At this point, $color_array is an array, but I want to add the string $new_color into $color_array. Can't for the life of me figure out how to do this

	$color_id = implode(",", $color_array);

	// Update necklace record with new color ID.
	$sql2 = "UPDATE necklace SET color_id='$color_id' WHERE necklace_id='5'";
	$result2 = mysql_query ($sql2);
Can anyone help? Thanks!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

$color_array[] = $AddMe;
// add more than one using
array_push($color_array, $AddMe, $AddMe2);
Post Reply