K/D Ratio with SQL and PHP - php

So I'm trying to make a K/D ratio with some data from a database using SQL.
The main problem is that the tables are so strange and I'm trying to display it as an HTML table.
There's a table called stats_player_kill (when player kills another)
Here's the content of the table:
So if I want to get the kills, I count the killer column with the same id.
Here's how I get number of kills of each player and display it as an HTML table:
<table class="tablitas" id="kills">
<thead>
<tr>
<th data-column-id="name">Player</th>
<th data-column-id="killer" data-type="numeric">Kills</th>
</tr>
</thead>
<?php
$sql = "select p.name as name, count(*) as amountOfObjects
from stats_player_kill b
join stats_player p on p.id = b.victim
group by b.victim
order by count(*) desc
limit 15";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["amountOfObjects"]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</td>";
} else {
echo "0 results";
}
?>
So I need help making the math to divide number of kills by the total deaths.
Sorry for my English and thanks to everyone! :D

Related

Join issue for two tables

I can not get Select to work properly. I'm fairly familiar putting img src and have used loops before but the select is stumping me.
I have two tables. TableA (Aid, Aname, Adescription) TableB (Bid, Bimagefile, Aid). TableB holds multiple images which belong to TableA Aid. There may be 5 images for one Aid and 3 images for another Aid.
How can I write the Select and Join so I only see images related to 1 TableA Aid? I have the following
$query = "SELECT TableB.Bid, TableB.Bimagefile, TableA.Aname
FROM TableB
INNER JOIN TableA ON TableB.Aid = TableA.Aid";
but the results are a complete list of all Aname and their matching Bimagefile. I need to have the result show for one Aid. Help please.
$stmt = $con->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
echo "<table class='table table-hover table-responsive table-
bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Image</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$Aname}</td>";
echo "<td>{$Bimagefile}</td>";
echo "</tr>";
}
echo "</table>";
}

Display count of unique values in MySQL db table using PHP

So I have a table named pins that has 3 columns that need to be taken into consideration. These columns are plan and order_id.
I need to get a count for all of the pins that have an order_id=0 and plan=9.
This is what I have so far:
$qT="SELECT plan, COUNT(*) as cnt FROM pins WHERE order_id=0 and plan=9";
$res=mysql_query($qT);<br/>
mysql_free_result($res);
while($row = mysql_fetch_array($res)) {<br/>
echo $row['plan'];<br/>
}
Any help in displaying the results would be a great help.
Try to group your pin using GROUP BY, So
$result = mysql_query("SELECT plan, COUNT(pin) as cnt FROM pins WHERE (order_id=0 and plan=9) GROUP BY plan");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Plan</th><th>Count</th>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>".$row['plan']."</td><td>".$row['cnt']."</td></tr>";
}
echo "</table>";
mysql_free_result($result);
?>

Roll MySql queries into next column using php.

I am creating a monitor display that needs to be able to display 16 numbers at a time. 8 in one table and 8 in the next table. Right now I have to tables that just keep going down the rows based on the number of entries I have in my MySql data base. What I need is for 2 columns in each table (4 numbers in each column) and limit them to the most recent 8 numbers for each given category.
Here is what I have so far:
Some help would be greatly appreciated, I'm pretty stuck on this, thank you.
$select_query = "SELECT buyback.buyback_callNumber
FROM buyback WHERE buyback.buyback_status = 'In-Progress' ";
$select_query2 = "SELECT buyback.buyback_callNumber
FROM buyback WHERE buyback.buyback_status = 'Ready for Pickup'";
$result = mysql_query($select_query);
echo "
<align='center'>
<div class='span9' >
<table class='table table-striped table-bordered'>
<tr>
<td><strong><h3>In-Progress</h3></strong></td>
</tr>";
while($record = mysql_fetch_array($result)){
echo "<tr>";
echo "
<td><strong><h3>".$record['buyback_callNumber'] ."</h3></strong></td></tr>";
}
echo "</table></div>";
echo "
<div class='span9'>
<table class='table table-striped table-bordered'>
<tr>
<td><strong><h3>Ready</h3></strong></td>
</tr>";
$result2 = mysql_query($select_query2);
while($record2 = mysql_fetch_array($result2)){
echo "<tr>";
echo"
<td><strong><h3>".$record2['buyback_callNumber'] ."</h3></strong></td></tr>";
}
echo "</table></div></align>";
echo "</form> ";
As for limiting results to 8 per table, just add 'LIMIT 8' to each query (you may want to ORDER these queries as well by date or some other field you may have set up)
$select_query = "SELECT buyback.buyback_callNumber
FROM buyback WHERE buyback.buyback_status = 'In-Progress' LIMIT 8";
$select_query2 = "SELECT buyback.buyback_callNumber
FROM buyback WHERE buyback.buyback_status = 'Ready for Pickup' LIMIT 8";
not sure why you want to use table when this can be achieved with div's only.
To align the columns side by side place float:left in the divs you want to show side by side.
<div style="float:left> 1st 4 values </div>
<div style="float:left> 2nd 4 values </div>
just a generic suggestion, avoid using tables for html layout as they are outdated and html has attributes like display: table-row, display: table-cell; to suit the purpose

2 while loops in php - mysql select statement

