Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- We will assume that our database has a table, which looks as follows:
- | ID | Name | Age | Address |
- |¯¯¯¯|¯¯¯¯¯¯|¯¯¯¯¯|¯¯¯¯¯¯¯¯¯|
- | .. | .. | ... | ... |
- -->
- <?php
- $servername = ........;
- $username = ........;
- $password = ........;
- $dbname = ........;
- $tablename = ........;
- $tablerows = "";
- $columnset = array(
- 0 => "ID",
- 1 => "Name",
- 2 => "Age",
- 3 => "Address",
- );
- $columns = explode(',', $_GET["col"]);
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error)
- die("Connection failed: ".$conn->connect_error);
- else
- {
- $sql = "SELECT $columns FROM $tablename";
- $result = $conn->query($sql);
- if ($result->num_rows > 0)
- while($row = $result->fetch_assoc())
- $tablerows = $tablerows."<tr><td>".get($row, "ID").
- "</td><td>".get($row, "Name").
- "</td><td>".get($row, "Age").
- "</td><td>".get($row, "Address").
- "</td></tr>\n";
- }
- $conn->close();
- function get($arr, $key)
- {
- if (isset($arr[$key]))
- return $arr[$key];
- else
- return "";
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title></title>
- </head>
- <body>
- <table>
- <tr>
- <?php
- foreach ($columnset as $key => $value)
- {
- $copy = $columns;
- if (in_array($value, $columns))
- unset($copy);
- else
- array_push($copy, $value);
- $selstr = implode(",", $copy);
- echo "<th><a href=\"#?col=$selstr\">$value</a></th>";
- }
- ?>
- </tr>
- <?php
- echo $tablerows;
- ?>
- </table>
- </body>
- </html>
Add Comment
Please, Sign In to add comment