2013-05-20 7 views
1

MySQL의 데이터를 PHP와 결합하여 같은 테이블에 표시하는 데 도움이 필요합니다. 이제동일한 ID의 테이블에 데이터 결합.

나는이 받고 있어요 :

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 28  | Salad | 4 | 10.99| 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 28  | Pizza | 1 | 15 | 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 26  | Fish | 3  | 12 | 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 22  | Pizza | 1  | 15 | 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 22  | Salad | 1 | 10.99| 
-------------------------------------- 

을 그리고 나는 이런 식으로 보여주고 싶은 :

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 28  | Salad | 4 | 10.99| 
| 28  | Pizza | 1 | 15 | 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 26  | Fish | 3  | 12 | 
-------------------------------------- 

-------------------------------------- 
|Order ID | Product | Quantity| Cost | 
-------------------------------------- 
| 22  | Pizza | 1  | 15 | 
| 22  | Salad | 1 | 10.99| 
-------------------------------------- 

내 PHP 코드 :

$username2= $_SESSION['Username']; 
$result = mysql_query("SELECT * FROM order_detail, products WHERE order_detail.user = '$username2' AND order_detail.productid = products.serial ORDER BY orderid DESC"); 


while($row = mysql_fetch_array($result)) 
{ 
echo "<table class='curvedEdges'> 
<tr> 
<th>Order ID</th> 
<th>Product</th> 
<th>Quantity</th> 
<th>Cost</th> 
</tr>"; 

echo "<tr>"; 
echo "<td>" . $row['orderid'] . "</td>"; 
echo "<td>" . $row['name'] . "</td>"; 
echo "<td>" . $row['quantity'] . "</td>"; 
echo "<td>" . $row['price'] . " LT</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
} 

가장 쉬운 방법은 무엇입니까 PHP에서이 작업을 수행 하시겠습니까? 누군가 코드를 보여줄 수 있습니까? :) 감사! 이 같은

+1

사용하는 방법에 대해 알아보세요'JOIN's [_random PDO/mysqli 호언 장담 here_]. – moonwave99

답변

2

뭔가 작업을해야합니다 :

$orderID = null; 
$tableShown = false; 

while($row = mysql_fetch_array($result)) { 
    if ($orderID != $row['orderid']) { 
     if ($tableShown) 
      echo "</table>"; 

     echo "<table class='curvedEdges'> 
     <tr> 
     <th>Order ID</th> 
     <th>Product</th> 
     <th>Quantity</th> 
     <th>Cost</th> 
     </tr>"; 
     $tableShown = true; 
    } 

    echo "<tr>"; 
    echo "<td>" . $row['orderid'] . "</td>"; 
    echo "<td>" . $row['name'] . "</td>"; 
    echo "<td>" . $row['quantity'] . "</td>"; 
    echo "<td>" . $row['price'] . " LT</td>"; 
    echo "</tr>"; 

    $orderID = $row['orderid']; 
} 

if ($tableShown) 
    echo "</table>"; 

이것은 HTML table은 주문 ID의 각 변화에 대한 헤더를 다시 취득하게한다.

+0

왜 downvote? 이것은 그에게 그가 요구하는 결과를 줄 것이다. –

+0

나는 downvote하지 않았지만,''을 잘못된 위치에 표시하고있다. – Barmar

+0

좋아, 그게 내가 고칠해야한다고 생각 ... Thx –

0

나는 당신이 이것을 달성하기 위해 두 개의 루프에 갈 필요가 있다고 생각 -

$username2= $_SESSION['Username']; 
    $result2 = mysql_query("SELECT distinct orderid FROM order_detail, products WHERE order_detail.user = '$username2' AND order_detail.productid = products.serial ORDER BY orderid DESC"); 

    while($row2 = mysql_fetch_array($result2)) 
    { 
    $row2orderid = $row2['orderid']; 

    $result = mysql_query("SELECT * FROM order_detail, products WHERE order_detail.user = '$username2' AND order_detail.productid = products.serial and orderid = '$row2orderid' ORDER BY orderid DESC"); 

echo "<table class='curvedEdges'> 
    <tr> 
    <th>Order ID</th> 
    <th>Product</th> 
    <th>Quantity</th> 
    <th>Cost</th> 
    </tr>"; 

    while($row = mysql_fetch_array($result)) 
    {  
    echo "<tr>"; 
    echo "<td>" . $row['orderid'] . "</td>"; 
    echo "<td>" . $row['name'] . "</td>"; 
    echo "<td>" . $row['quantity'] . "</td>"; 
    echo "<td>" . $row['price'] . " LT</td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; 
    } 
+0

감사합니다! 둘 다 주어진 코드는 내가 원하는대로 작동합니다 :) – user2078728