我有一个查询,我已经通过数组从Sql查询中撤回结果,如下所示。
<?php
$sql = "SELECT inc.STATUS as STATUS,
FROM dbo.HELP_DESK as inc";
$result = sqlsrv_query($conn, $sql);
$status=(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);
echo "<table><tr id=\"header\"><td><center>Status</center></td><tr>";
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
echo "<tr><td><b><center> " . $row["STATUS"] . "</center></b></td></tr>";
}
echo "</table>";
sqlsrv_close( $conn );
?>
$row
从数据库中取回一个数字。但是,我想要显示我的$ status数组中的文本。
我尝试了几种方法,但最终还是返回了一个空白屏幕。
有什么建议吗?感谢帮助。
答案 0 :(得分:2)
更改你的while循环,如下所示。检查状态编号在$status
中可用,如果可用,则显示其值,否则显示空白
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$status_str = isset($status[$row["STATUS"]])?$status[$row["STATUS"]]:"";
echo "<tr><td><b><center> " . $status_str . "</center></b></td></tr>";
}
同时将您的数组更改为(您忘记了array
关键字)
$status=array(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);
答案 1 :(得分:0)
另请阅读:http://php.net/manual/en/function.strtr.php
strtr($row["STATUS"], $status);
答案 2 :(得分:0)
由于PHP错误导致空白屏幕?你应该检查你的日志。 因为,在你的例子中,你写了这样的数组:
$status=(
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
);
但你必须改用括号:
$status=[
'1' => "Request For Authorization",
'3' => "Pending",
'4' => "Scheduled For Review",
'5' => "Scheduled For Approval",
'8' => "Pending",
];
然后你必须用这样的东西显示标签:
echo $status[$row['STATUS']];
顺便说一句,您应该使用(微)框架进行开发,因为您应该(或必须)不在您的数据库中编写查询并在同一文件中显示resuslts。
HTH。