php - 如何使用 php 从 mysql 导出数据?
问题描述
我使用 numberic 将我的休假状态存储在 mysql 数据库中。如果编号为 1 则批准请假,如果编号为 2 则请假已被拒绝。现在我现在面临的问题是我不知道如何使用 php 将数字转换为字符串。我希望在我导出的 .csv 文件中获得批准和拒绝这个词。现在它只显示数字 1 和 2 。
$query ="SELECT tblemployees.FirstName,tblemployees.LastName,tblemployees.EmpId,tblemployees.Gender,tblemployees.Phonenumber,tblemployees.EmailId,applyleave.id,applyleave.LeaveType,applyleave.FromDate,applyleave.duration,applyleave.ToDate,applyleave.descr,applyleave.PostingDate,applyleave.sta,applyleave.adminRemark,applyleave.AdminRemarkDate from applyleave inner join tblemployees on applyleave.employeeID=tblemployees.EmpId";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>No</th>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Email</th>
<th>Employee Contact Number</th>
<th>Leave Type</th>
<th>From Date</th>
<th>To Date</th>
<th>Duration</th>
<th>Reason</th>
<th>Leave Status</th>
<th>Request Date</th>
<th>Admin Remark</th>
<th>Admin Remark Date</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$sta='.$row["sta"].';
if($sta==1){
$status="Approved";
}
if($sta==2){
$status="Rejected";
}
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td>'.$row["EmpId"].'</td>
<td>'.$row["LastName"].' '.$row["FirstName"].'</td>
<td>'.$row["EmailId"].'</td>
<td>'.$row["Phonenumber"].'</td>
<td>'.$row["LeaveType"].'</td>
<td>'.$row["FromDate"].'</td>
<td>'.$row["ToDate"].'</td>
<td>'.$row["duration"].'</td>
<td>'.$row["descr"].'</td>
<td>'.$status.'</td>
<td>'.$row["PostingDate"].'</td>
<td>'.$row["adminRemark"].'</td>
<td>'.$row["AdminRemarkDate"].'</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename='. date('Y-m-d') .' Leave Balance.xls');
echo $output;
解决方案
您的错误是您要附加点.
。到变量并使用单引号。单引号上的变量不会被解释并且它们的值不会被返回。
//Your code
$sta='.$row["sta"].'; // This will save on $sta ( .$row["sta"]. ) because you are using single quotes.
// Update to
$sta=$row["sta"]; // you don't need quotes or dots in the variable
还有 if 语句,我会将其更改为:
if($sta==1){
$status="Approved";
}else if($sta==2){
$status="Rejected";
}else{
$status = "Not Defined";
}
考虑其他值,如空值等。
推荐阅读
- javascript - 如何使用 PHP、Java Script 构建 Web 小部件并向外部站点提供自定义 CSS 控件?
- jquery - 什么是 jquery 中的 .kendobutton() 方法以及如何取消绑定和绑定此按钮的“单击”事件?
- html - webView中的HTML视频
- javascript - 如何从第二个 Javascript 文件中获取标签元素(标题、src 等)
- php - 防止 Laravel 中原始条件的 Sql 注入
- android - Android logcat没有显示异常完整数据?
- python - Pygame 没有正确运行
- excel - Selenium VBA 将数据提取到 Excel 中
- go - 如何在golang中的整数运算中获得最近的int?
- c - fwrite() c 函数写入额外字节