내 데이터베이스에 이와 같은 테이블이 있습니다.
orderID| productID | qty
1 5 1
1 2 1
1 6 1
1 4 1
2 9 1
2 5 2
2 6 1
이와 비슷한 방식으로 HTML / php 페이지에 출력하고 싶습니다.
Order ID:1
Product ID | Qty
5 | 1
2 | 1
6 | 1
4 | 1
Order ID:2
Product ID | Qty
9 | 1
5 | 2
6 | 1
이것이 가능할까요? 나는 DISTINCT를 사용하려고 시도했지만 각 orderID에 대해 1 행만 출력합니다. 다음은 현재 ORDER BY 문을 사용하여 출력 한 가장 가까운 방법이지만 이것은 데이터베이스의 모든 행에 대한 헤더를 만듭니다.
<?php
if ($query = "SELECT * FROM productOrders ORDER BY orderID"){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<table>
<tr>
<th>Order ID: <?php echo $row['orderID'] ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row['productID'] ?></td>
<td><?php echo $row['qty'] ?></td>
</tr>
</table>
<?php
}
}
?>
이 시도
<?php
$query = "SELECT * FROM productOrders ORDER BY orderID";
if ($query){
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$id=$row['orderID'];
$sql="SELECT orderID, qty FROM productOrders WHERE orderID=$id";
$q=mysqli_query($connection,$sql);
if(!$q){ echo "error"; }
else { while($row1=mysqli_fetch_assoc($q)){
?>
<table>
<tr>
<th>Order ID <?php echo $id; ?></th>
</tr>
<tr>
<th>Product ID</th>
<th>Qty</th>
</tr>
<tr>
<td><?php echo $row1['productID'] ?></td>
<td><?php echo $row1['qty'] ?></td>
</tr>
</table>
<?
}
}
}
}
}
?>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다