i am making a top 10 list of the best times from my bhop server in csgo. All the times get stored in the database, but the problem is that each time the user gets a new time it adds a new row. With the code i have now it displays the top ten list of all the times, i want it to only display one result per user(the best one)
My current code:
<div id="content">
<table cellspacing="0">
<h1 align="center">bhop_eazy_csgo</h1>
<tr><th>Place</th><th>Name</th><th>Time</th></tr>
<?php
$i = 1;
$con=mysqli_connect("localhost","root","*******","timer");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM round WHERE map = 'bhop_eazy_csgo' ORDER BY time LIMIT 0, 10");
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . ($i) . "</td><td>" . $row['name'] . "</td> <td>" . gmdate("i:s", $row['time']) . "</td></tr>";
$i++;
}
mysqli_close($con);
?>
</table>
</div>
How it looks:
http://gyazo.com/3653ec8a87fbf68c4137b67b75e20f8d
Edit
Now it looks like this:
1 Wildmine 00:49 2 Wildmine 00:50 3 Wildmine 01:02 4
?J#rr3? 01:12
I want it to look like this:
1 Wildmine 00:49 2 ?J#rr3? 01:12
Thanks to Michael Wagner for helping me getting it working:)
$result = mysqli_query($con,"
SELECT DISTINCT name n, (
SELECT time
FROM round
WHERE map = 'bhop_eazy_csgo'
AND name = n
ORDER BY time
LIMIT 1
) AS time
FROM round
WHERE map = 'bhop_eazy_csgo'
ORDER BY time
LIMIT 0 , 10
");
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . ($i) . "</td><td>" . $row['n'] . "</td> <td>" . gmdate("i:s", $row['time']) . "</td></tr>";
$i++;
}
Try this:
SELECT DISTINCT name n, (
SELECT time
FROM round
WHERE map = 'bhop_eazy_csgo'
AND name = n
ORDER BY time
LIMIT 1
) AS time
FROM round
WHERE map = 'bhop_eazy_csgo'
ORDER BY TIME
LIMIT 0 , 10
Related
I am trying to find if two runners have ever ran in the same race. The two runners are Peter Smith and Diane Peters.
$resultRaceType = mysqli_query($db,"SELECT DISTINCT date,time FROM results where runner = 'Peter, Smith' ");
while($row = mysqli_fetch_array( $resultRaceType ))
{
$resultRaceType1 = mysqli_query($db,"SELECT * FROM results where date = ' " . $row['date'] . " ' and time = ' " . $row['time'] . " ' and runner = 'Diane, Peters'");
while($row1 = mysqli_fetch_array( $resultRaceType1 ))
{
echo "<tr >";
echo "<td>";
echo $row1['date'];
echo " - " . $row1['time'];
echo "</td>";
echo "<tr>";
}
}
The above code works, but only if I limit the first select to LIMIT 50. So I can see that it is timing out. My table has over 100K rows. I know I am doing something wrong but cant see what it is.
Thanks for any help you guy's can give me.
Try:
SELECT a.date, a.time FROM results a
JOIN results b ON (a.date = b.date AND a.time = b.time)
WHERE a.runner='Peter, Smith' AND b.runner='Diane, Peters';
I'm having this problem on how I can access a specific table cell when I get the column from a mysqli_fetch_array result and storing it in a variable.
$sql5 = "SELECT e.*, #curRow := #curRow + 1 AS row_number FROM employee e JOIN (SELECT #curRow := 0) r WHERE e.team_team_id = '$team_id' AND (position_pos_id = 3 or position_pos_id = 4 or position_pos_id = 5 or position_pos_id = 6 or position_pos_id = 7 or position_pos_id = 8 or position_pos_id = 9 or position_pos_id = 10 or position_pos_id = 11 or position_pos_id = 13 or position_pos_id = 14 or position_pos_id = 15 or position_pos_id = 16)";
$result5 = mysqli_query($connect, $sql5);
$row5 = mysqli_fetch_array($result5);
for($i=0;$i<$number;$i++){
echo "<select id='testingDiv".$i."' name='employee[]' class='clonedInput'>
<option value='".$row5['row_number'][$i]."'>" .$row5['emp_fname']. " " .$row5['emp_lname'] ."</option>";
while ($row3 = mysqli_fetch_array($result3)) {
echo "<option value='" . $row3['emp_id'] ."'>" .$row3['emp_fname']. " " .$row3['emp_lname'] ."</option>";
}
echo "</select><br>";
As you can see I was planning to iterate it through a counter and treat it like a 3d array but I found out that it wasn't possible. Now I'm at a lost as I don't know how to access the specific cell of the column.
To do what you want, you should use the structure of the inner loop in the outer one too
$result5 = mysqli_query($connect, $sql5);
$i = 0;
while ($row5 = mysqli_fetch_array($result5)) {
echo "<select id='testingDiv" . $i . "' name='employee[]' class='clonedInput'>
<option value='" . $i . "'>" .
$row5['emp_fname'] . " " . $row5['emp_lname'] .
"</option>";
while ($row3 = mysqli_fetch_array($result3)) {
echo "<option value='" . $row3['emp_id'] . "'>" .
$row3['emp_fname'] . " " . $row3['emp_lname'] .
"</option>";
}
echo "</select><br>";
$i++;
}
Edit (explaination of what's going on)
The main problem with your code is that mysqli_fetch_array returns a single row of the resultset, so you cannot use that result as a bidimensional array and access all the rows with the for loop you were using.
What you have to do is translate that outer for into a while loop, so that on each iteration you fetch a new row into that $row5 variable. By doing that, you need to handle the counter value manually, hence that $i++ at the end.
Regarding the translation of $row5['row_number'][$i] into $i, I guess you wanted to get the current row index. For the same reason I explained above, that instruction is wrong, because the $row5 variable contains a single row, which most likely won't have a 'row_number' column and definitely won't have a second dimension you can access with $i. The current row index however is already available in $i itself, so I used that counter instead.
I have two tables: departments (20 departments) and tickets (lets say
1000 tickets). Each ticket is assigned to one department. I wanted to
know how many tickets are assigned to each department.
[SOLVED] thank to the kind tip from frz3993
for the sake of completing the thread with the working result, at bottom you find my new script
I succeeded in that with these two queries.
The former loads the departments.
For the latter, I have used SELECT COUNT for how many tickets for the current department.
PHP
<?php
$mysqli = new mysqli("localhost", "root", "*****", "tickets");
$openticket = null;
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT id, name FROM dept ORDER BY id ASC"; // loads all the departmentes and their id
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
//echo $row["id"] . " " . $row["name"] . "<br>"; // test point
$sqlcounttickets = "SELECT COUNT(dept_id) FROM ticket WHERE (dept_id=" . $row["id"] . " AND status!=1)"; // count how manytickets for that department id , IF status 1, skip, since ticket is closed
//echo $sqlcounttickets; // test point
$result2 = $mysqli->query($sqlcounttickets); //execute second query and get the result of the SELECT COUNT
//if ($mysqli->error) { //test point
// die($mysqli->error);
//} // no errors
$rowdue = $result2->fetch_row();
if ($rowdue[0] > 0){
echo "DeptLabelNum: " . $row["id"] . " - DeptName: " . $row["name"] . " " . $rowdue[0] ."<br>";
}
$openticket=$openticket+$rowdue[0];
}
/* free result set */
$result->free();
}
echo "<br>" . "Open Tickets: " . $openticket;
/* close connection */
$mysqli->close();
?>
the output is obviously unsorted since the tickets amount for department is random
DeptLabelNum: 0 - DeptName: Global (All Departments) 1
DeptLabelNum: 1 - DeptName: LCD 1
DeptLabelNum: 2 - DeptName: Smartphones 6
DeptLabelNum: 4 - DeptName: Pendrive 4
DeptLabelNum: 6 - DeptName: Plasma 7
DeptLabelNum: 22 - DeptName: HDD 1
DeptLabelNum: 23 - DeptName: Notebook 8
DeptLabelNum: 24 - DeptName: Tablet 12
Open Tickets: 40
You may bet on it :-) , I'd like to sort the output in descending order
So Tablet should be the first with 12 tickets
second notebook with 8 tickets
3rd plasma
and so on
Do you suggest to load the output of the cycle into a MySQL temporary table?
Or would you use an PHP array?
Or it can be done with a more effective query?
Thank you for any help and suggestion since I am confuse with any of the three
R.
P.S. SOLUTION - the new script with one query only
this is the new script which encloses in an html table the result.
PHP
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
echo '<table>'."\xA";
$query = "
SELECT COUNT( ticket.id ) AS ticket_count, dept.id, dept.name
FROM ticket
LEFT JOIN dept ON ticket.dept_id = dept.id
WHERE ticket.status !=1
GROUP BY dept.id
ORDER BY ticket_count DESC";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo "\t" . "<tr><th>" . $row["name"] . "</th><th>" . $row["ticket_count"] . "</th></tr>". "\xA";
$openticket=$openticket+$row["ticket_count"];
}
/* free result set */
$result->free();
}
echo "\t" . "<tr><th></th><th></th></tr>". "\xA";
echo "\t" . "<tr><th>" . "Open Tickets: " . "</th><th>" . $openticket . "</th></tr>". "\xA";
echo "</table>". "\xA";
/* close connection */
$mysqli->close();
?>
You can do it by one query. Also, to be sure you get the list of all departments even there is no tickets for them:
SELECT d.*,tmp.ticket_count FROM departments d
LEFT JOIN (SELECT count(*) AS ticket_count, department_id FROM tickets GROUP by department_id) tmp
ON d.id = tmp.department_id
I have a table "data" with a column "name" "state" and a few more
name state
peter MN
john NY
jay NY
sam CO
jack TX
jill NO
I want to calculate the number of entries per state and want my output as follows for example:
NY: 125
MN: 21
CO: 17
TX: 10
NO: 59
etc...
I have a query like this
$stmt = $db->query("SELECT state, COUNT(*) FROM `data` GROUP BY state;");
$nums = $stmt->rowCount();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>" . $row["state"] . "</td>
<td>$nums</td>
</tr>";
}
This displays every state in my table but does not return the corresponding number of entries for that state. This only returns the number of states i.e. 50. How can I display the number of entries per state?
You seem not to be referring to the column which has the count. Try aliasing it an referencing the alias in your PHP code:
$stmt = $db->query("SELECT state, COUNT(*) cnt FROM `data` GROUP BY state;");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>" . $row["state"] . ":</td>
<td>" . $row["cnt"] . "</td>
</tr>";
}
$stmt = $db->query("SELECT COUNT(name) as occurances,state FROM `data` GROUP BY state;");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>" . $row["occurances"] . "</td>
<td>" . $row["state"] . "</td>
</tr>";
}
Try this version,select the names only
I'm trying to display data starting at a certain time and day and ending and another time and day. here is my code:
<?php
include 'includes/connect3.php';
$query = "SELECT * FROM u_visits WHERE date >= '2013-08-31 22:56:20' AND date <= '2013-
08- 31 23:59:59'";
$result = mysqli_query($con,$query);
echo "<table><tr>
<th>USER ID</th>
<th>TIMES VISITED</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['visits'] . "</td></tr>";
}
echo "</table>";
?>
When I go to the page, only the table header is displayed with no data showing.
you can also use BETWEEN for range comparisons.
$query = "SELECT * FROM u_visits WHERE `date` BETWEEN '2013-08-31 22:56:20' AND '2013-08-31 23:59:59'";
The reason you didn't see any result is because your query has some errors. The error in your query is that you have a space after 2013-08- in your where clause (date <= '2013-08- 31 23:59:59'";)
Had you used proper error handling, you would have noticed this yourself. One common way to do this is:
$result = mysqli_query($con,$query) or die ('Query execution failed: ' . mysqli_error($con));