我创建了一个小型帮助台系统,允许用户在创建门票时上传文件。故障单存储在一个文件夹中,其ID号与存储在数据库中的故障单详细信息相匹配。现在,当我打开故障单详细信息时,我还希望它列出与该故障单关联的文件,以便我可以打开它们。到目前为止,我可以检索所有票证详细信息,但我仍然坚持使用json_encode($ files)以及如何使用我当前的JavaScript代码引用它们。有任何想法吗?
我也有问题。和..在scandir()数组中,并希望删除它们。使用注释行时,您可以在我的PHP文件中看到它使json_encode数组看起来不正确。感谢
PHP文件(摘录)
$value = $_POST['value'];
$sql = "SELECT * FROM helpdesk WHERE ID = '$value'";
$result = mysqli_query( $conn, $sql);
while( $rowEdit = mysqli_fetch_array($result))
{
echo json_encode(array($rowEdit['ID'], $rowEdit['DateCreated'], $rowEdit['Name'], $rowEdit['Company'], $rowEdit['Phone'], $rowEdit['Email']));
}
$dir = 'uploads/' . $value .'/';
$files = scandir($dir);
//$files = array_diff(scandir($dir), array('.', '..'));
echo json_encode($files);
HTML文件(JavaScript代码段)
$(function(){
/* Opens selected ticket details */
var modal = document.getElementById('modal');
var output = "";
$('#btnEdit').click(function(e){
var value = $("#tblTickets tr.selected td:first").html();
$.ajax({
type : "POST",
url : "sql_helpdesk_ticket_details.php",
data : {value:value},
success : function(output) {
var result = $.parseJSON(output);
$(".modal-body #txtID").val(result[0]);
$(".modal-body #txtDateCreated").val(result[1]);
$(".modal-body #txtName").val(result[2]);
$(".modal-body #txtCompany").val(result[3]);
$(".modal-body #txtPhone").val(result[4]);
$(".modal-body #txtEmail").val(result[5]);
modal.style.display = 'block';
}
});
});
答案 0 :(得分:1)
听起来你想要将两个输出结合起来,我可以解读。为此,通过将数据库返回存储在一个名为data
的键中,将另一个存储在名为files
的文件中,并在结尾输出到json,创建一个数组:
define('DS',DIRECTORY_SEPARATOR);
# Trim any spacing
$id = trim($_POST['value']);
# Just die if there are any errors
# I am presuming $_POST['value'] is numeric. If not, you need to bind_param
if(!empty($id)) {
if(!is_numeric($id))
die(json_encode(array('error'=>'Id not numeric')));
}
else
die(json_encode(array('error'=>'Id can not be empty')));
# As noted, if you are allowing anything but numeric, bind_param is required
$result = mysqli_query( $conn, "SELECT * FROM `helpdesk` WHERE `ID` = '{$id}'");
$data = array();
# Loop through and save to array
while($rowEdit = mysqli_fetch_array($result)) {
$data['data'][] = array(
$rowEdit['ID'],
$rowEdit['DateCreated'],
$rowEdit['Name'],
$rowEdit['Company'],
$rowEdit['Phone'],
$rowEdit['Email'])
);
}
# Create directory path
$dir = 'uploads'.DS.$value.DS;
# It is wise to check that it exists first
if(is_dir($dir)) {
$files = scandir($dir);
# I just loop through the results, but you can array_diff to filter the dots
foreach($files as $file) {
if(!is_file($file))
continue;
# Save files to array
$data['files'][] = $file;
}
}
# Output all the data
die(json_encode($data));
您的javascript必须进行调整以适应新密钥。