2013-12-14 64 views
1

我從數據庫中的兩個不同表中提取數據。一旦我擁有了我所需要的全部內容,我想要將這些數組合並打印到HTML表格中。組合數組將不會顯示在單個表格行中

它們都結合在一起,但從它看起來array_merge正在將第二個數組追加到第一個數組中,就像它應該那樣。但是,我的問題是,當我從新組合數組中的數據創建一個表時,它將在一行中顯示我想從第一個數組獲得的所有字段,並在第二行顯示第二個數組中的所有數據。有沒有辦法強制array_merge 100%將數組合併成一個?

$names = Array (
[0] => Array (
    [PointID] => -3621 
    [AddressID] => -503718 
    [CommonName] => AARONS 
    [MapDataID] => -2 
    [AlternateAddressID] => 
    [GISObjectID] => 28 
    [DateChanged] => 2013-06-19 13:48:44.947 
    [EnteredDate] => 2010-09-21 08:21:26.847 
) 
) 

$addressInfo= Array (
[0] => Array (
    [AddressID] => -503718 
    [LocationType] => 1 
    [HouseNumber] => XXXX 
    [AdditionalInfo] => 
    [...] 
) 
) 

陣列一旦合併:

Array ( 
[0] => Array ([PointID] => -3621 [AddressID] => -503718 [CommonName] => XXXXX) 
[1] => Array ([AddressID] => -503718 [LocationType] => 1 
    [HouseNumber] => XXXX [AdditionalInfo] => [StreetPrefix] => N 
    [StreetPreType] => [StreetName] => XXXX [StreetType] => AVE 
    [StreetSuffix] => [Apt] => [XStreetPrefix] => [XStreetPreType] => 
    [XStreetName] => [XStreetType] => [XStreetSuffix] => [VenueName] => XXXX 
    [City] => XXXXX [vsState] => 52 [Zip] => XXXX) 
) 

代碼:

echo "<table> 
<tr> 
<th>CN</th> 
<th>House#</th> 
<th>St Name</th> 
<th>St Prefix</th> 
<th>St Type</th> 
<th>City</th> 
<th>Zip</th> 
</tr>"; 
//Combine the arrays into one 
$output = array_merge($names, $addressInfo); 
print_r($output); 
//Run a loop 
foreach ($output as $test) 
    { 
    echo "<tr>"; 
    echo "<td>".$test['CommonName']."</td>"; 
    echo "<td>".$test['HouseNumber']."</td>"; 
    echo "<td>".$test['StreetName']."</td>"; 
    echo "<td>".$test['StreetPrefix']."</td>"; 
    echo "<td>".$test['StreetType']."</td>"; 
    echo "<td>".$test['City']."</td>"; 
    echo "<td>".$test['Zip']."</td>"; 
    echo "</tr>"; 
    } 
echo "</table>"; 

回答

0

好吧,我正在創建另一個更清晰的答案。

這是您當前的代碼:

$cn = "AARONS"; //Connect to NWSDB 

$pdo = new PDO("odbc:NWS","docserver","docserver"); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$result = $pdo->prepare("SELECT CommonName,AddressID from CommonNameAddress where CommonName like :name"); 
$result->execute(array(':name' => "%$cn%")); 
//Put the results in an array for use later in the table 
$names = array(); 
$addquery = array(); 
while($row = $result->fetch(PDO::FETCH_ASSOC)) { 
    $addquery[] = $row['AddressID']; 
    $names[] = $row; 
} 



$pdo = new PDO("odbc:NWS","docserver","docserver"); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
//Create an array for storing the address information from the below query 
$addressInfo = array(); 
//Loop the query to get a result for every CommonName 
foreach ($addquery as $address) { 
    //Match the AddressID from the CommonNames Table to the Address information in the Address Table 
    $common = $pdo->prepare("SELECT HouseNumber,StreetPrefix,StreetName,StreetType,City,Zip from Address where AddressID like :id"); 
    $common->execute(array(':id' => $address)); 
    //Put the results in an array for use later in the table 
    while($row2 = $common->fetch(PDO::FETCH_ASSOC)) { $addressInfo[] = $row2; } 
} 

這裏是你能做什麼:

$cn = "AARONS"; //Connect to NWSDB 

$pdo = new PDO("odbc:NWS","docserver","docserver"); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$result = $pdo->prepare("SELECT CommonName, CommonNameAddress.AddressID, Address.HouseNumber, Address.StreetPrefix, Address.StreetName, Address.StreetType, Address.City, Address.Zip from CommonNameAddress LEFT JOIN Address ON Address.AddressID=CommonNameAddress.AddressID where CommonName like :name"); 
$result->execute(array(':name' => "%$cn%")); 
//Put the results in an array for use later in the table 
$names = array(); 
while($row = $result->fetch(PDO::FETCH_ASSOC)) { 
    $names[] = $row; 
} 
print_r($names); 
?> 

告訴我們是怎麼回事。

+0

該死!這很好!非常感謝soooo!我終於用自己的for循環來想出它,但是你的方法效率提高了十億倍,所以我就這麼做了!再次感謝! –

+0

Coool :-)請加我的答案。祝你的項目好運。 – Thibault

0

顯然你的2個陣列$names$addressInfo已經包含了數據的另一個層次。

你可能想這樣做:

$output = array_merge($names[0], $addressInfo[0]); 

如果它不能正常工作,請同時提供$names$addressInfoprint_r()輸出在合併之前。

+0

那個真的很接近的男人。我改變了你的行: '$ output [] = array_merge($ names [0],$ addressInfo [0]);' 修復它,但問題是它只是返回一行結果。 –

+0

只需給我們'print_r($ names)'和'print_r($ addressInfo)',我們將能夠爲您提供幫助。 – Thibault

+0

$ names = 'Array([0] => Array([PointID] => -3621 [AddressID] => -503718 [CommonName] => AARONS [MapDataID] => [AlternateAddressID] => [GISObjectID ] => 28 [DateChanged] => 2013-06-19 13:48:44.947 [EnteredDate] => 2010-09-21 08:21:26.847))' –

相關問題