2011-12-01 68 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