0

I am currently having my code sort SQL results by date by default, but I want to give users the ability to order by other values as well. I have attempted using a button press to save a PHP variable and to call a query whenever it is pressed but it never actually calls the query. I am trying to reorder using the buttons in the "sidenav" bar.

<!DOCTYPE html>
<html>
<head>
    <title>Main Board</title>
    <link rel="stylesheet" type="text/css" href="http://www.project485.ca/styles.css">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>


<div class="navbar">
    <a href="index.php" class="active">Home</a>
    <a href="">Create Event</a>
    <a href="form.php" style="float:right">Login</a>
    <a href="" style="float:right">Admin Panel</a>
</div>

<div class="sidenav">
    <h3 style="color:white; margin-left:20px; font-size:30px">Order By:</h3>
    <div class="snButt">
        <a><button class="snButt" onclick="<?php $sql = "SELECT * FROM Confirmed ORDER BY DATE"; $result = mysqli_query($conn,$sql);?>">Date</button></a>
        <a><button class="snButt" onclick="<?php $sql = "SELECT * FROM Confirmed ORDER BY DEPARTMENT";?> console.log("dep clicked")">Department</button></a>
        <a><button class="snButt" onclick="<?php $sort = 'FEE' ?>">Cost</button></a>
    </div>
</div>

<div class="cards">
    <?php
    $dbhost = 'localhost';
    $dbuser = 'p485';
    $dbpass = 'project485';
    $dbname = '485_main';

    $sql = "SELECT * FROM Confirmed ORDER BY FEE DESC";

    $conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
    if(! $conn ) {
        die('Could not connect: ' . mysqli_connect_error());
    }
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_assoc($result)) {
    ?>
        <div class="w3-card" style="width:30%;">
            <header class="w3-container">
                <h1><?php echo $row['TITLE']?></h1>
            </header>
            <div class="w3-container">
                <p>Date: <?php echo $row['DATE']?></p>
                <p>Description: <?php echo $row['DESCRIPTION']?></p>
                <p>Location: <?php echo $row['LOCATION']?></p>
                <p>Department: <?php echo $row['DEPARTMENT']?></p>
                <p>Fee: <?php echo $row['FEE']?></p>
            </div>
        </div>
    <?php
    }
    ?>
</div>    



</body>
</html> 
Jake
  • 3
  • 3
  • 1
    PHP is ran before javascript, use query params to pass the button clicks then have your back end read those to decide how to sort. – Lulceltech Feb 06 '20 at 19:58
  • Btw if the resultset is small, you may want to do the reordering in javascript on the client side. There are several sortable js-based tables available for all sorts of frameworks. – Shadow Feb 06 '20 at 21:00

0 Answers0