How do I insert an array into a database? Lets say I want to have tags for posts. How should I go about this? Should I just have it insert
[syntax=text]array('tag', 'foo', 'foobar');[/syntax]
or is there some other magical thing I can do?
Also, I could probably figure this out on my own but, how do I divide tags up? If I had a textbox with
[syntax=text]foo, tag, foobar, steve, bob, fred[/syntax]
Then how do I insert that into an array?
Inserting Array into Database?
Re: Inserting Array into Database?
You use the explode() function to create an array from a comma separated list.
http://php.net/manual/en/function.explode.php
There are many different methods of getting the array in your database. You could insert is a string using implode()
http://php.net/manual/en/function.implode.php
or you could create a foreach loop and run a query for every tag? ( not very effective, performance wise, but it works )
[syntax=php]foreach($tags as $tag){
insert into `tags` $tag;
}[/syntax]
http://php.net/manual/en/function.explode.php
There are many different methods of getting the array in your database. You could insert is a string using implode()
http://php.net/manual/en/function.implode.php
or you could create a foreach loop and run a query for every tag? ( not very effective, performance wise, but it works )
[syntax=php]foreach($tags as $tag){
insert into `tags` $tag;
}[/syntax]
- FrederickGeek8
- Posts: 148
- Joined: Wed Nov 30, 2011 10:31 pm
Re: Inserting Array into Database?
The third option would not work... I would be trying for something like
[syntax=text]post_title post_id post_body post_tags[/syntax]
[syntax=text]post_title post_id post_body post_tags[/syntax]
- FrederickGeek8
- Posts: 148
- Joined: Wed Nov 30, 2011 10:31 pm
Re: Inserting Array into Database?
Ooh I could just insert the comma separated tags into the database and when search or outputting, I could just explode it
Re: Inserting Array into Database?
FrederickGeek8 wrote:Ooh I could just insert the comma separated tags into the database and when search or outputting, I could just explode it
yes.
Re: Inserting Array into Database?
There are also the serialize() and unserialize() functions
[syntax=php]$some_array = array(1, 2, 3);[/syntax]
if you do
[syntax=php]$string = serialize($some_array);[/syntax]
$string is now an encoded version of the array $some_array that you can store easily, to get back to the original array you use the opposite function
[syntax=php]$some_array = unserialize($string);[/syntax]
The explode method would work fine too but this is a bit more robust, it won't break if one of your values has a comma in it.
[syntax=php]$some_array = array(1, 2, 3);[/syntax]
if you do
[syntax=php]$string = serialize($some_array);[/syntax]
$string is now an encoded version of the array $some_array that you can store easily, to get back to the original array you use the opposite function
[syntax=php]$some_array = unserialize($string);[/syntax]
The explode method would work fine too but this is a bit more robust, it won't break if one of your values has a comma in it.
- FrederickGeek8
- Posts: 148
- Joined: Wed Nov 30, 2011 10:31 pm
Re: Inserting Array into Database?
jacek wrote:There are also the serialize() and unserialize() functions
[syntax=php]$some_array = array(1, 2, 3);[/syntax]
if you do
[syntax=php]$string = serialize($some_array);[/syntax]
$string is not an encoded version of the array $some_array that you can store easily, to get back to the original array you use the opposite function
[syntax=php]$some_array = unserialize($string);[/syntax]
The explode method would work fine too but this is a bit more robust, it won't break if one of your values has a comma in it.
Ooh this seems nice :3 thanks!