tags:

views:

51

answers:

2

I have an table in my database which so far has been great for storing stuff in more than one object. But i want to be able to transform it into a multi-object array thingy.

Here is the data which is related to this new 'object' (in mysql):

    uid     field              value
    page:1  shop[2].location    In Shops, Dundas Arcades,Middlesbrough, TS1 1HT
    page:1  shop[1].location    5a High Street, Stockton-on-tees, TS18 1UB
    page:1  name                Enter The Asylum
    page:1  contact.website     http://entertheasylum.co.uk
    page:1  contact.phone       0800 090 090

Now what i'm looking for is to via PHP transform it into something like (print_r output):

array(
   "name" => "Enter The Asylum",
   "shop" => array(
      array("location" => "In Shops, Dundas Arcades..."),
      array("location" => "5a High Street, Stockton-on-tees...")
   ),
   "contact" => array(
      "website" => "http://entertheasylum.co.uk",
      "phone" => "0800 090 090"
   )
 )

anybody got any ideas?

Joe

+1  A: 

I can't help you with the original problem, but I'll suggest a proper way to use relational databases like MySQL. That is, you tables should have a structure like this:

Table page:

  • id PK
  • name
  • phone
  • website

Table page_location:

  • id PK (this is technically not necessary, but nicer than having a primary key on position)
  • page_id FK (references page.id)
  • position
  • location

Data would then look something like this.

Table page:

| id | name               | phone          | website                       |
| 1  | "Enter The Asylum" | "0800 090 090" | "http://entertheasylum.co.uk" |

Table page_position:

| id | page_id | position | location                                          |
| 1  | 1       | 1        | "5a High Street, Stockton-on-tees, TS18 1UB"      |
| 2  | 1       | 2        | "In Shops, Dundas Arcades,Middlesbrough, TS1 1HT" |

And some additional reading: Relational model

Lukáš Lalinský
that's no good as I want to be able to easily via the code dynamically add new parts (they'll be a lot more in the database, each with different items etc)
Joe Simpson
@jos Simpson: You would be surprised how powerful this kind of setup is. Unless you are building some sort of CMS (Where the ability of users to define new information is vast) This is probably the way to go. Especially if you expect to be able to easily translate the DB Information to a format that you can use.
Guvante
i don't want like 1000 tables just for these objects... i've figured it out with some lovely regex :)
Joe Simpson
A: 

I've managed to do this via regex and php. Here is the magic function (it's part of my code and stuff):

function fetch_profile_info($id){
    $sql = "SELECT * FROM profile_info WHERE `uid`='".me($id)."'";
    $r = mysql_query($sql);
    $profile = array();
    while($row = mysql_fetch_array($r)){
     /* new: process it into objects etc */
     $field = $row['field'];
     $field = preg_replace('/\.([a-zA-Z]*)/i', "['$1']", $field);
     $field = preg_replace('/^([a-zA-Z]*)/', "['$1']", $field);
     eval("\$profile{$field} = \"".addslashes($row['value'])."\";");
    }
    return $profile;
}

it works! yay!

Joe Simpson
"If eval() is the answer, you're almost certainly asking the wrong question." -- Rasmus Lerdorf, BDFL of PHP
Dereleased
well it's required in this code because there is no other way i could get this to work... why is eval() in there anyway?
Joe Simpson