PHP - 在功能名称上显示“否”

时间:2016-03-01 14:15:50

标签: php mysql

这是我的数据库:
enter image description here

以下是我尝试过的代码:

<?php
//---DB Connection---

//---FIND ALL AMBANK RECORD---
$q = "SELECT * 
      FROM mtid 
      WHERE provider = Ambank";
$r = mysqli_query($dbc, $q);

//---CREATE TABLE---
echo '<table border="1" style="border-collapse:collapse">';
echo '<tr>';
echo '<th>Serial No</th>';
echo '<th>Feat A?(Y/N)</th>';
echo '<th>Feat B?(Y/N)</th>';
echo '<th>Feat C?(Y/N)</th>';
echo '<th>Feat D?(Y/N)</th>';
echo '</tr>';

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
    //---FIND FEATURE ID---
    $q1 = "SELECT feat_ID
           FROM functionfeatures 
           WHERE sNo = '".$row['sNo']."' && mmID = '".$row['mmID']."' &&
           ttID = '".$row['ttID']."'";
    $r1 = mysqli_query($dbc, $q1);

    //---[HERE]---
    echo '<tr>'; 
    echo '<td>'.$row['sNo'].'</td>';
    ...
    echo '</tr>';
}

echo '</table>';
?>

我想制作如下表格:
enter image description here

此表由5个字段组成,分别是序列号,功能A,功能B,功能C,功能D.
从我的数据库中,序列号11-11-11-11具有功能B,功能C,功能D.因此,我想在功能A上显示否,在功能B上显示是,在功能表中显示功能C和功能D.

但是,我仍然不知道如何比较功能ID并在功能名称上显示是/否。

我应该如何从[HERE]部分继续? 有人能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

您只能使用一个查询:

$query = "SELECT
    m.sNo,
    f.feat_name,
    CASE
        WHEN ff.sNo IS NULL THEN 'No'
        ELSE 'Yes'
    END AS yesno
FROM
    mtid m
    JOIN features f
    LEFT JOIN functionfeatures ff ON ff.feat_ID = f.ID AND ff.sNo = m.sNo
WHERE provider = 'Ambank'
ORDER BY
    m.sNo,
    f.feat_name";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$sNo = '';
echo '<table border="1">
    <tr>
        <td>sNo</td>
        <td>Feat A</td>
        <td>Feat B</td>
        <td>Feat C</td>
        <td>Feat D</td>
    </tr>
    <tr>';
        while($row = mysqli_fetch_array($result)){
            if($sNo != $row['sNo']){
                if($sNo != '') echo '</tr><tr>';
                echo '<td>'.$row['sNo'].'</td>';
                $sNo = $row['sNo'];
            }
            echo '<td>'.$row['yesno'].'</td>';
        }
    echo '</tr>
</table>';

输出是:

documentation

答案 1 :(得分:0)

试试这个:

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
    // set default values
    $array = array(
        "1" => "NO",
        "2" => "NO",
        "3" => "NO",
        "4" => "NO"
    );

    $q1 = "SELECT feat_ID
           FROM functionfeatures 
           WHERE sNo = '".$row['sNo']."' && mmID = '".$row['mmID']."' &&
           ttID = '".$row['ttID']."'";
    $r1 = mysqli_query($dbc, $q1);

    // if we have a feat_ID match - set its value in the array to YES
    while($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC))
    {
        $array[''.$row1['feat_ID'].''] = "YES";
    }

    echo '<tr>'; 
    echo '<td>'.$row['sNo'].'</td>';

    // output the values for each function ID
    foreach ($array as $yesno) {
         echo '<td>'.$yesno.'</td>';   
    }


    echo '</tr>';
}

答案 2 :(得分:0)

我会接近它有点不同。如上所述使用JOINs,但也将代码分开以将前端与后端分开(阅读The MVC Pattern):

您的后端处理数据,获取SQL结果并构建前端所需的数组:

<?php
// $dbc is set in your ---DB Connection---

$sql = "SELECT m.sNo, f.feat_name, ff.feat_ID
        FROM mtid m
        JOIN features f
        LEFT JOIN functionfeatures ff ON ff.feat_ID = f.ID AND ff.sNo = m.sNo
        WHERE provider = 'Ambank'
        ORDER BY m.sNo, f.feat_name";

$result = mysqli_query($dbc, $sql) or die(mysqli_error($dbc));

$yes_no_feats = [];
while ($row = mysqli_fetch_assoc($result)) {
    $yes_no_feats[$row['sNo']][] = $row;
}

mysqli_close($dbc);
?>

正如您所看到的,变量$yes_no_feats保存结果,然后将结果传递给前端,以便可以使用HTML解析它:

<table border="1">
    <tr>
        <th>Serial No</th>
        <th>Feat A</th>
        <th>Feat B</th>
        <th>Feat C</th>
        <th>Feat D</th>
    </tr>
    <?php
    foreach ($yes_no_feats as $sno => $features) {
        ?>
        <tr>
            <td><?= $sno ?></td>
            <?php foreach ($features as $f) { ?>
                <td><?= (!empty($f['feat_ID'])) ? "Yes" : "No" ?></td>
            <?php } ?>
        </tr>
        <?php
    }
    ?>
</table>

希望这有帮助。