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.

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

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图