MySQL row not being ordered in php page - php

Here you can see the code I use to display a table from a MySQL database in a PHP webpage:
<?php
$con=mysqli_connect("localhost","aaaaaa","bbbbb","my_mk7vrlist");
$x = 1;
$result = mysqli_query($con,"SELECT * FROM 0_vrs_japan ORDER BY `vrs` DESC, `date` ASC");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $x . "</td>";
echo "<td>" . $row['playername'] . "</td>";
echo "<td><img src='http://mk7vrlist.altervista.org/flags/" . $row['Country'] . ".gif' /></td>";
echo "<td>" . $row['contactable'] . "</td>";
echo "<td>" . $row['vrs'] . "</td>";
if($row['date'] != "-"){
$formatted = date('jS F Y', strtotime($row['date']));
} else {
$formatted = "-";
}
if($row['pic'] != "-"){
echo "<td>" . $formatted . "</td>";
} else {
echo "<td>" . $formatted . "</td>";
}
echo "</tr>";
$x = $x+1;
}
mysqli_close($con);
?>
And the result is this:
Every player with 99'999 points (which are called 'vrs') is sorted by the date. I am having troubles with that player I have just added.
His date 2014/05/22 in the database isn't in the correct position (after Megaman). Do you have any idea?

You're storing the date as a varchar so MySQL is having trouble sorting it in the way you need it to, as it's sorting lexicographically with the field being a varchar.
Convert them to proper date fields and natural date sorting will work as intended.

Related

How to separate date and time in php when displaying data in loop

