如何使我的数组patient[6]等于true,并且输出应为Approved


How to make my array patient[6] equals to true and the output should be Approved

如何使我的数组patient[6]等于true(true,意味着有数据,如果patient[0]内部有数据,则patient[6]应具有输出"APPROVED",如果patient[0]中没有数据,则patient[6]应具有输出"DISAPPROVED"

这是我的密码。。。我怎么才能在这里。。。

    $sql = "select patientid, firstname, lastname, gender, patienttype, philhealth, status from patients where lastname LIKE '%" . $_POST["key"] . "%' or philhealth LIKE '%" . $_POST["key"] . "%' ";
    $result = mysql_query($sql, $connection);
    $rownum = 0;
    $bgcolor = "";
    while($patient = mysql_fetch_array($result))
    {
        $rownum += 1;
        if($rownum == 2)
        {
            $bgcolor = "#FFF";
            $rownum = 0;
        }
        else
        { $bgcolor = "#f9f9f9"; }
        echo "
        <tr id='" . $patient[0] . "' style='background: " . $bgcolor . "' onclick='openphilhealthapproval() '>
                <td id='td27_cell1' style='height: 25px;'>" . $patient[5] . " </td>
                <td id='td27_cell2' style='height: 25px;'>" . $patient[0] . "</td>
                <td id='td27_cell3' style='height: 25px;'>" . $patient[1] . " " . $patient[2] . "</td>
                <td id='td27_cell4' style='height: 25px;'>" . $patient[3] . "</td> 
                <td id='td27_cell5' style='height: 25px;'>" . $patient[4] . "</td>
                <td id='td27_cell6' style='height: 25px;'>" . $patient[6] . "</td>
            </tr>
        ";
    }

您可以使用这个:($patient[0]!=")?:$patient[6]="已批准":$patient[6]="已取消批准"

您可以使用三元运算符为患者赋值[6]:

$patient[6]=($patient[0]?'已批准':'已取消批准');

对于您的问题,您可以使用三元运算符或if语句赋值。拿起你的毒药。

三元:$patient[6] = !empty($patient[0]) ? "APPROVED" : "DISAPPROVED";

If语句:

if(!empty($patient[0]))
{
    $patient[6] = "APPROVED";
}
else
{
    $patient[6] = "DISAPPROVED";
}

您也不应该使用任何PHP的mysql函数,它们是不推荐使用的且不安全的,请使用mysqli或PDO。

试试这个:

$sql = 'SELECT patientid, firstname, lastname, gender, patienttype, philhealth, status FROM patients WHERE lastname LIKE ''%' . $_POST['key'] . '%'' or philhealth LIKE ''%' . $_POST['key'] . '%''';
$result = mysql_query($sql, $connection);
$rowstyle = false;
while($patient = mysql_fetch_array($result)) {
    if($rowstyle)
        $bgcolor = '#fff';
    else
        $bgcolor = '#f9f9f9';
    $rowstyle = !$rowstyle;
    echo
        '<tr id="', htmlspecialchars($patient[0]), '" style="background: ', $bgcolor, ';" onclick="openphilhealthapproval()">', "'n",
        '<td id="td27_cell1" style="height: 25px;">', htmlspecialchars($patient[5]), '</td>', "'n",
        '<td id="td27_cell2" style="height: 25px;">', htmlspecialchars($patient[0]), '</td>', "'n",
        '<td id="td27_cell3" style="height: 25px;">', htmlspecialchars($patient[1]), '</td>', "'n",
        '<td id="td27_cell4" style="height: 25px;">', htmlspecialchars($patient[3]), '</td>', "'n",
        '<td id="td27_cell5" style="height: 25px;">', htmlspecialchars($patient[4]), '</td>', "'n",
        '<td id="td27_cell6" style="height: 25px;">', (!isset($patient[0][0]) ? 'DIS' : ''), 'APPROVED', '</td>', "'n",
        '</tr>', "'n";
}

PHP使用字符串作为字节数组,因此我们不检查其是否为"空"或字符串长度是否等于0,而是检查字符串的第一个字节是否已设置。

$patient[6] = (isset($patient[0][0]) ? 'DIS' : '') . 'APPROVED';

if(isset($patient[0][0]))
    $patient[6] = 'DIS';
else
    $patient[6] = '';
$patient[6] .= 'APPROVED';

这应该可以做到:

if(empty($patient[0])) {
    $patient[6] = "DISAPPROVED";
else {
    $patient[6] = "APPROVED";
}

当值未定义或为空(null、0、false、"、[]等)时,empty函数返回false。请参阅:http://php.net/manual/en/function.empty.php