Fill empty cells in table - php

Basically I have a ranking-page that I'm working on for my CS:GO server. So far it's working fine, and it's looking like this:
https://puu.sh/aJlTj/b6fb5a7a06.png
And the next-page arrow(s) works fine, but the problem is, at page 3+, it's empty because there haven't been more players on the server yet, and it looks like this:
https://puu.sh/aJlXH/7e69071a2b.png
I am using the answer from Make multiple pages out of a mysql query in order to create the pages for all the players.
My table-printing-part looks like this:
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,15");
while($row = mysqli_fetch_array($query)) {
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
?>
</table>
$offset is declared by using $offset = 15 * intval($_GET['page']); unless it's the first page, then it's set to 0.
I understand that this code will only echo out <tr><td></td></tr>'s as long as there's data to post, so how would I go about checking if the page's cells are empty, then just echo out empty cells? (due to this, I can't use empty-cells: show; either as there are no cells to show, not even empty ones)
Thanks in advance.

I think this is what you want... It basically finds the number of rows that were returned and then continues with blanks until the limit is reached.
$limit = 15;
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,$limit");
$i=0;
while($row = mysqli_fetch_array($query)) {
$i = $i+1;
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
for($i=$i;$i<=$limit;$i++)[
echo "<tr><td> </td><td> </td><td> </td><td> </td></tr>";
}
?>
</table>

Related

How do I correctly echo checkboxes in a PHP table?

I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)
$sql = "SELECT * FROM products";
$answer = mysqli_query($connection, $sql);
if ($answer->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Stock</th><th>Delete Product</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
echo "0 results";
}
Insert the checkbox in an additional table cell (td).
A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
. "</td></tr>";

Need to Count results of a list created in PHP

Okay I have a list being populated and echoed out on a page.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
while($record = mysql_fetch_array($mydata)){
echo "<td>" . $record['units'] . "</td>";
Now the results fluctuate depending on the number of 'mech_units' there are. What I need is to display how many are being displayed in the list. Any suggestions?
you can use built in function mysql_num_rows($mydata). This will give you the total number of records that are fetched.
First of all, I would suggest using mysqli.
You could declare a variable which increases by one every time you echo a 'mech_unit'.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
$i = 0;
while($record = mysql_fetch_array($mydata)){
$i++;
echo "<td>" . $record['units'] . "</td>";
}
echo "There are " . $i . " mech_units.";
Another option would be to use the mysql_num_rows() function.

How to add ajax pagination?

I am using this
PHP Ajax example from w3schools.This is working fine but it's not an efficient solution when the record list will be huge.So i want to add pagination to handle large records.How can i add pagination (Ajax pagination so that i can avoid page reloading) with this?
Here is my code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE type_no = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What I normally do is use the LIMIT feature in mysql. When I look at your example I would assume that the sql statement would only return one result.
Lets assume you had a table called user with many users if you had a query like this:
SELECT * FROM user
You would get all the users back in one query. This is not practical for most uses. The most elegant solution in my opinion is the LIMIT feature.
For example if I wanted to see results in a range:
SELECT * FROM user LIMIT 0,50 #return first 50 results - records 0 to 49
SELECT * FROM user LIMIT 50,50 #return the next 50 results - records 50 to 99
LIMIT syntax looks like this LIMIT <offset>,<count> Where offset is the starting result (starts at 0) and count is the number of results you want back.
So lets assume you had pages size of 50 and and were on the 5th page you would want:
LIMIT 100,25
Handling this at the database level is the simplest and quickest method.

Deleting an item from a mysql cart with php

I'm creating a cart system and trying to find a way to have a simple button that once pressed deletes the corresponding row. However, I cannot seem to find a way to dynamically do this with while loop I currently have.
<?php
//connect to DB.
$con = mysqli_connect("localhost", "root", "", "books");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySql: ". mysqli_connect_error();
}
$query = "SELECT * FROM cart WHERE customerID = '$_SESSION['id']'";
$result = mysqli_query($con, $query);
//creates a table for dumping the DB to, loops through the DB and posts the contents elegantly.
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td></tr>";
$totalprice += $row['bookPrice'];
}
echo "</table>";
echo "The total present price is: $".$totalprice;
//closes the conncection to the DB
mysqli_close($con);
?>
I've considered trying to put an echo query statement into the database and adding "$row['deletebutton']" to the while loop but I'm not sure that would necessarily work.
The easy way is to create a new page and send the message to this page to delete the item.
So, in the table you add a new column with a link
delete
And in the page you treat this value.
To expand on the comment posted to the question, you could add this to your row:
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td><td><input type='checkbox' name='remove[]' value='" . $row['ROW_ID'] . "' /></td></tr>";
And then with the data submitted in the form you could do something like this:
$query = "DELETE FROM cart WHERE ROW_ID IN (" . implode(',',$_POST['remove']) . ")";
Keep in mind to check the value of remove before using it in queries.

Create an HTML5 range "slider" that does the same as the following drop down menu

I have a drop down menu, which is filled from a database. When I select a value in the menu it displays a table of the data selected from the database. I'd like to change this to an HTML5 range slider. So far with no luck. I also want to show the values (dates) beside the range as I move along it.
This is the code to the drop down menu:
// Set SQL string
$query = "SELECT * FROM Test";
// Execute SQL
$result = mysql_query($query);
// Find number of rows in the resulting recordset array
$num = mysql_numrows($result);
// Initialise loop counter
$i = 0;
echo ("<form><select name='users' onchange='showUser(this.value)'>");
// Loop through recordset until end
while ($i < $num) {
// Associate variables for result at position i at table location specified
$Time = mysql_result($result, $i, "Time");
// Echo each entry as an OPTION for the Select List
echo ("<option value=\"$Time\">$Time</option>");
// Increment Loop Counter
$i++;
}
echo ("</select></form><br>");
gettime.php:
$sql = "SELECT * FROM Test WHERE Time = '" . $q . "'";
$resultb = mysql_query($sql);
if (!$resultb) {
echo "<p>The following SQL failed</p><p>" . $sql . "</p>";
}
echo "<table border='1'>
<tr>
<th>Time</th><th>First PC Room</th>
<th>First Group Study Room 1</th>
<th>First Group Study Room 2</th>
<th>First Main Room</th>
</tr>";
while ($rowb = mysql_fetch_array($resultb)) {
$bmsTime = $rowb['Time'];
//Convert Excel Timestamp of DB to Unix Timestamp
$unixtime=($bmsTime-25569)*86400;
$readable=date('l jS \of F Y h:i:s A',($unixtime));
echo "<tr>";
echo "<td>" . $readable . "</td>";
echo "<td>" . $rowb['firstPCroom'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm1'] . "</td>";
echo "<td>" . $rowb['firstGrpStdyRm2'] . "</td>";
echo "<td>" . $rowb['firstmainroom'] . "</td>";
echo "</tr>";
}
echo "</table>";
Below is what I have so far on the "slider":
echo "<input id='slider' type='range' min='0' max=\"$num\" step='any' />
<span id='range'> </span>";
?>
<script>
var selectmenu=document.getElementById("slider");
var colorchange;
selectmenu.onchange=function changecolour(){
if (selectmenu.value<"0.5")
{colorchange=0}
else if (selectmenu.value>="0.5") {colorchange=Math.round(selectmenu.value)}
document.getElementById("range").innerHTML=colorchange;
}
</script>
Any help would be greatly appreciated! Thanks
Here is a jsFiddle that updates a table row using jQuery. Presumably you would replace the data with an AJAX call.
If you were a little more specific about where exactly you were having trouble, someone else may be able to tailor a solution that better fits your needs.

Categories