我是新来的,也刚开始学习编程。
我正在从Web项目中创建PDF文件,表中有1个整数列,其值为0,1,2。 PDF的输出是数字,但我想将数字更改为文本。
示例,如果“状态”为1,则输出为“已批准”;如果“状态”为2,则输出为“拒绝”;如果“状态”为0,则输出为“正在处理”
function fetch_data()
{
$output = '';
$conn = mysqli_connect("localhost", "root", "", "system");
$sql = "SELECT Status from tabel";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result))
{
$output .= '<tr>
<td>'.$row["Status"].'</td>
</tr>
';
}
return $output;
}
答案 0 :(得分:1)
使用状态数组最快,最简单:
$status = [
'On Process',
'Approved',
'Declined'
];
echo $status[$row["Status"]];
使用您的代码:
function fetch_data() {
$status = [
'On Process',
'Approved',
'Declined'
];
$output = '';
$conn = mysqli_connect("localhost", "root", "", "system");
$sql = "SELECT Status from tabel";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)) {
$output .= '<tr>
<td>'.$status[$row["Status"]].'</td>
</tr>';
}
return $output;
}
执行此操作的最佳方法是在数据库中拥有第二个表,该表将数字状态映射到状态文本。然后,您可以执行JOIN使其进入查询并简化代码:
表statusText
:
StatusId | StatusName
---------|-----------
0 | On Process
1 | Approved
2 | Declined
新代码:
function fetch_data() {
$output = '';
$conn = mysqli_connect("localhost", "root", "", "system");
$sql = "SELECT t.StatusName from table s JOIN statusText t ON s.Status = t.StatusId";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)) {
$output .= '<tr>
<td>'.$status[$row["StatusName "]].'</td>
</tr>';
}
return $output;
}
答案 1 :(得分:0)
您可以使用开关盒实现以下目的,
$statusText = '';
switch ($row["Status"]) {
case '1':
$statusText = 'Approved';
break;
case '2':
$statusText = 'Declined';
break;
default:
$statusText = 'On Process';
break;
}
$output .= '<tr> <td>' . $statusText . '</td> </tr> ';
答案 2 :(得分:0)
您可以这样声明一个数组集:
Hibernate: select country0_.country_id as country_1_12_0_, country0_.name as name2_12_0_, country0_.phonecode as phonecod3_12_0_ from country country0_ where country0_.country_id=?
country: India
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.country_id as country_3_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
......................until stackoverlow error
并将数组用作:
$status_array = array(1=>'Approved', 2=>'Declined', 0=>'on Process');
答案 3 :(得分:0)
只需简单地创建一个查找列表:
$statuses = [
'On Process',
'Approved',
'Declined'
];
和
$output .= '<tr>
<td>' . $statuses[$row["Status"]] . '</td>
</tr>';
答案 4 :(得分:0)
由于您的代码专门要求SELECT STR_TO_DATE('Fri May 04 08:08:42 UTC 2018', '%a %b %d %T %Y') AS to_date
声明:
SELECT STR_TO_DATE('Fri May 04 08:08:42 2018', '%a %b %d %T %Y') AS to_date
答案 5 :(得分:0)
一种好的方法是创建一个普通数组或一个关联数组,该数组存储数字:0、1、2和各自代表的文本。这样,您始终可以扩展程序以包含更多状态代码。在PHP
中,您可以创建一个常规数组,如下所示:
$statuses = array(
"On Process",
"Approved",
"Declined"
);
如果使用普通数组,则只需键入$statuses[$status_number]
,就可以使用Declined
来访问所需的文本,以便显示$statuses[2]
。但是,当您想扩展此功能以包括更多状态时,使用普通阵列可能会给您带来麻烦,尤其是当您想要更改状态的排列时,您将必须在程序中出现的所有位置对其进行更改。
使用关联数组,它看起来像这样:
$statuses = array(1=>'Approved', 2=>'Declined', 0=>'On Process');
使用关联数组确实没有任何优势,因为您使用整数作为键,由于您使用与普通数组相同的方法访问值,因此这并没有什么不同。