I need my computer game to take all kills and deaths from a user's total and then make it so the kills are divided by the deaths, then that total is put at the end. This ratio is known as their kill-death ratio, or "KDR".
<?php
// Create connection
$con=mysqli_connect("ipaddress","user","password","minecraft");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT *, `kills`/`deaths` as `KDR` FROM war_kills ``ORDER BY kills DESC");
echo "<table border='1'>
<tr>
<th>Player</th>
<th>Kills</th>
<th>Deaths</th>
<th>KDR</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['KDR'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
We have this up so far: http://gexgaming.com/warstats/index.php
Change the query from
SELECT player, kills, deaths, KDR FROM war_kills ORDER BY kills DESC`
to
SELECT player, kills, deaths, kills / deaths as KDR FROM war_kills ORDER BY kills DESC`
Either modify the SQL query as suggested or do it within the PHP loop:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . ($row['kills'] / $row['deaths']) . "</td>"; // <--- this one
echo "</tr>";
}
It looks like you need to group your results based on the player name. Also, you need to account for when deaths = 0. Try changing your query to -
SELECT
player,
SUM(kills) as kills,
SUM(deaths) as deaths,
CASE WHEN SUM(deaths) = 0 THEN SUM(kills)
ELSE SUM(kills)/SUM(deaths)
END as `KDR`
FROM war_kills
GROUP BY player
ORDER BY kills DESC
The other answers I read don't account for the fact that someone might have zero deaths. If they do, you'll get a divide by zero error.
inside your while loop,
if ($row['deaths'] == 0)
$kdr = '-';
else
$kdr = $row['kills']/$row['deaths'];
then change $row['KDR'] to $kdr
Related
I have this SQL query: I need some sort of conditional WHERE clause if possible to use the WHERE clause on EVERYTHING except this Row Below -> $row['value_count_deaths'].
I do not want this one to be filtered and to show the whole number before being filtered.
Is this possible within the same query? If so ... how?
IMG: I want to accumulate the death number after their name, must be outside WHERE clause
Focus of Code:
echo "<td style='text-align: center'>" . $row['pe_DataPlayers_lastname'] . " " . $row['value_count_deaths'] . "</td>";
All overall coded query statement:
echo "<h3> All Pilot Statistics (Realistic/Hardcore) ... only populate stats after last pilot death for each pilot</h3>";
if ($result = $mysqli->query("SELECT a.`pe_LogEvent_pilotname`,
COUNT(a.`pe_LogEvent_deaths`) AS value_count_deaths,
COUNT(a.`pe_LogEvent_kills_planes`) AS value_count_kills_planes,
COUNT(a.`pe_LogEvent_kills_helicopters`) AS value_count_kills_helicopters,
COUNT(a.`pe_LogEvent_kills_armor`) AS value_count_kills_armor,
b.`pe_DataPlayers_lastname`,
b.`pe_DataPlayers_updated`,
b.`pe_DataPlayers_id`,
c.`pe_LastPilotDeath_pilotname`,
c.`pe_LastPilotDeath_datetime`
FROM `pe_LogEvent` AS a
INNER JOIN `pe_DataPlayers` AS b
ON a.pe_LogEvent_pilotname = b.pe_DataPlayers_lastname
LEFT JOIN `pe_LastPilotDeath` AS c
ON b.pe_DataPlayers_lastname=c.pe_LastPilotDeath_pilotname
WHERE a.`pe_LogEvent_datetime` > c.`pe_LastPilotDeath_datetime`
GROUP BY b.`pe_DataPlayers_lastname`
ORDER BY b.`pe_DataPlayers_updated` DESC")) {
echo "<table>";
echo "<table class='table_stats'>";
echo "<tr class='table_header'><th>Pilot</th><th>Last Flight Log</th><th>A2A Kills</th><th>A2G Kills</th><th>Sorties</th><th>Landing%</th><th>Deaths</th><th>Ejections</th><th>Crashes</th><th>PVP Kills</th><th>Team Kills</th><th>Planes</th><th>Helos</th><th>Armor</th><th>Light Vehicles</th><th>UnArmored</th><th>Air Defense</th><th>Artillery</th><th>Infantry</th><th>Ships</th><th>Structures</th></tr>"; // First Code
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='text-align: center'>" . $row['pe_DataPlayers_lastname'] . " " . $row['value_count_deaths'] . "</td>";
echo "<td style='text-align: center'>" .
$row['value_count_deaths'] . "</td>";
echo "<td style='text-align: center'>" . $row['value_count_ejections'] . "</td>";
echo "<td style='text-align: center'>" . $row['value_count_crashes'] . "</td>";
//echo "<td style='text-align: center'>" . $row['value_sum_pvp'] . "</td>";
echo "</tr>";
}
echo "</table>";
$result->close();
}
A typical approach would be to use a subquery to create a flag.
Your code looks like MySQL, so this uses MySQL syntax conventions:
SELECT SELECT pe_LogEvent_pilotname, pe_LogEvent_friendly_fire_killer,
COUNT(*) as total,
SUM(pe_LogEvent_crashes IS NOT NULL AND flag) AS value_count_crashes,
. . .
FROM (SELECT le.*,
. . . , -- other columns you want
(le.pe_LogEvent_datetime > lpd.pe_LastPilotDeath_datetime) as flag
FROM pe_LogEvent le JOIN
pe_DataPlayers dp
ON le.pe_LogEvent_pilotname = dp.pe_DataPlayers_lastname LEFT JOIN
pe_LastPilotDeath lpd
ON dp.pe_DataPlayers_lastname = lpd.pe_LastPilotDeath_pilotname
) x
GROUP BY . . .
This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
Closed 6 years ago.
The code below is returning a table with data from mysql, but what I'm missing in the output is a generated column with a rank based on a column's data (wilks in this example). I've looked for examples and answers to similar questions, but I can't get it to work.
In short: I need an extra column next to the ID column where a rank beginning from 1 is displayed, based on the data from the Wilks column.
Thank you so much!
<?php
$con=mysqli_connect("localhost", "user", "password", "dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `Mannen` ORDER BY `Wilks` DESC");
echo "<table border='1'>
<tr>
<th>#</th>
<th>Naam</th>
<th>Lichaamsgewicht</th>
<th>Vereniging</th>
<th>Squat</th>
<th>Bench</th>
<th>Deadlift</th>
<th>Totaal</th>
<th>Wilks</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Naam'] . "</td>";
echo "<td>" . $row['Lichaamsgewicht'] . "</td>";
echo "<td>" . $row['Vereniging'] . "</td>";
echo "<td>" . $row['Squat'] . "</td>";
echo "<td>" . $row['Bench'] . "</td>";
echo "<td>" . $row['Deadlift'] . "</td>";
echo "<td>" . $row['Totaal'] . "</td>";
echo "<td>" . $row['Wilks'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
First, if you just want an enumerated value, you can get that using PHP.
But, it is easy enough in MySQL, using variables:
SELECT m.*, (#rn := #rn + 1) as rank
FROM Mannen m CROSS JOIN
(SELECT #rn := 0) params
ORDER BY Lichaamsgewicht DESC;
Technically, this implements the related function of row_number() rather than rank(). Your question is unclear on what you mean by "rank".
I an trying to display records in table with top 10 enteries with specific color
My Query just for Ref.
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");
This code below displays a simple table, I need to display table With TOP 10 entries be different in colour[blue].. rest remains the same [white background].
i.e Top 10 can be in blue color, and rest in white color.
Below is the code I am using to display records.
<?php
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr> ";
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
}
echo "</tr> " ;
?>
I have this table structure as sample. That I want to use, with this code. The table need to be the same , but i am not to find the logic , how to build the table
with this structure
<table>
<thead><tr><td colspan="2"><center>Prizes</center></td></tr><tr>
<th>Position</center></th><th><center>Prize</center></th></tr></thead>
<tbody><tr><td>1st</td><td>0.0$</td></tr>
<tr class="alt"><td>2nd</td><td>0.0$</td></tr>
<tr><td>3rd</td><td>0.0$</td></tr>
<tr class="alt"><td>4th</td><td>0.0$</td></tr>
<tr><td>5th</td><td>0.0$</td></tr>
</tbody>
</table>
Remove Limit 10 For Fetch All Data:
$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM
`tablename` GROUP BY `userid` ORDER BY total DESC");
php:
<?php
$i=1;
while($row = mysql_fetch_array($sqlsum))
{
echo "<tr ".(($i <= 10) ? "bgcolor='blue'" : '')'."> ";
// Apply attrinute bgcolor for backgroung color
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr>";
$i++;
}
?>
Set an indicator variable to point it.
<?php
$rowNumber = 0;
while($row = mysql_fetch_array($sqlsum))
{
if ($rowNumber < 10)
{
echo "<tr class=\"alt\"> ";
}
else
{
echo "<tr> ";
}
echo "<td>" .$row[userid] . "</td>";
echo "<td>" .$row[total] . "</td>";
echo "</tr> " ;
$rowNumber++;
}
?>
im busy with a script to load mysql data in php variablen.
My database is an gps tracking database with different users and gps coordinates in a table.
The problem is. each gps update has its own ID... for an example if userA posting his gps location that will be id 1, but when user B also post their location 5 minutes later this is id 2, when userA post their location again 10 minutes later, that will be id 3.
Now is the question. How can i sort the newest gps locations for each user.
This is my table structure and my code:
//Tabel selecteren en wat opties defineren
$posities = mysqli_query($raw,"SELECT * FROM positions INNER JOIN devices ON positions.device_id = devices.id ORDER BY positions.id DESC");
//Aantal gebruikers tellen in de devices tabel aanroepen via $aantalgb
$query = mysqli_query($raw,"SELECT COUNT(id) FROM devices");
$row = mysqli_fetch_row($query);
$aantalgb = $row[0];
mysqli_free_result($query);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Naam</th>
<th>Tijd</th>
<th>Lat</th>
<th>Lon</th>
<th>Snelheid</th>
</tr>";
while($pos = mysqli_fetch_array($posities)) {
echo "<tr>";
echo "<td>" . $pos['device_id'] . "</td>";
echo "<td>" . $pos['name'] . "</td>";
echo "<td>" . $pos['time'] . "</td>";
echo "<td>" . $pos['latitude'] . "</td>";
echo "<td>" . $pos['longitude'] . "</td>";
echo "<td>" . $pos['speed'] . "</td>";
echo "</tr>";
}
echo "</table>";
Thank you!
I am working on a formula where I am adding multiple values in a row and column depending on the values of other cells. For example I have the following working code:
$result = mysql_query("SELECT School,SUM(Wins + Losses) as total FROM tennis GROUP BY School");
echo "<table border='1'>
<tr>
<th>School</th>
<th>Score</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['School'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "</tr>";
}
However I want to add more columns that are also sums of other rows/columns and don't know how to do this while still keeping everything Grouped by the column 'School'. What I essentially want is the following, but the code is incorrect:
$result = mysql_query("SELECT School,SUM(Wins + Losses) as 1sttotal FROM tennis WHERE Event='1st Singles', SUM(Wins + Losses) as 2ndtotal FROM tennis WHERE Event='2nd Singles' GROUP BY School");
echo "<table border='1'>
<tr>
<th>School</th>
<th>1st Singles</th>
<th>2nd Singles</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['School'] . "</td>";
echo "<td>" . $row['1sttotal'] . "</td>";
echo "<td>" . $row['2ndtotal'] . "</td>";
echo "</tr>";
}
I'm new to PHP so I'm not sure the correct/optimal way to go about setting this up. Thanks
this is the query you should use to get the 2 totals:
SELECT
School,
SUM(IF(Event='1st Singles', Wins + Losses, 0)) as 1sttotal,
SUM(IF(Event='2nd Singles', Wins + Losses, 0)) as 2ndtotal
FROM tennis
GROUP BY School;
See how it adds according to the event columns? The trick is to pass a WHERE filter within the SELECT clause through conditional execution (IF)
Other possibility using CASE WHEN:
SELECT
School,
SUM(
CASE
WHEN Event='1st Singles' THEN Wins + Losses
ELSE 0
END
) as 1sttotal,
SUM(
CASE
WHEN Event='2nd Singles' THEN Wins + Losses
ELSE 0
END
) as 2ndtotal
FROM tennis
GROUP BY School;