PHP匹配2个选中的表

I've to select 2 table from mysql. After select the data from query and I would to to match for this 2 table. Code is in below

echo "<table>";

foreach($tableA as $tableA){
    echo "<tr>";
    echo "<td>" . $tableA['ID'] . "</td>";
    echo "<td>" . $tableA['Name'] . "</td>";
    echo "<td>" . $tableA['Date'] . "</td>";

    // Start a new table for description    
    echo "<td><table>"; 
    foreach($tableB as $tableB){                    
        if ($tableA['ID'] = $tableB['ID']){
            echo "<td>" . $tableB['Item'] . "</td>";
        }
        else {
            // do nothing
        }
    }               
    echo "</table></td>";
    echo "</tr>";
}
echo "</table>";

Table A

ID  Name    Date
1   Test 1  1/01/2014
2   Test 2  2/01/2014
3   Test 3  3/01/2014
4   Test 4  4/01/2014
5   Test 5  5/01/2014

Table B

ID  Item
1   Tomato
1   Orange
2   Apple
2   Watermelon
3   Honeydrew
4   Pineapple
4   Durian
5   Grape

From this 2 table A and B, I select from query, but from the PHP codeigniter View, how can match this 2 table to below sample table? Please help. Join the data:

ID  Name    Date        Item
1   Test 1  1/01/2014   Tomato
                        Orange
2   Test 2  2/01/2014   Apple
                        Watermelon
3   Test 3  3/01/2014   Honeydrew
4   Test 4  4/01/2014   Pineapple
                        Durian
5   Test 5  5/01/2014   Grape

Below may satisfy you.

echo "<table>";

foreach($tableA as $rowA){
    echo "<tr>";
    echo "<td>" . $rowA['ID'] . "</td>";
    echo "<td>" . $rowA['Name'] . "</td>";
    echo "<td>" . $rowA['Date'] . "</td>";

    // Start a new table for description    
    echo "<td><table>"; 
    foreach($tableB as $rowB){    
        if ($rowA['ID'] == $rowB['ID']){
            echo "<tr>" . $rowB['Item'] . "</tr>";
        }   
        else {
            // do nothing
        }   
    }        
    echo "</table></td>";
    echo "</tr>";
}
echo "</table>";

Matching the data in PHP after it is pulled from separate queries is very sloppy and generally doesnt work well. Just join them in your MySQL query and the data will come out already matched up.

Example:

$query = "SELECT a.id, a.name, a.date, b.item 
          FROM tableA a 
          LEFT JOIN tableB b
          ON a.id = b.id 
          WHERE {your selection criteria here}
          ORDER BY a.name ASC, a.date ASC"

Then loop through it

$result_set = mysql_query($query,$connection);

echo "<table>";
while($result = mysql_fetch_assoc($result_set)) {
    echo "<tr>" . "<td>{$result['id']}</td>" . "<td>{$result['name']}</td>" . "<td>{$result['date']}</td>" . "<td>{$result['item']}</td>" . "</tr>";
}
echo "</table>";

clean and easy.

EDIT: New code below regarding your comment

$result_set = mysql_query($query,$connection);
$previousName = "";
$previousDate = "";

echo "<table>";
while($result = mysql_fetch_assoc($result_set)) {
    $currentName = $result['name']; $currentDate = $result['date'];

    if (($currentName == $previousName && $currentDate == $previousDate) || $previousName = "") {
        $id = $result['id']; $name = $result['name']; $date = $result['date']; $item .= ", ".$result['item'];
    } else {
        echo "<tr>" . "<td>{$id}</td>" . "<td>{$name}</td>" . "<td>{$date}</td>" . "<td>{$item}</td>" . "</tr>";
        $id = $result['id']; $name = $result['name']; $date = $result['date']; $item = $result['item'];
    }  

    $previousName = $result['name']; $previousDate = $result['date'];
}
echo "<tr>" . "<td>{$id}</td>" . "<td>{$name}</td>" . "<td>{$date}</td>" . "<td>{$item}</td>" . "</tr>";
echo "</table>";