2012-10-10 32 views
-1

我試圖把在PHP中一個巨大的配置數組,看起來像這樣談到multidementional數組到數據庫

$config['festival'] = 
array 
(
    'title' => 'USF Tango Festival', 
    'tableLayout' => array 
    (
     'registration' => array 
     (
      array('firstName','text'), 
      array('lastName','text'), 
      array('phone','text'), 
      array('email','text'), 
      array('hearAboutFestival','text'), 
      array('danceAs','enum(\'Leader\', \'Follower\', \'Both\')'), 
      array('student','tinyint(1)') 
     ), 
     'experience' => array 
     (
      array('options','text'), 
      array('lunchMeat','enum(\'Ham\',\'Turkey\',\'Vegetarian\')'), 
      array('lunchBread','enum(\'White\',\'Wheat\')'), 
      array('dinnerPref','enum(\'Chicken\',\'Beef\',\'Vegetarian\')') 
     ) 
    ), 
    'pageLayout' => array 
    (
     'registration' => array 
     (
      'jqueryRules' => 
<<<EOT 
    'firstName': 'required', 
    'lastName': 'required', 
    'phone': { 
     required: true, 
     phoneUS: true 
    }, 
    'email': { 
     required: true, 
     email: true 
    }, 
    'danceAs': 'required', 
    'partner': 'required', 
    'partnerMatching': { 
     required: function() { 
      return $("input[name='partner']").val() == 0; 
     } 
    }, 
    'partnerName': { 
     required: function() { 
      return $("input[name='partner']").val() == 1; 
     } 
    } 
EOT 
    , 
     'inputs' => array 
     (
      array 
      (
       'type' => 'text', 
       'name' => 'firstName', 
       'fullName' => 'First Name', 
       'required' => true, 
       'separateDiv' => false 
      ), 
      array 
      (
       'type' => 'text', 
       'name' => 'lastName', 
       'fullName' => 'Last Name', 
       'required' => true, 
       'separateDiv' => false 
      ), 
      array 
      (
       'type' => 'text', 
       'name' => 'phone', 
       'fullName' => 'Phone number', 
       'required' => true, 
       'separateDiv' => false 
      ), 
      array 
      (
       'type' => 'text', 
       'name' => 'email', 
       'fullName' => 'Email address', 
       'required' => true, 
       'separateDiv' => false 
      ), 
      array 
      (
       'type' => 'text',    
       'name' => 'hearAboutFestival', 
       'separateDiv' => false, 
       'fullName' => 'How did you hear about the festival?', 
       'required' => false 
      ), 
      array 
      (
       'type' => 'select', 
       'name' => 'danceAs', 
       'fullName' => 'You dance as a...', 
       'required' => true, 
       'separateDiv' => false, 
       'options' => array(array('leader','Leader'),array('follower','Follower'),array('both','Both')) 
      ), 
      array 
      (
       'type' => 'checkbox', 
       'name' => 'student', 
       'value' => '1', 
       'separateDiv' => false, 
       'fullName' => 'I am a student', 
       'required' => false 
      ) 
     ) 
    ) 

'options' => array 
(
    'busMilonga' => array 
    (
     'price' => 20, 
     'student' => false, 
     'name' => 'Tango on the Town Bus milonga', 
     'description' => 'A bus milonga!' 
    ), 
    'thursdayMilonga' => array 
    (
     'price' => 10, 
     'student' => false, 
     'name' => 'Thursday Kickoff Milonga', 
     'description' => 'The un-official kick off milonga!' 
    ), 
    'saturdayPass' => array 
    (
     'price' => 90, 
     'student' => true, 
     'name' => 'Saturday pass', 
     'description' => 'Includes all Saturday workshops and milongas' 
    ), 
    'sundayPass' => array 
    (
     'price' => 80, 
     'student' => true, 
     'name' => 'Sunday pass', 
     'description' => 'Includes all Sunday workshops, the jam session, and milonga' 
    ), 
    'milongaPass' => array 
    (
     'price' => 70, 
     'earlyBird' => 50, //array(50,60), 
     'student' => true, 
     'name' => 'Milonga Pass', 
     'description' => 'Includes entrance to all night milongas' 
    ), 
    'dinnerPass' => array 
    (
     'price' => 20, 
     'student' => false, 
     'name' => 'Dinner pass', 
     'description' => 'Includes Saturday dinner' 
    ), 
    'lunchPass' => array 
    (
     'price' => 10, 
     'student' => false, 
     'name' => 'Saturday lunch', 
     'description' => 'Includes lunch on Saturday' 
    ) 
), 
'info' => array 
(
    'instructors' => array('Instructors'), 
    'hour' => array('10','11','12','01','02','03'), 
    'min' => array('15','30','45','00'), 
    'tod' => array('AM','PM'), 
    'day' => array('Friday','Saturday','Sunday'), 
    'level' => array('Beginner','Intermediate','Advanced'), 
    'place' => array('REC 107','REC 033','REC 005'), 
    'sessions' => array 
    (
     3, // days 
     array 
     (
      'Friday', // day name 
      3, // sessions on this day 
      array 
      (
       3, // workshops in session 
       '1100AM' // time 
      ), 
      array 
      (
       3, 
       '0100PM' 
      ), 
      array 
      (
       1, 
       '0230PM' 
      ) // 1,4 
     ), 
     array('Saturday', // 2,0 
      3, // 2,1 
      array(1,'1030AM'), //2,2 
      array(3,'1145AM'), // 2,3 
      array(3,'0145PM') // 2,4 
     ), 
     array 
     (
      'Sunday', // 3,0 
      3, // 3,1 
      array(1,'1115AM'), // 3,2 
      array(3,'1230PM'), // 3,3 
      array(3,'0230PM') // 3,4 
     ) 
    ) 
) 

到數據庫中。我想我可以製作幾張表,標題爲config.event.festival,但之後會變得很麻煩,因爲我必須爲陣列下的每個陣列創建表...

我想避免使用JSON編碼或序列化,以便我保持數據的所有關係和乾淨的外觀,但除了保留一個大的配置文件而不是數據庫之外,我不知道其他方式。有任何想法嗎?

+0

因此,您希望觀衆設計您的數據庫結構併爲您編寫代碼? PHP和MYSQL是不同的。 MYSQL不知道PHP數組是什麼。所以沒有單一的方法來實現這一點。 –

+0

我不是要求代碼,我要求一個設計理念,將我的配置文件放入數據庫。我理解編程語言(PHP)和數據庫(MySQL)之間的區別,我知道你不能只將數組放在MySQL中。 – josh

回答

2

假設您提供的示例代表了整個數據結構的截斷視圖,可以使用三個表完成此操作。

節日

  • festival_id
  • 標題

佈局

  • layout_id
  • festival_id
  • layout_type_id
  • 類型
  • 名稱
  • 全名
  • 需要
  • separateDiv

layout_type
  • layout_type_id
  • 名稱

節日表將保留關於活動頁面的高級元數據。佈局表格將包含關於頁面上每個元素的元數據。最後,layout_type表將允許您識別應該在給定頁面上的不同元素類型。

這應該讓您開始並允許您根據需要進行修改。

+0

你能舉個例子說明這個看起來怎麼樣?我認爲這可能是一個很好的解決方案,但我不知道如何開始。我將如何使用layout_type?像:layout_type_id = 1,name = input? – josh

+0

我將更新示例以顯示我的配置文件的截斷視圖。 – josh

+0

你的擴展配置塊很明顯,這不是你的問題的完整答案(我可以看到需要2-3個表格來處理表格,輸入選項和其他可能的元素)。此外,由於我不瞭解您的全部要求,我不願意提供比我已經提供的解決方案更多的解決方案。但是,您應該能夠將問題中的示例配置代碼片段應用到上面的表格中。這應該給你足夠的想法,你可以從這裏拿走它。 – Thomas

1

試試這個:

// To save multidimensional array into database: 

$confIn = serialize($config); 

// Save serialized config into database 

// To get it from database, query the database and get serialized value 

$confOut = serialize($confIn); 

// Check if its ok 

var_export($confOut); 

更多關於序列化功能:Serialize

這種方法的好處是,你可以在數據庫中使用只有一列。

希望這會有所幫助!

+0

我想避免序列化數據,因爲我希望能夠在可能的情況下將關係結構保持在一起。 – josh