Php JQuery Ajax-Order在1个字段上工作

Php JQuery Ajax-Order在1个字段上工作,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我是jquery/ajax的新手,正在处理一个简单的ajax/jqueryhtml表-MySql。除了“点击订购”字段外,所有操作都正常。单击列标题(期望在此字段中排序)后,它将在上一列/字段中排序。我做了一些调整,但还是一样。原始代码来自phpzag.com <?php include('inc/header.php'); ?> <title>phpzag.com : Demo Live Add Edit Delete datatables Records with

我是jquery/ajax的新手,正在处理一个简单的ajax/jqueryhtml表-MySql。除了“点击订购”字段外,所有操作都正常。单击列标题(期望在此字段中排序)后,它将在上一列/字段中排序。我做了一些调整,但还是一样。原始代码来自phpzag.com

<?php 
include('inc/header.php');
?>

<title>phpzag.com : Demo Live Add Edit Delete datatables Records with Ajax, PHP and MySQL</title>

<?php //include('inc/container.php');
    $Col0 = "id";
    $Col1 = "Topic";
    $Col2 = "FileName";
    $Col3 = "Size";
    $Col4 = "Created";
    $Col5 = "Modified";
    $Col6 = "Folder";
?>
<div class="container contact"> 

    <h2>Example: Live Add Edit Delete datatables Records with Ajax, PHP and MySQL</h2>  
    <div class="col-lg-10 col-md-10 col-sm-9 col-xs-12">        

        <div class="panel-heading">
            <div class="row">
                <div class="col-md-10">
                    <h3 class="panel-title"></h3>
                </div>
                <div class="col-md-2" align="right">
                    <!--- <button type="button" name="add" id="addRecord" class="btn btn-success">Add New Record</button> <!-- Original---> 
                    <button type="button" name="add" id="addRtf" class="btn btn-success">Add New WordPad</button> <!--- Manoj --->
                </div> 
            </div>
        </div> 
        <table id="recordListing" class="table table-bordered table-striped"> <!-- ManojComment View page + update/close button ---> 
            <thead>
                <tr>
                        <!---<th>#</th> --->
                        <!---<th><?php //echo $Col0; ?></th> --->
                        <th><?php echo $Col1; ?></th>                   
                        <th><?php echo $Col2; ?></th>                   
                        <th><?php echo $Col3; ?></th>
                        <th><?php echo $Col4; ?></th>
                        <th><?php echo $Col5; ?></th>                   
                        <th><?php echo $Col6; ?></th>
                        <th>#</th>
                        <!--- <th></th> //Manoj removed - avoid del/update buttona              
                        <th></th> -->
                        <th></th>
                </tr>
            </thead>
        </table>
    </div>
    <div id="recordModal" class="modal fade">
        <div class="modal-dialog">
            <form method="post" id="recordForm">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title"><i class="fa fa-plus"></i> Edit Record</h4> 
                    </div>
                    <div class="modal-body"> <!--- ManojComment EDIT modal page --->
                        <div class="form-group">
                            <label for="<?php echo $Col0; ?>" class="control-label"><?php echo $Col0; ?></label>
                            <input type="text" class="form-control" id="<?php echo $Col0; ?>" name="<?php echo $Col0; ?>" placeholder="<?php echo $Col0; ?>" required>          
                        </div>
                        <div class="form-group">
                            <label for="<?php echo $Col1; ?>" class="control-label"><?php echo $Col1; ?></label>
                            <input type="text" class="form-control" id="<?php echo $Col1; ?>" name="<?php echo $Col1; ?>" placeholder="<?php echo $Col1; ?>" required>          
                        </div>
                        <div class="form-group">
                            <label for="<?php echo $Col2; ?>" class="control-label"><?php echo $Col2; ?></label>                            
                            <input type="number" class="form-control" id="<?php echo $Col2; ?>" name="<?php echo $Col2; ?>" placeholder="<?php echo $Col2; ?>">                         
                        </div>      
                        <div class="form-group">
                            <label for="<?php echo $Col3; ?>" class="control-label"><?php echo $Col3; ?></label>                            
                            <input type="text" class="form-control"  id="<?php echo $Col3; ?>" name="<?php echo $Col3; ?>" placeholder="<?php echo $Col3; ?>" required>                         
                        </div>   
                        <div class="form-group">
                            <label for="<?php echo $Col4; ?>" class="control-label"><?php echo $Col4; ?></label>                            
                            <textarea class="form-control" rows="5" id="<?php echo $Col4; ?>" name="<?php echo $Col4; ?>"></textarea>                       
                        </div>
                        <div class="form-group">
                            <label for="<?php echo $Col5; ?>" class="control-label"><?php echo $Col5; ?></label>                            
                            <input type="text" class="form-control" id="<?php echo $Col5; ?>" name="<?php echo $Col5; ?>" placeholder="<?php echo $Col5; ?>">
                        </div>  
                        <div class="form-group">
                            <label for="<?php echo $Col6; ?>" class="control-label"><?php echo $Col6; ?></label>                            
                            <input type="text" class="form-control" id="<?php echo $Col6; ?>" name="<?php echo $Col6; ?>" placeholder="<?php echo $Col6; ?>">
                        </div>  
                    </div>
                    <div class="modal-footer">
                        <input type="hidden" name="id" id="id" />
                        <input type="hidden" name="action" id="action" value="" />
                        <input type="submit" name="save" id="save" class="btn btn-info" value="Save" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>  
