Guest User

Untitled

a guest
Nov 15th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <!--
  2. We will assume that our database has a table, which looks as follows:
  3.  
  4. | ID | Name | Age | Address |
  5. |¯¯¯¯|¯¯¯¯¯¯|¯¯¯¯¯|¯¯¯¯¯¯¯¯¯|
  6. | .. | .. | ... | ... |
  7. -->
  8.  
  9. <?php
  10.  
  11. $servername = ........;
  12. $username = ........;
  13. $password = ........;
  14. $dbname = ........;
  15. $tablename = ........;
  16. $tablerows = "";
  17.  
  18. $columnset = array(
  19. 0 => "ID",
  20. 1 => "Name",
  21. 2 => "Age",
  22. 3 => "Address",
  23. );
  24. $columns = explode(',', $_GET["col"]);
  25. $conn = new mysqli($servername, $username, $password, $dbname);
  26.  
  27. if ($conn->connect_error)
  28. die("Connection failed: ".$conn->connect_error);
  29. else
  30. {
  31. $sql = "SELECT $columns FROM $tablename";
  32. $result = $conn->query($sql);
  33.  
  34. if ($result->num_rows > 0)
  35. while($row = $result->fetch_assoc())
  36. $tablerows = $tablerows."<tr><td>".get($row, "ID").
  37. "</td><td>".get($row, "Name").
  38. "</td><td>".get($row, "Age").
  39. "</td><td>".get($row, "Address").
  40. "</td></tr>\n";
  41. }
  42.  
  43. $conn->close();
  44.  
  45.  
  46. function get($arr, $key)
  47. {
  48. if (isset($arr[$key]))
  49. return $arr[$key];
  50. else
  51. return "";
  52. }
  53. ?>
  54. <!DOCTYPE html>
  55. <html lang="en">
  56. <head>
  57. <title></title>
  58. </head>
  59. <body>
  60. <table>
  61. <tr>
  62. <?php
  63. foreach ($columnset as $key => $value)
  64. {
  65. $copy = $columns;
  66.  
  67. if (in_array($value, $columns))
  68. unset($copy);
  69. else
  70. array_push($copy, $value);
  71.  
  72. $selstr = implode(",", $copy);
  73.  
  74. echo "<th><a href=\"#?col=$selstr\">$value</a></th>";
  75. }
  76. ?>
  77. </tr>
  78. <?php
  79. echo $tablerows;
  80. ?>
  81. </table>
  82. </body>
  83. </html>
Add Comment
Please, Sign In to add comment