I have 2 tables in DB:
clients (Show all clients data)
clientsmany (admin could add many phone numbers for each client)
I would like to print all the details about the clients in 1 html table and if any client has more than phone number, all the numbers are printed in the same cell of 'td'
<?php
$result = mysql_query("SELECT * FROM clients");
$result1 = mysql_query("SELECT clientsmany.phone, clients.ID FROM clients INNER JOIN clientsmany ON clients.ID=clientsmany.ClientID");
while($row = mysql_fetch_array($result)){ //Not read!
while($row1 = mysql_fetch_array($result1)){ //Working correctly and show the list of 'phone' in $row1 for clientsmany.phone
echo "<center><table border='1'>";
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td></tr>";
echo "</table></center>";
}}
?>
Why the 1st while is not working??
The 2nd while only works and print a correct data then it exit automatic!
<?php
echo "<center><table border='1'>";
$result = mysql_query("SELECT * FROM clients");
while($row = mysql_fetch_array($result)){
$result1 = mysql_query("SELECT * FROM clientsmany WHERE clientsid=".$row['id']);
if(mysql_num_rows($result1)>0 ){
while($row1 = mysql_fetch_array($result1)){
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."<br>".$row1['phone']."</td> </tr>";
}
}else{
echo "<tr><td>".$row['ID']."</td><td>".$row['phone']."</td></tr>";
}
}
echo "</table></center>";
?>
Use GROUP_CONCAT to create a single query and you will be able to a single loop
GROUP_CONCAT will take a column that is repeated and separate each value with a comma (by default it can be changed) and return it into a single value
$query = <<<END
SELECT
clients.*,
GROUP_CONCAT(clientsmany.phone) as phonenums
FROM
clients
INNER JOIN
clientsmany ON clients.ID=clientsmany.ClientID
GROUP BY
clients.ID
END;
A query like this would give you all the clients table columns and a column named phonenums which will be a comma separated list of the phone numbers
Now since you only have one query you only need one loop
$db = mysqli_connect(...);
...
//only need to echo out the <table> part once
//so taken out of the while loop
echo "<center><table border='1'>";
$result = mysqli_query($db,$query);
while( ($row = mysqli_fetch_assoc($result)) ) {
echo <<<END
<tr>
<td>{$row['ID']}</td>
<td>{$row['SomeOtherColumn']}</td>
<td>{$row['phonenums']}</td>
</tr>
END;
}
//Again the </table> only needs done once
//so taken out of the loop
echo "</table></center>";
Notice the mysli_* functions being used. Mysql api is depreciated, for the most part you can just rename the functions you currently use to mysqli_, but note that some require the $db link as a parameter so make sure to read the php manual on each of the functions so you know how to call them correctly.
Also note that I am using heredoc syntax instead of doing multiple echo calls
First, mysql_* functions are depreciated. Try to use mysqli_* functions.
If you want to display data in 1 html table, so why you have started while above table tag?
Try this query..
SELECT clientsmany.phone, clients.* FROM clients, clientsmany WHERE clients.ID=clientsmany.ClientID"
Then use while statement below table tag (No need of 2 different while loop)...
echo "<center><table border='1'>";
while($row1 = mysqli_fetch_array($result1)) {
// your <tr><td> code
}
echo "</table></center>";

Limit the data that will be shown on the table and add pages for the next data. a gridview like table

i have my sql server connection
$sql_exp = "select * from dbo.PC inner join dbo.FA_PC on dbo.PC.PCID = dbo.FA_PC.PCID WHERE UserID is NOT NULL order by dbo.fa_pc.PCID";
$sql_exp1= "select * from dbo.users inner join dbo.FA_PC on dbo.users.UserID = dbo.FA_PC.UserID order by dbo.fa_pc.PCID";
$sql_exp2 = "select * from dbo.FA_Type inner join dbo.FA_PC on dbo.FA_Type.FA_TypeID = dbo.FA_PC.FA_TypeID order by dbo.fa_pc.PCID";
$rs = $conn->Execute($sql_exp);
$rs1 = $conn->Execute($sql_exp1);
$rs2 = $conn->Execute($sql_exp2);
echo "<table border='1' cellpadding='1' cellspacing='0' id='rounded-corner'><tr><th>Desktop Number</th><th>Employee</th><th><p align=left>Fixed Asset Accountability</p></th></tr>";
echo '<select name="print">';
echo "<option value=".$rs->Fields("PC_Number")."> </option>";
while (!$rs->EOF) {
set_time_limit(0);
echo "<td>CP # <br>".$rs->Fields("PC_Number")."</td>";
echo "<td>".$rs1->Fields("EmployeeName")."</td>";
echo "<td>".$rs2->Fields("FA_Type")."</td><tr>";
$rs->MoveNext();
$rs1->MoveNext();
$rs2->MoveNext();
}
echo "</table>";
$rs->Close();
$rs1->Close();
$rs2->Close();
so this code will just print every single data in my database , around 100 details and how could i just limit the data input by 10, 20, 30,40,50,100 and just add few next and previous page link for the remaining data which is mos likely a gridview like form
Try to put a LIMIT. e.g for page 1 LIMIT 0, 10, page 2 LIMIT 10,10, page 3 'LIMIT 20, 10`, so on and so fort.....
where LIMIT 0(page), 10(max content shows)

Categories