행에서 동일한 ID를 사용하지만 다른 열에서 다른 값을 가진 데이터를 가져와 PHP의 테이블에 표시하는 방법

탄 질라 이슬람

내 SQL 테이블에 다음 데이터가 있습니다.

id      code     day     time    year
 1     PRC-001    0       t-1     2017
 2     PRC-001    1       t-2     2017
 3     PRC-002    0       t-3     2017
 4     PRC-002    1       t-4     2017
 5     PRC-003    0       t-5     2017
 6     PRC-003    1       t-6     2017

출력은 다음과 같아야합니다.

day0     day1     code      
 t-1      t-2    PRC-001
 t-3      t-4    PRC-002    
 t-5      t-6    PRC-003   

어떻게 할 수 있습니까? 다음 코드와 같은 것을 시도했습니다. 그러나 나는 욕망 결과를 얻지 못했습니다. 내 코드는 다음과 같습니다.

$query1 = "SELECT * FROM routine AS tab1,"; 
$query1.= " GROUP BY code";

$rs = mysql_query($query1);
$numOfRows=mysql_num_rows($rs);
$printed = array();
$resultset = array();

while($row = mysql_fetch_assoc($rs)) {
    $resultset[] = $row;
    #print_r($row);
}

$q = "<table id='ash'>";
$q.= "<tr id='grey'>";
$q.= "<th rowspan='2'>day0</th>";
$q.= "<th rowspan='2'>day1(s)</th>";
$q.= "<th rowspan='2'>code</th></tr>";
$q.= "</tr>";


foreach ($resultset as $row){ 
    $q.= "<tr>";
    $q.= "<tr><td id='clist'>".$row["time"]."</td>";
    $q.= "<td id='clist'>".$row["time"]."</td>";
    $q.= "<td id='clist'>".$row["code"]."</td></tr>";
} 
$q .= "</table>";
echo $q;
Rwd

첫째, 주석에서 언급했듯이 mysql_함수 이외의 다른 것을 사용하는 것을보아야 합니다.

둘째, 쿼리를 사용하여 GROUP BY.

그런 다음 다음과 같이 할 수 있습니다.

$rs = mysql_query("SELECT * FROM routine");

$results = [];

while ($row = mysql_fetch_assoc($rs)) {

    $code = $row['code'];

    if (!isset($results[$code])) {
        $results[$code] = [
            'day0' => '-',
            'day1' => '-',
        ];
    }

    $results[$code]['day' . $row['day']] = $row['time'];

}

?>

<table>
    <thead>
    <tr id="grey">
        <th rowspan="2">Day0</th>
        <th rowspan="2">Day1(s)</th>
        <th rowspan="2">code</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($results as $code => $result) : ?>
        <!--You shouldn't have multiple elements using the same ids-->
        <tr>
            <td id='clist'><?php echo $result['day0'] ?></td>
            <td id='clist'><?php echo $result['day1'] ?></td>
            <td id='clist'><?php echo $code ?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>

도움이 되었기를 바랍니다!

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관