0

I'd like to be able to change the SQL query to another with the press of a button, I need about 8 buttons and 8 separate queries. If possible, I'd like to be able to do this without refreshing the page, just updating the table.

This is my current code:

<?php
                    include 'table/modes.php';

                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);
                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    } 

                    $sql = "SELECT m.MapName, SEC_TO_TIME(TRUNCATE(t.Time,3)) AS Time, p.User FROM times t INNER JOIN maps m ON t.MapID = m.MapID INNER JOIN players p ON p.PlayerID = t.PlayerID INNER JOIN (SELECT t.MapId, MIN(t.time) as time FROM times t WHERE t.style = 0 and t.type = 0 GROUP BY t.MapId ) tmin ON tmin.MapId = t.MapId and tmin.time = t.time";
                    $result = $conn->query($sql);


                    if ($result->num_rows > 0) {
                        echo "<table class='table table-striped table-fixedheader sortable'><thead><tr><th data-defaultsort='asc'>Map</th><th>Time</th><th>Player</th></tr></thead><tbody style='height:300px' class='searchable'>";
                        // output data of each row
                        while($row = $result->fetch_assoc()) {
                            echo "<tr><td>".$row["MapName"]."</td><td>".$row["Time"]."</td><td>".$row["User"]."</td></tr>";
                        }
                        echo "</tbody></table>";
                    } else {
                        echo "0 results";
                    }
                    $conn->close();
?>

Currently that fetches data for a table and displays it. I'd like buttons that execute different queries this and update the table without refreshing the page (not sure if possible).

How can this be achieved?

The main values I am looking to change in each query is this WHERE t.style = 0 and t.type = 0 too different numbers.

1 Answer 1

1

You can simply create a hidden input field to hold the type you want to change and when every you click the button you can change the value of the hidden field. During submision of the form the hidden input will go to the server with the value.

Sign up to request clarification or add additional context in comments.

2 Comments

I've been looking around but can not find/work out a way to do this or integrate it into my script. I'm not great when it comes to web.
Your issue is not with server script, it is front end issue, you can use JavaScript to handle the button clicks and according to the clicks you can send different request to the server , so the server can execute different sql queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.