Close
<?php require_once '../../includes/connection.php' ; require_once '../../includes/utility_funcs.php' ; // connect to MySQL $conn = dbConnect ( 'read' ); // set default values $col = 'image_id' ; $dir = 'ASC' ; // create arrays of permitted values $columns = [ 'image_id' , 'filename' , 'caption' ]; $direction = [ 'ASC' , 'DESC' ]; // if the form has been submitted, use only expected values if (isset( $_GET [ 'column' ]) && in_array ( $_GET [ 'column' ], $columns )) { $col = $_GET [ 'column' ]; } if (isset( $_GET [ 'direction' ]) && in_array ( $_GET [ 'direction' ], $direction )) { $dir = $_GET [ 'direction' ]; } // prepare the SQL query using sanitized variables $sql = "SELECT * FROM php_a04_images ORDER BY $col $dir " ; try { // submit the query and capture the result $result = $conn -> query ( $sql ); $num_rows = $result -> num_rows ; } catch ( Exception $e ) { $error = $e -> getMessage (); } $tools = true ; include( "../../includes/header.php" ); ?> <main> <h2><?php echo $folder_name ; ?> <span><?php echo $file_name ; ?> </span></h2> <?php if (isset( $error )) { echo "<p class=\"error\"> $error </p>" ; } ?> <figure class="code"> <pre class="language-sql"><code><?= $sql ?> </code></pre> </figure> <form method="get"> <fieldset> <legend>Sort Table</legend> <ol> <li class="choose-sort"> <label for="column">Column:</label> <select name="column" id="column"> <option <?php if ( $col == 'image_id' ) echo 'selected' ; ?> >image_id</option> <option <?php if ( $col == 'filename' ) echo 'selected' ; ?> >filename</option> <option <?php if ( $col == 'caption' ) echo 'selected' ; ?> >caption</option> </select> <label for="direction">Direction:</label> <select name="direction" id="direction"> <option value="ASC" <?php if ( $dir == 'ASC' ) echo 'selected' ; ?> >Ascending</option> <option value="DESC" <?php if ( $dir == 'DESC' ) echo 'selected' ; ?> >Descending</option> </select> <input type="submit" name="change" id="change" value="Change Order"> </li> </ol> </fieldset> </form> <?php if (isset( $num_rows )) { ?> <table id="output-sql"> <tr> <th>ID</th> <th>filename</th> <th>caption</th> </tr> <?php while ( $row = $result -> fetch_assoc ()) { ?> <tr> <td><?= $row [ 'image_id' ]; ?> </td> <td><?= safe ( $row [ 'filename' ]); ?> </td> <td><?= safe ( $row [ 'caption' ]); ?> </td> </tr> <?php } ?> </table> <?php } ?> </main><?php # The side-bar section of the layout use custom path to load from a different folder. include( "../../includes/sidebar.php" ); # The footer section of the layout. include( "../../includes/footer.php" ); ?>