Sveiki, ir neliela problēma ar kodu. Īsti nevaru iebraukt kur ir tā problēma. Varbūt kādam ir idejas kā to savādāk uzrakstīt.
Jo mana ideja ir uztaisīt lai izskatās šādi:
Datubāzes dizains:
CREATE TABLE table_name(
ID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255) NOT NULL,
Points INT NOT NULL,
Points2 INT NOT NULL
);
INSERT INTO table_name (Name, Points, Points2)
VALUES
('Bob Johnson', 75, 68),
('John Doe', 80, 72),
('Emily Davis', 85, 67),
('Jane Smith', 90, 82);
PHP kods:
<?php
// Connect to the database
$conn = mysqli_connect("hostname", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Select data from the table, add consecutive number and sort by Total Points in ascending order
$sql = "SET @count = 0;
SELECT @count := @count + (TotalPoints = @prev) as Place,
@prev := TotalPoints as prev,
Name, Points, Points2, Points + Points2 AS TotalPoints
FROM table_name
ORDER BY TotalPoints, Name ASC;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// Output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Place: " . $row["Place"]. " - Name: " . $row["Name"]. " - Points: " . $row["Points"]. " - Points2: " . $row["Points2"]. " - TotalPoints: " . $row["TotalPoints"]. "<br>";
}
} else {
echo "0 results";
}
// Close the connection
mysqli_close($conn);
?>