繁体   English   中英

如何在PHP,SQLSRV中的点击标题ASC / DESC上排序表

[英]How to sort table on click header ASC/DESC in PHP, SQLSRV

我们想通过头文件[JobTitle ]和[Status]SQLSRV/PHP表进行排序,我们尝试使用$_GET选项,但按照表代码进行管理无法正常工作。 我们无法将$_GET解决方案付诸实践。

<html>
<head>
<title>PHP & SQL Server (sqlsrv)</title>
</head>
</head>
<body>
<div class="table-title">
    <h3>Test table</h3>
<?php
   ini_set('display_errors', 1);
   error_reporting(~0);

    $serverName = "localhost";
    $userName = "username";
    $userPassword = "password";
    $dbName = "AdWorks";

   $connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);

   $conn = sqlsrv_connect( $serverName, $connectionInfo);

   if( $conn === false ) {
      die( print_r( sqlsrv_errors(), true));
   }

?>
<form action="list.php" method="POST">
<select name="RegSelect"><option> Choose </option>
<?php

$sql = "SELECT DISTINCT CountryRegionName FROM dbTest ORDER BY CountryRegionName";
$resultRg = sqlsrv_query($conn,$sql) or die("Couldn't execut query");
while ($data=sqlsrv_fetch_array($resultRg, SQLSRV_FETCH_ASSOC)){
    echo '<option value="'.$data['CountryRegionName'].'">';
    echo $data['CountryRegionName']; 
    echo "</option>";
}

if(empty($_POST['RegSelect'])){
    $_SESSION['tower'] = '';
} else {
    $stmt = "SELECT BusinessEntityID, FirstName, LastName, JobTitle, PhoneNumber, PhoneNumberType, EmailAddress, AddressLine1, City, PostalCode, CountryRegionName, Status FROM dbTest WHERE CountryRegionName = '".$_POST['RegSelect']."'";

    $qry = sqlsrv_query($conn, $stmt);
}
?>
<input type="submit" value="Select Tower">
</select></br></br>
</form>
<table align="center">
  <tr>
    <th width="91"> <div align="center">BusinessEntityID </div></th>
    <th width="98"> <div align="center">FirstName </div></th>
    <th width="198"> <div align="center">LastName </div></th>
    <th width="97"> <div align="center">JobTitle </div></th>
    <th width="59"> <div align="center">PhoneNumber </div></th>
    <th width="71"> <div align="center">PhoneNumberType </div></th>
    <th width="30"> <div align="center">EmailAddress </div></th>
    <th width="30"> <div align="center">AddressLine1 </div></th>
    <th width="30"> <div align="center">City </div></th>
    <th width="30"> <div align="center">PostalCode </div></th>
    <th width="30"> <div align="center">CountryRegionName </div></th>
    <th width="30"> <div align="center">Status </div></a></th>
    <th width="30"> <div align="center">Edit </div></th>
  </tr>
 <tbody class="rows">
<?php

while($result = sqlsrv_fetch_array($qry, SQLSRV_FETCH_ASSOC)){
    //print_r($result);
?>
  <tr>
    <td><div align="center"><?=$result["BusinessEntityID"];?></div></td>
    <td><div align="center"><?=$result["FirstName"];?></div></td>
    <td><?=$result["LastName"];?></td>
    <td align="right"><?=$result["JobTitle"];?></td>
    <td align="right"><?=$result["PhoneNumber"];?></td>
    <td align="right"><?=$result["PhoneNumberType"];?></td>
    <td align="right"><?=$result["EmailAddress"];?></td>
    <td align="right"><?=$result["AddressLine1"];?></td>
    <td align="right"><?=$result["City"];?></td>
    <td align="right"><?=$result["PostalCode"];?></td>
    <td align="right"><?=$result["CountryRegionName"];?></td>
    <td align="right"><?=$result["Status"];?></td>
    <td align="center"><a href="edit_bck.php?BusinessEntityID=<?php echo $result["BusinessEntityID"];?>">Edit </a></td>
  </tr>
<?php
}
?>
</table>
<?php
sqlsrv_close($conn);
?>
</body>
</html>

在了解了它真正易于使用的工作原理之后,我使用了JQuery Tablesorter找到了解决方案。

<html>
<head>
<title>SQL Server (sqlsrv)</title>
<link rel="stylesheet" href="jquery/themes/blue/style.css" type="text/css" media="print, projection, screen" />
<script type="text/javascript" src="jquery/jquery-3.2.1.js"></script> 
<script type="text/javascript" src="jquery/jquery.tablesorter.js"></script>
<script type="text/javascript" src="jquery/jquery.tablesorter.widgets.js"></script>

<script type="text/javascript">
$(document).ready(function() 
    { 
        $("#myTable").tablesorter(); 
    } 
);
</script>
</head>
<body>
<div class="table-title">
    <h3>Test Table</h3>
<?php
   ini_set('display_errors', 1);
   error_reporting(~0);

   include('connect.php');
?> 
<form action="list.php" method="POST">
<select name="RegSelect"><option> Choose </option>
<?php
    $query1 = "SELECT DISTINCT CountryRegionName FROM someDB ORDER BY CountryRegionName";
    $stmt1 = $conn->query( $query1 );
    while ($data = $stmt1->fetch(PDO::FETCH_ASSOC)){
        echo '<option value="'.$data['CountryRegionName'].'">';
        echo $data['CountryRegionName']; 
        echo "</option>";
    }
if(empty($_POST['RegSelect'])){
    $_SESSION['tower'] = '';
} else {
    $query2 = "SELECT BusinessEntityID, FirstName, LastName, JobTitle, PhoneNumber, PhoneNumberType, EmailAddress, AddressLine1, City, PostalCode, CountryRegionName, Status FROM someDB WHERE CountryRegionName = '".$_POST['RegSelect']."' ORDER BY JobTitle ASC";

    $stmt2 = $conn->query( $query2 );
}
?>
<input type="submit" value="Select Tower">
</select></br></br>
</form>
<table id="myTable" class="tablesorter" align="center">
<thead>
  <tr>
    <th width="91">Entity ID</th>
    <th width="98">Fist Name</th>
    <th width="198">Last Name</th>
    <th width="97">Job Title</th>
    <th width="59">Phone Number</th>
    <th width="71">Phone Type</th>
    <th width="30">Email Address</th>
    <th width="30">Address</th>
    <th width="30">City</th>
    <th width="30">Postal Code</th>
    <th width="30">Region Name</th>
    <th width="30">Status</th>
    <th width="30">Edit</th>
  </tr>
<thead>
<tbody>
<?php
    if(empty($stmt2)){
        echo '';
    }else{
        while ($result = $stmt2->fetch(PDO::FETCH_ASSOC)){
?>
    <tr>
        <td><?=$result["BusinessEntityID"];?></td>
        <td><?=$result["FirstName"];?></td>
        <td><?=$result["LastName"];?></td>
        <td><?=$result["JobTitle"];?></td>
        <td><?=$result["PhoneNumber"];?></td>
        <td><?=$result["PhoneNumberType"];?></td>
        <td><?=$result["EmailAddress"];?></td>
        <td><?=$result["AddressLine1"];?></td>
        <td><?=$result["City"];?></td>
        <td><?=$result["PostalCode"];?></td>
        <td><?=$result["CountryRegionName"];?></td>
        <td><?=$result["Status"];?></td>
        <td><a href="edit_bck.php?BusinessEntityID=<?php echo $result["BusinessEntityID"];?>">Edit </a></td>
    </tr>
<?php
        }
    }
?>
</tbody>
</table>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM