2016-09-16 72 views
0

我创建了一个小型帮助台系统,允许用户在创建票据时上传文件。故障单存储在ID号与存储在数据库中的故障单详细信息相匹配的文件夹中。现在,当我打开票证详情时,我还希望它列出与该票证关联的文件,以便我可以打开它们。到目前为止,我可以检索所有的票据细节,但我坚持使用json_encode($ files),以及如何使用当前的JavaScript代码引用它们。有任何想法吗?JavaScript调用PHP文件scandir()结果

我也有问题。和..在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'; 
       } 
      });    
    }); 

回答

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将不得不进行调整以适应新钥匙。

+0

感谢@Rasclatt,现在我的json_encode格式为{“data”:[“data1”,“data2”],“files”:[“。”,“..”,“file1”,“file2” ]},从JavaScript我怎么引用'数据'数组对象'data2'例如。 'var result = $ .parseJSON(output); (“。modal-body #txtID”).val(result [data] [2]);' – user2168287

+0

对不起,我解决了。 '$(“。modal-body #txtID”).val(result.data [2]);' – user2168287