I can't figure out how to separate the date and time when displaying the data from the database table.
if($num>0){
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row['Id'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Number'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Password'] . "</td>";
echo "<td>" . $row['CNIC'] . "</td>";
echo "<td>" . $row['dt'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
If your dt field contains a properly formatted datetime string (i.e. the MySQL column data type is set to datetime) then you can create a new DateTime object in PHP and format it however you want.
$dt = new DateTime($row['dt']);
$time = $dt->format('h:i:s');
$date = $dt->format('Y-m-d');
After the while..
$date = date("Y-m-d", $row['dt']);
$time = date("h:i:sa", $row['dt']);
then use those variables.
It works when dt is a unix timestamp or parsable date format.

PHP/SQL Per-Person Search

How would i do a 'per-person search' using PHP? I have it at the moment working, but i want to be able to go to this: /profile/JohnSmith, or something similar.
At the moment i have it like this (probs not the most efficient way but it works):
$sql = "SELECT * FROM data_players_sg WHERE player LIKE '%" . $name . "%'";
$result = $conn->query($sql);
echo "<br /><h3>Survival Games Stats</h2>";
echo "<div id=containter class=CSSTableGenerator>";
echo "<table id=player_profile cellspacing=15><tr><th>Points</th><th>Wins</th><th>Losses</th><th>Kills</th><th>Deaths</th><th>KDR</th></tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row["player"] == $name) {
$UUID = $row["uuid"];
echo "<tr><td>" . $row["points"] . "</td>";
echo "<td>" . $row["wins"] . "</td>";
echo "<td>" . $row["losses"] . "</td>";
$kills = $row["kills"];
$deaths = $row["deaths"];
$kdr = $deaths != 0 ? $kills / $deaths : $kills;
echo "<td>" . $kills. "</td>";
echo "<td>" . $deaths. "</td>";
echo "<td>" . $kdr. "</td></tr>";
}
}
} else {
echo "<tr><td>Player not found</td><td></td><td></td><td></td></tr>";
}
echo "</table></div>";
Which is called when they search, and this works fine. Except it means you have to search in the search bar every time you want to get to this page, instead of being able to go straight to the page with a URL.
So in short i want it to create a page per person, which is their profile.
First of all, you can start by creating a new page, called profile.php or something similar. Next, use a combination of a .htaccess rewrite of the url (for example, example.com/player-name will be rewritten to example.com/profile.php?url=player-name, and a GET statement in your profile.php to retrieve this player-name. Use this name, id or url to match with data in your database. Use a query like this:
$x=$_GET["url"];
$query = "SELECT * FROM data_players_sg WHERE playername =".$x;
if ($result = mysqli_query($link, $query))
{
while ($row = mysqli_fetch_assoc($result))
{
$UUID = $row["uuid"];
echo "<tr><td>" . $row["points"] . "</td>";
echo "<td>" . $row["wins"] . "</td>";
echo "<td>" . $row["losses"] . "</td>";
$kills = $row["kills"];
$deaths = $row["deaths"];
$kdr = $deaths != 0 ? $kills / $deaths : $kills;
echo "<td>" . $kills. "</td>";
echo "<td>" . $deaths. "</td>";
echo "<td>" . $kdr. "</td></tr>";
}
}
Hope this helps.
Best regards,
Motbrok

PHP: Building an HTML Table from an Array with Repeated Entries

I have a database table (built from a query) that lists member (called Troop), events, start date, and end date. A member might be listed more than once in the table as they are associated with more than one event.
I want to build an HTML table that lists the member's name followed by each event they are associated with before going on to the next member, which may be a few items down in the array.
I was thinking about using aforeach() statement but could not get it to work. Any pointers on how I might accomplish this? Thanks!
while(list($Event, $Troop, $StartDate, $EndDate) = mysqli_fetch_array($result)) {
$return=$return . "<table>";
$return=$return . "<tr>";
$return=$return . "<th colspan=3>" . $Troop . "</th>";
$return=$return . "</tr>";
$return=$return . "<tr>";
$return=$return . "<th>Event</th>";
$return=$return . "<th>Start Date</th>";
$return=$return . "<th>End Date</th>";
$return=$return . "</tr>";
//foreach ($Troop as $thisTroop) {
$return=$return . "<tr>";
$return=$return . "<td>" . $Event . "</td>";
$return=$return . "<td>" . $StartDate . "</td>";
$return=$return . "<td>" . $EndDate . "</td>";
$return=$return . "</tr>";
//}
$return=$return . "</tr>";
$return=$return . "</table>";
}
Replace your code with this. It's necesary that the sql result is sorted by Troop.
$return = '';
$currentTroop = '';
while(list($Event, $Troop, $StartDate, $EndDate) = mysqli_fetch_array($result)) {
if ($currentTroop != $Troop) {
$return .= "<table>"
."<tr>";
."<th colspan=3>" . $Troop . "</th>"
."</tr>"
."<tr>"
."<th>Event</th>"
."<th>Start Date</th>"
."<th>End Date</th>"
."</tr>";
}
$return .= "<tr>"
."<td>".$Event."</td>"
."<td>".$StartDate."</td>"
."<td>".$EndDate."</td>"
."</tr>";
if ($currentTroop != $Troop) {
$return .= "</tr>"
."</table>";
$currentTroop = $Troop;
}
}
Create a multi-dimensional array with keys of Troops and group every records with the same Troop. And then use foreach() to loop the array and print out the information.
Group array by subarray values

PHP - Display SQL results in multiple tables

I have a database table with student room assignments. Each student has a specific hall, floor, and apartment. I need to display each student in a specific table so the results look like a floor layout. Below is an example. The student ID needs to be in the correct Apartment slot. There could be several ID's per apartment. Right now it just lists them down the page.
Apartment 102 Apartment 101
Apartment 104 Apartment 103
Apartment 106 Apartment 105
$query = "select res.ID_NUM as ID, res.APARTMENT
From Residents res
Where res.sess_cde = '$pulledsession'
and res.ROOM_ASSIGN_STS = 'A'
and res.BLDG_CDE = '$pulledhall'
and res.FLOOR = '$pulledfloor'";
$result = odbc_exec($connect, $query);
echo "<table style='padding:25;'>
<tr>
<th>Apartment</th>
<th>ID</th>
</tr>";
while(odbc_fetch_row($result)){
$ID = odbc_result($result,ID);
$APARTMENT = odbc_result($result,APARTMENT);
if ($APARTMENT == $pulledfloor.'01')
{
echo "<tr >";
echo "<td>" . $pulledfloor.'01' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'02')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'02' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'03')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'03' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'04')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'04' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'05')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'05' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'06')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'06' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
}
echo "</table>";
You would need to retrieve the room number as well. After that, one way to do it would be to load the results into an associated array:
$rooms[$row['roomNumber']] = $resName; // Example
Once you have the array built, then you can echo it back out into a table, either manually (build the full table and echo each one with
echo "<tr><td>".$rooms['102']."</td><td>".$rooms['101']."</td></tr>";
or similar, or do it dynamically by incrementing two room numbers in a loop.
If you have multiple students in a room, then tack on more depth to the array:
$rooms[$row['roomNumber']][] = $resName;
Then use a loop in each cell to echo it back out.

PHP - How do I get this to print out in a table from an reference number?

echo '<td><input type="checkbox" name="items[]" value="' . $row['0'] . '" /></td>';
Hi, I'm trying to get a reference number which comes from a an array called items from another page as shown above, and to find it in the table and print out the reference row, like "Title,Platform...." into another table, but I can't seem to get it working...any help be appreciated
if (isset($_POST['items'])) {
$n = count($_POST['items']);
for($i=0; $i < $n; $i++){
// echo $_POST['items'][$i];
}
$items = array();
foreach ($_POST['items'] as $item) {
$items[] = pg_escape_string($con, $item);
}
if (!$_SESSION["selectingrows"]) {
$item_string = "'" . implode("','", $items) . "'";
$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames WHERE 'refnumber' IN ($item_string)");
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
}
}
One thing, you need to put {} braces after your while loop.
Here is what you are doing:
while($rows = pg_fetch_assoc($result))
echo"<tr>"; echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
By not putting braces around the code after the while statement, here is what your code really does:
while($rows = pg_fetch_assoc($result))
{
echo"<tr>";
}
echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
You should always put braces in to define what code is in the while loop.
You want your code to be something like this:
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
Format your code neatly and properly. By doing this your code is clearer and it is much easier to notice possible mistakes like the above. Always use braces for if, while, for statements. When putting an end line semicolon ; put in a new line break. Indent your code correctly. It's little things like formatting that make coding easier.
Now the next problem I can see is the values you are getting from the $rows array:
$rows['1'];
$rows['2'];
$rows['3'];
$rows['4'];
This is trying to get something from the $rows array which has the key of string '1'.
Usually you access array values by index, which uses an integer beggining from 0. Or you access it by a key.
Either you can try this:
$rows[0];
$rows[1];
$rows[2];
$rows[3];
Or this:
$rows['title'];
$rows['platform'];
$rows['description'];
$rows['price'];

Categories