Forum Moderators: coopster

Message Too Old, No Replies

What's the best way to store an array in the database

         

NooK

8:20 am on Oct 11, 2007 (gmt 0)

10+ Year Member



I am trying to build a simple tree menu where the main items can be either a link or open up a subList of items which themselves can belinks or open into more submenus and so on.

An example is

$ar = array(
'new_user' => 'newuser.php',
'tst1' => array(
'subtst' => 'subtstlink.php',
'subtst2' => 'sublink2.php'
),
'heyhey' => 'heyhey.php',
);

I am wondering what is the best way to store that in a mysql database (Both the item name and the link to it). Is there any php function that will allow me to make such an array into some sort of variable (String?) which can then be read easilly and made back into the array for manipulation and creation of the tree menu?

Best Regards

NooK

joelgreen

8:43 am on Oct 11, 2007 (gmt 0)

10+ Year Member



Maybe serialize [php.net] would help

hughie

11:45 am on Oct 11, 2007 (gmt 0)

10+ Year Member



serialize works but you need to be make sure you escape any nasty characters before sending it to the DB

This works for me

$array=array("dog","cat","frog's");

$arrayString=serialize($array)
$arrayString=htmlentities($arrayString,ENT_QUOTES);

mysql_query...// insert your string into the db.

Then when fetching the string and converting it back to an array.

$result=mysql....
$row=mysql_fetch_array($result);
$arrayString=html_entity_decode($row['FieldName'],ENT_QUOTES);
$array=unserialize($arrayString);

and that should be that.

NooK

9:01 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Thanks guys. Exactly what I was looking for.

Best Regards

NooK