<?php include('inc/footer.php');?>
Records.php

class Records { 

    private $recordsTable = 'files';
    public $id;
    public $Topic;
    public $FileName;
    public $Size;
    public $Created;
    public $Modified;
    public $Folder;
    private $conn;

    public function __construct($db){
        $this->conn = $db;
    }       





    public function listRecords(){
        $Col0 = "id";
        $Col1 = "Topic";
        $Col2 = "FileName";
        $Col3 = "Size";
        $Col4 = "Created";
        $Col5 = "Modified";
        $Col6 = "Folder";

        $sqlQuery = "SELECT * FROM ".$this->recordsTable." ";
        if(!empty($_POST["search"]["value"])){
            $sqlQuery .= 'where('.$Col0.' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col1 .' LIKE "%'.$_POST["search"]["value"].'%" ';          
            $sqlQuery .= ' OR '.$Col2 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col3 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col4 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col5 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col6 .' LIKE "%'.$_POST["search"]["value"].'%") ';         
        }

        if(!empty($_POST["order"])){
            $sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
        } else {
            $sqlQuery .= 'ORDER BY id DESC ';
        }

        if($_POST["length"] != -1){
            $sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
        }

        $stmt = $this->conn->prepare($sqlQuery);
        $stmt->execute();
        $result = $stmt->get_result();  

        $stmtTotal = $this->conn->prepare("SELECT * FROM ".$this->recordsTable);
        $stmtTotal->execute();
        $allResult = $stmtTotal->get_result();
        $allRecords = $allResult->num_rows;

        $displayRecords = $result->num_rows;
        $records = array();     
        while ($record = $result->fetch_assoc()) {              
            $rows = array();            

            //$rows[] = ucfirst($record[$Col1]);//original
            $rows[] = $record[$Col1];//manoj as order not working properly, test
            $rows[] = $record[$Col2];       
            $rows[] = $record[$Col3];   
            $rows[] = $record[$Col4];
            $rows[] = $record[$Col5];                   
            $rows[] = $record[$Col6];
            $rows[] = $record[$Col0];
            //$rows[] = '<button type="button" name="update" id="'.$record["id"].'" class="btn btn-warning btn-xs update">Update</button>'; //original
            //$rows[] = '<button type="button" name="delete" id="'.$record["id"].'" class="btn btn-danger btn-xs delete" >Delete</button>'; //Original
            $rows[] = '<button type="button" name="open" id="'.$record["id"].'" class="btn btn-warning btn-xs open" >Open</button>';
            $records[] = $rows;
        }

        $output = array(
            "draw"  =>  intval($_POST["draw"]),         
            "iTotalRecords" =>  $displayRecords,
            "iTotalDisplayRecords"  =>  $allRecords,
            "data"  =>  $records
        );

        echo json_encode($output);
    }
类记录{
private$recordsTable='files';
公费$id;
公共话题;
public$FileName;
公帑数额;;
创建公共资源;
修改公共预算;
公用$Folder;
私人康涅狄格州;
公共函数构造($db){
$this->conn=$db;
}       
公共职能清单记录(){
$Col0=“id”;
$Col1=“主题”;
$Col2=“文件名”;
$Col3=“大小”;
$Col4=“已创建”;
$Col5=“已修改”;
$Col6=“文件夹”;
$sqlQuery=“从“$this->recordsTable”中选择*;
如果(!empty($_POST[“search”][“value”])){
$sqlQuery.='where('.$Col0.'像“%”.$\u POST[“search”][“value”].%'”;
$sqlQuery.='或'$Col1.'类似于“%”。$\u POST[“search”][“value”].%';
$sqlQuery.='或'$Col2.'类似于“%”。$\u POST[“search”][“value”].%';
$sqlQuery.='或'$Col3.'类似于“%”。$\u POST[“search”][“value”].%';
$sqlQuery.='或'$Col4.'类似于“%”。$\u POST[“search”][“value”].%';
$sqlQuery.='或'$Col5.'类似于“%”。$\u POST[“搜索”][“值”].%';
$sqlQuery.='或'$Col6.'类似“%”。$\u POST[“搜索”][“值”].%');
}
如果(!空($_POST[“order”])){
$sqlQuery.='ORDER BY'.$\u POST['ORDER']['0']['column']...$\u POST['ORDER']['0']['dir'.';
}否则{
$sqlQuery.='ORDER BY id DESC';
}
如果($_POST[“length”]!=-1){
$sqlQuery.='LIMIT'.$\u POST['start'.],'.$\u POST['length'];
}
$stmt=$this->conn->prepare($sqlQuery);
$stmt->execute();
$result=$stmt->get_result();
$stmtottal=$this->conn->prepare(“从“$this->recordsTable”中选择*);
$stmtottal->execute();
$allResult=$stmtottal->get_result();
$allRecords=$allResult->num\u行;
$displayRecords=$result->num\u行;
$records=array();
而($record=$result->fetch_assoc()){
$rows=array();
//$rows[]=ucfirst($record[$Col1]);//原始
$rows[]=$record[$Col1];//由于订单工作不正常,请测试
$rows[]=$record[$Col2];
$rows[]=$record[$Col3];
$rows[]=$record[$Col4];
$rows[]=$record[$Col5];
$rows[]=$record[$Col6];
$rows[]=$record[$Col0];
//$rows[]=“更新”;//原始
//$rows[]=“删除”;//原始
$rows[]=“打开”;
$records[]=$rows;
}
$output=array(
“draw”=>intval($\u POST[“draw”]),
“iTotalRecords”=>$displayRecords,
“iTotalDisplayRecords”=>$allRecords,
“数据”=>$records
);
echo json_编码($output);
}

有人能帮忙吗?有人能帮忙吗。
<?php
class Database{

    private $host  = 'localhost';
    private $user  = 'root';
    private $password   = "dd";
    private $database  = "dd"; 

    public function getConnection(){        
        $conn = new mysqli($this->host, $this->user, $this->password, $this->database);
        if($conn->connect_error){
            die("Error failed to connect to MySQL: " . $conn->connect_error);
        } else {
            return $conn;
        }
    }
}
$(document).ready(function(){   

    var dataRecords = $('#recordListing').DataTable({
        "lengthChange": false,
        "processing":true,
        "serverSide":true,
        'processing': true,
        'serverSide': true,
        'serverMethod': 'post',     
        "order":[],
        "ajax":{
            url:"ajax_action.php",
            type:"POST",
            data:{action:'listRecords'},
            dataType:"json"
        },
        /*"columnDefs":[
            {
                "targets":[0, 6, 7],
                "orderable":false,
            },
        ],*/
        "pageLength": 100
    }); 
class Records { 

    private $recordsTable = 'files';
    public $id;
    public $Topic;
    public $FileName;
    public $Size;
    public $Created;
    public $Modified;
    public $Folder;
    private $conn;

    public function __construct($db){
        $this->conn = $db;
    }       





    public function listRecords(){
        $Col0 = "id";
        $Col1 = "Topic";
        $Col2 = "FileName";
        $Col3 = "Size";
        $Col4 = "Created";
        $Col5 = "Modified";
        $Col6 = "Folder";

        $sqlQuery = "SELECT * FROM ".$this->recordsTable." ";
        if(!empty($_POST["search"]["value"])){
            $sqlQuery .= 'where('.$Col0.' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col1 .' LIKE "%'.$_POST["search"]["value"].'%" ';          
            $sqlQuery .= ' OR '.$Col2 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col3 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col4 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col5 .' LIKE "%'.$_POST["search"]["value"].'%" ';
            $sqlQuery .= ' OR '.$Col6 .' LIKE "%'.$_POST["search"]["value"].'%") ';         
        }

        if(!empty($_POST["order"])){
            $sqlQuery .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
        } else {
            $sqlQuery .= 'ORDER BY id DESC ';
        }

        if($_POST["length"] != -1){
            $sqlQuery .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
        }

        $stmt = $this->conn->prepare($sqlQuery);
        $stmt->execute();
        $result = $stmt->get_result();  

        $stmtTotal = $this->conn->prepare("SELECT * FROM ".$this->recordsTable);
        $stmtTotal->execute();
        $allResult = $stmtTotal->get_result();
        $allRecords = $allResult->num_rows;

        $displayRecords = $result->num_rows;
        $records = array();     
        while ($record = $result->fetch_assoc()) {              
            $rows = array();            

            //$rows[] = ucfirst($record[$Col1]);//original
            $rows[] = $record[$Col1];//manoj as order not working properly, test
            $rows[] = $record[$Col2];       
            $rows[] = $record[$Col3];   
            $rows[] = $record[$Col4];
            $rows[] = $record[$Col5];                   
            $rows[] = $record[$Col6];
            $rows[] = $record[$Col0];
            //$rows[] = '<button type="button" name="update" id="'.$record["id"].'" class="btn btn-warning btn-xs update">Update</button>'; //original
            //$rows[] = '<button type="button" name="delete" id="'.$record["id"].'" class="btn btn-danger btn-xs delete" >Delete</button>'; //Original
            $rows[] = '<button type="button" name="open" id="'.$record["id"].'" class="btn btn-warning btn-xs open" >Open</button>';
            $records[] = $rows;
        }

        $output = array(
            "draw"  =>  intval($_POST["draw"]),         
            "iTotalRecords" =>  $displayRecords,
            "iTotalDisplayRecords"  =>  $allRecords,
            "data"  =>  $records
        );

        echo json_encode($output);
    }