2016-09-16 6 views
0

티켓을 만들 때 파일을 업로드 할 수있는 작은 헬프 데스크 시스템을 만들었습니다. 티켓은 데이터베이스에 저장된 티켓 세부 사항과 일치하는 ID x 호가있는 폴더에 저장됩니다. 이제 티켓 세부 정보를 열면 해당 티켓과 관련된 파일을 나열하여 열 수 있습니다. 지금까지 모든 티켓 세부 정보를 검색 할 수 있지만 json_encode ($ 파일)가 붙어 있고 현재 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 파일 (자바 스크립트 조각은)

$(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

소리가 난다. 이를 위해 files라는 이름의 파일에 data라는 하나의 키의 데이터베이스 반환 및 다른 저장함으로써 및 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)); 

자바 스크립트는을 수용 할 수 있도록 조정되어야 할 것이다 새 키.

+0

내 json_encode의 형식은 { "data1", "data2", "files": [ ".", "file1", "file2" ]}, JavaScript에서 'data'배열 객체 'data2'를 어떻게 참조할까요? 'var result = $ .parseJSON (출력); $ (". modal-body #txtID") .val (결과 [데이터] [2]); ' – user2168287

+0

죄송합니다, 나는 그것을 해결했습니다. '$ (". 모달 - 몸 #txtID") .Val (result.data [2]);' – user2168287