douhao1956 2016-09-16 02:43
浏览 36
已采纳

Javascript调用PHP文件scandir()结果

I have created a small helpdesk system which allows users to uploads files when creating tickets. The tickets are stored in a folder with an ID number that matches the ticket details that are stored in a database. Now when I open the ticket details I also want it to list the files associated with that ticket so I can open them. So far I can retrieve all the ticket details but I'm stuck with the json_encode($files) and how I can reference them with my current JavaScript code. Any ideas?

I also have the issue with the . and .. in the scandir() array and would like to remove them. When using the commented line you can see in my PHP file it makes the json_encode array look incorrect. Thanks

PHP file (snippet)

$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 file (JavaScript snippet)

$(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条回答 默认 最新

  • dtc9222 2016-09-16 03:04
    关注

    It sounds like you want to combine the two outputs, from what I can decipher. To do that, create one array by storing the database return in one key called data and the other with the files named files and at the end output to 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));
    

    Your javascript will have to be adjusted to accommodate the new keys.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式