2011-12-01 71 views
1

我正在研究一個Web應用程序,並希望稍微改進我的代碼。SQL從三個表中選擇並保存在PHP數組中

這是SQL表結構我對應用程序進行:

enter image description here

現在我的問題:是否有PHP一個簡單的方法可以選擇(與加入或什麼?)從一個特定的信息所有表上的事項標識,並將其保存在,接着結構的數組,例如:

$events = array(
    'event_id' => 1, 
    'event_name' => '{"de":"Weltwirtschaftsforum","fr":"Forum \u00e9conomique mondial","it":"Forum Economico Mondiale"}', 
    'event_description' => '{"de":"Description DE","fr":"Description FR","it":"Description IT"}', 
    'event_lastedit' => 2011-12-01 10:23:35, 
    'locations' => array(
     array(
      'location_id' => 1, 
      'location_name' => 'Bern', 
      'location_date' => '01.01.2012', 
      'location_deadline' => 1323340607, 
      'timestamps' => array(
       array(
        'timestamp_id' => 1, 
        'timestamp_time' => '10:30', 
        'timestamp_seats' => 30 
       ), 
       array(
        'timestamp_id' => 2, 
        'timestamp_time' => '16:30', 
        'timestamp_seats' => 40 
       ) 
      ) 
     ), 
     array(
      'location_id' => 2, 
      'location_name' => 'Davos', 
      'location_date' => '02.02.2012', 
      'location_deadline' => 1323340607, 
      'timestamps' => array(
       array(
        'timestamp_id' => 3, 
        'timestamp_time' => '12:30', 
        'timestamp_seats' => 50 
       ), 
       array(
        'timestamp_id' => 4, 
        'timestamp_time' => '15:30', 
        'timestamp_seats' => 60 
       ) 
      ) 
     ) 
    ) 
); 

我希望這個問題是非常明顯的。

問候 蜘蛛

編輯: 我做了什麼至今:

$event = $event_db->querySingle("SELECT * FROM rf_events WHERE event_id={$event_id}", true) 

$rf_locations = $event_db->query("SELECT * FROM rf_locations WHERE event_id={$event_id}"); 

$locations = array(); 
$timestamps = array(); 
$counter = 0; 

// Loop sql query result and save entries in $events array. 
while ($row = $rf_locations->fetchArray()){ 

    $locations[$counter] = array(
     'location_id' => $row['location_id'], 
     'location_name' => json_decode($row['location_name'], true), 
     'location_date' => $row['location_date'], 
     'location_deadline' => $row['location_deadline'] 
    ); 

    $rf_timestamps = $event_db->query("SELECT * FROM rf_timestamps WHERE location_id={$row['location_id']}"); 

    $counter2 = 0; 

    while ($row2 = $rf_timestamps->fetchArray()){ 

     $locations[$counter]['timestamps'][$counter2] = array(
      'timestamp_id' => $row2['timestamp_id'], 
      'timestamp_time' => $row2['timestamp_time'], 
      'timestamp_seats' => $row2['timestamp_seats'] 
     ); 

     $counter2++; 
    } 

    $counter++; 
} 
+2

是的,這是可能的,你說得對,JOINSs是做正確的方式。你使用的是什麼MySQL庫?你有迄今爲止嘗試過的代碼的例子嗎? –

+0

我使用PHP的SQLite3核心庫(http://php.net/manual/de/book.sqlite3.php)。到目前爲止,我通過id進行了正常選擇,並創建了一些嵌套的while循環,並將數據中的信息推送出去。 – Spinnenzunge

+0

請修改您的帖子並顯示您到目前爲止的代碼。 –

回答

0
SELECT * FROM rf_events, rf_locations, rf_timestamps WHERE 
rf_locations.event_id = rf_events.event_id AND 
rf_timestamps.location_id = rf_locations.event_id 

然後,利用數據添加到PHP相應。

你可以參考:MYSQL JOIN

+0

正確的方向,但與您的代碼我得到以下陣列提取: '陣列 ( [event_id] => 1 [event_name] => {「de」:「Weltwirtschaftsforum」,「fr」:「Forum \ u00e9conomique mondial」 ,「it」:「Forum Economico Mondiale」} [event_desc] => {「de」:「Description DE」,「fr」:「Description FR」,「it」:「Description IT」} [event_lastedit] = > 2011-12-01 10:23:35 [location_id] => 1 [location_name] => {「de」:「達沃斯」,「fr」:「達沃斯」,「it」:「達沃斯」} [location_date] => 10.10.2010 [location_deadline] => 1323340607 [timestamp_id] => 1 [timestamp_time] => 11:30 [timestamp_seats] => 50 )' – Spinnenzunge