Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>PHP & SQL Server (sqlsrv)</title>
  4. </head>
  5. </head>
  6. <body>
  7. <div class="table-title">
  8. <h3>Test table</h3>
  9. <?php
  10. ini_set('display_errors', 1);
  11. error_reporting(~0);
  12.  
  13. $serverName = "localhost";
  14. $userName = "username";
  15. $userPassword = "password";
  16. $dbName = "AdWorks";
  17.  
  18. $connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
  19.  
  20. $conn = sqlsrv_connect( $serverName, $connectionInfo);
  21.  
  22. if( $conn === false ) {
  23. die( print_r( sqlsrv_errors(), true));
  24. }
  25.  
  26. ?>
  27. <form action="list.php" method="POST">
  28. <select name="RegSelect"><option> Choose </option>
  29. <?php
  30.  
  31. $sql = "SELECT DISTINCT CountryRegionName FROM dbTest ORDER BY CountryRegionName";
  32. $resultRg = sqlsrv_query($conn,$sql) or die("Couldn't execut query");
  33. while ($data=sqlsrv_fetch_array($resultRg, SQLSRV_FETCH_ASSOC)){
  34. echo '<option value="'.$data['CountryRegionName'].'">';
  35. echo $data['CountryRegionName'];
  36. echo "</option>";
  37. }
  38.  
  39. if(empty($_POST['RegSelect'])){
  40. $_SESSION['tower'] = '';
  41. } else {
  42. $stmt = "SELECT BusinessEntityID, FirstName, LastName, JobTitle, PhoneNumber, PhoneNumberType, EmailAddress, AddressLine1, City, PostalCode, CountryRegionName, Status FROM dbTest WHERE CountryRegionName = '".$_POST['RegSelect']."'";
  43.  
  44. $qry = sqlsrv_query($conn, $stmt);
  45. }
  46. ?>
  47. <input type="submit" value="Select Tower">
  48. </select></br></br>
  49. </form>
  50. <table align="center">
  51. <tr>
  52. <th width="91"> <div align="center">BusinessEntityID </div></th>
  53. <th width="98"> <div align="center">FirstName </div></th>
  54. <th width="198"> <div align="center">LastName </div></th>
  55. <th width="97"> <div align="center">JobTitle </div></th>
  56. <th width="59"> <div align="center">PhoneNumber </div></th>
  57. <th width="71"> <div align="center">PhoneNumberType </div></th>
  58. <th width="30"> <div align="center">EmailAddress </div></th>
  59. <th width="30"> <div align="center">AddressLine1 </div></th>
  60. <th width="30"> <div align="center">City </div></th>
  61. <th width="30"> <div align="center">PostalCode </div></th>
  62. <th width="30"> <div align="center">CountryRegionName </div></th>
  63. <th width="30"> <div align="center">Status </div></a></th>
  64. <th width="30"> <div align="center">Edit </div></th>
  65. </tr>
  66. <tbody class="rows">
  67. <?php
  68.  
  69. while($result = sqlsrv_fetch_array($qry, SQLSRV_FETCH_ASSOC)){
  70. //print_r($result);
  71. ?>
  72. <tr>
  73. <td><div align="center"><?=$result["BusinessEntityID"];?></div></td>
  74. <td><div align="center"><?=$result["FirstName"];?></div></td>
  75. <td><?=$result["LastName"];?></td>
  76. <td align="right"><?=$result["JobTitle"];?></td>
  77. <td align="right"><?=$result["PhoneNumber"];?></td>
  78. <td align="right"><?=$result["PhoneNumberType"];?></td>
  79. <td align="right"><?=$result["EmailAddress"];?></td>
  80. <td align="right"><?=$result["AddressLine1"];?></td>
  81. <td align="right"><?=$result["City"];?></td>
  82. <td align="right"><?=$result["PostalCode"];?></td>
  83. <td align="right"><?=$result["CountryRegionName"];?></td>
  84. <td align="right"><?=$result["Status"];?></td>
  85. <td align="center"><a href="edit_bck.php?BusinessEntityID=<?php echo $result["BusinessEntityID"];?>">Edit </a></td>
  86. </tr>
  87. <?php
  88. }
  89. ?>
  90. </table>
  91. <?php
  92. sqlsrv_close($conn);
  93. ?>
  94. </body>
  95. </html>
  96.  
  97. <th onclick="sortBy('JobTitle', 'ASC')" width="91"> <div align="center">BusinessEntityID</div></th>
  98. <th onclick="sortBy('Status', 'DESC')" width="98"> <div align="center">FirstName</div></th>
  99.  
  100. <script>
  101. function sortBy(columnName, order) {
  102. window.location.href = window.location.href.split('?')[0] + "?column=" + columnName + "&order=" + order;
  103. }
  104.  
  105. //get the column name & sort type
  106. $columnName = isset($_REQUEST['column']) ? $_REQUEST['column'] : '';
  107. $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : '';
  108.  
  109. if (empty($_REQUEST['RegSelect'])) {
  110. $_SESSION['tower'] = '';
  111. } else {
  112. $stmt = "SELECT BusinessEntityID, FirstName, LastName, JobTitle, PhoneNumber, PhoneNumberType, EmailAddress, AddressLine1, City, PostalCode, CountryRegionName, Status FROM dbTest WHERE CountryRegionName = '" . $_REQUEST['RegSelect'] . "'";
  113.  
  114. //append order by clause with type
  115. if (!empty($columnName)) {
  116. $stmt .= " ORDER BY $columnName $order";
  117. }
  118.  
  119. $qry = sqlsrv_query($conn, $stmt);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement