With my code I can sort all columns individually but I do have an issue with data not being sorted alphabetically / chronologically apart from the 1st column. Please see below what is displayed and then what I need to see.
What is currently displayed (Payment type and Date not sorted):
| Account | Payment type | Date | Amount |
|:--------|:------------:|:----------:|-------:|
| A001 | Rent | 2021-06-01 | 150.00 |
| A001 | Deposit | 2021-04-15 | 200.00 |
| A001 | Rent | 2021-05-02 | 150.00 |
| A002 | Deposit | 2021-06-20 | 220.00 |
| A003 | Rent | 2021-06-02 | 250.00 |
| A003 | Deposit | 2021-05-25 | 300.00 |
What I want to get displayed:
| Account | Payment type | Date | Amount |
|:--------|:------------:|:----------:|-------:|
| A001 | Deposit | 2021-04-15 | 200.00 |
| A001 | Rent | 2021-05-02 | 150.00 |
| A001 | Rent | 2021-06-01 | 150.00 |
| A002 | Deposit | 2021-06-20 | 220.00 |
| A003 | Deposit | 2021-05-25 | 300.00 |
| A003 | Rent | 2021-06-02 | 250.00 |
So in short, I want to sort and display data 1stly by the Account column (which displays correctly) and then by the Date column.
My code currently (and yes I know it's open to sql injection and I am working on that ;-) ):
$orderBy = !empty($_GET["orderby"]) ? $_GET["orderby"] : "contracts_account_nr";
$order = !empty($_GET["order"]) ? $_GET["order"] : "asc";
$sql = "SELECT contract.contracts_account_nr, payment.rental_payment_type, payment.rental_payment_amount, payment.rental_payment_date FROM contracts contract RIGHT JOIN rental_payments payment ON contract.contracts_id = payment.contracts_id ORDER BY " . $orderBy . " " . $order;
$result = mysqli_query($con, $sql);
$contractOrder = "asc";
$typeOrder = "asc";
$dateOrder = "asc";
if($orderBy == "contract.contracts_account_nr" && $order == "asc") {
$contractOrder = "desc";
}
if($orderBy == "payment.rental_payment_type" && $order == "asc") {
$typeOrder = "desc";
}
if($orderBy == "payment.rental_payment_date" && $order == "asc") {
$dateOrder = "desc";
}
Your help will be much appreciated!