Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--PHP BLOCK-->
- <?php
- // If we are searching the database
- if(isset($_POST['search']))
- {
- // Get the value from the search form
- $valueToSearch = $_POST['valueToSearch'];
- // search in all table columns using concat mysql function
- $query = "SELECT * FROM `VAR_table_name` WHERE CONCAT(`VAR_id`, `VAR_column`, `VAR_column`, `VAR_column`) LIKE '%".$valueToSearch."%'";
- $search_result = filterTable($query);
- }
- // we are not searching, get everything from the database
- else {
- $query = "SELECT * FROM `VAR_table_name`";
- $search_result = filterTable($query);
- }
- // function to connect and execute the query
- function filterTable($query)
- {
- // 1. Create a database connection
- $dbhost = "ecsmysql";
- $dbuser = "cs332u14"; // where ?? is your id
- $dbpass = "oodokahc"; // replace with your password
- $dbname = "cs332u14";
- $dbconnection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
- // 2. Check if the connection is ok
- if (mysqli_connect_errno()) {
- die("Database connection failed: " .
- mysqli_connect_error() . " (" > mysqli_connect_errno() . ")" );
- }
- // 3. Execute the query
- $filter_Result = mysqli_query($dbconnection, $query);
- // Check if there is a query error
- if (!$filter_Result) {
- die("Database query failed.");
- }
- // Return the query result
- return $filter_Result;
- }
- ?>
- <!--END OF PHP BLOCK-->
- <!--HTML BLOCK-->
- <!DOCTYPE html>
- <html>
- <head>
- <title>TABLE DATA SEARCH</title>
- <style>
- table,tr,th,td
- {
- border: 1px solid black;
- }
- </style>
- </head>
- <body>
- <!--Search Form-->
- <form action="table_data_filter.php" method="post">
- <!--Edit text box-->
- <input type="text" name="valueToSearch" placeholder="Value To Search"><br><br>
- <!--Sumbit Button-->
- <input type="submit" name="search" value="Filter"><br><br>
- <!--Data Table-->
- <table>
- <tr>
- <th>Id</th>
- <th>VAR_column</th>
- <th>VAR_column</th>
- <th>VAR_column</th>
- </tr>
- <!-- Populate table from mysql database -->
- <?php while($row = mysqli_fetch_array($search_result)):?>
- <tr>
- <td><?php echo $row['id'];?></td>
- <td><?php echo $row['VAR_column'];?></td>
- <td><?php echo $row['VAR_column'];?></td>
- <td><?php echo $row['VAR_column'];?></td>
- </tr>
- <?php endwhile;?>
- </table>
- </form>
- </body>
- <!--Cleanup-->
- <?php
- // 4. Release returned result
- mysqli_free_result($search_result);
- ?>
- <?php
- // 5. Close the database connection
- mysqli_close($dbconnection);
- ?>
- </html>
- <!--END OF HTML BLOCK-->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement