SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    eval() an array from a database

    I have two fields in a database, one for a setting_name and one for a setting value. I then want to eval() the setting_name to make it a PHP variable and set it to the corresponding value. I've got it working fine for standard text values in setting_value but as soon as I put an array in it stops working. To test it I set up the following code

    PHP Code:
    $property "emailAddressesSetting";
    $value '"news" => "news@domain.com","administrator" => "admin@domain.com"';
    eval(
    "\$$property = array(\$value);");
    foreach (
    $emailAddressesSetting as $key=>$value)
    {
         echo 
    $key." - ".$value."<br />";

    This generates an array with one value set to the text string that is in $value rather than an array with two values. I've looked on the forum and seen similar threads but none of the seem to address the issue I have or more likely I haven't understood them

    any help much appreciated
    HEXUS Webmaster

  2. #2
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems to me your error is subtle yet sneaky.

    Try instead of this
    PHP Code:
    eval("\$$property = array(\$value);"); 
    This:
    PHP Code:
    eval("\$$property = array($value);"); 

  3. #3
    SitePoint Enthusiast DaveCheney's Avatar
    Join Date
    Dec 2003
    Location
    Melbourne, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use serialize() and unserialize() to store an unknown length array in my database tables. Something like

    INSERT INTO user SET preferences = '.serialize($prefs)

    Cheers

    Dave

  4. #4
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jesus, i hate it when it turns out to be something simple Your suggestion was spot on ghurtado. Dave thanks for your reply as well, I didn't get around to using it but I've noted it for future use as I haven't used the serialize function before

    thanks again
    HEXUS Webmaster

  5. #5
    SitePoint Member
    Join Date
    Feb 2004
    Location
    London, UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes serialize is a much safer way to do this kind of thing.
    Justin

    ----------------------------------------------------------------
    http://php.jvmultimedia.com - PHP Freeware and Tutorials

  6. #6
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •