How can I put identical columns (sorted by "level" in this case) together? I am making a highscore where I list them by level from my database. I want them to have the same ID if they are the same level.
But I do not want to display the ID on the others. Only the first one.
Here is an example:
ID - Name - Level
1 - John - 5
2 - David - 4
3 - Josh - 3
- Sam - 3
4 - George - 2
So I want to put them together, but if they have the same level, only the first one displays the ID.
I don't want it to look like:
1 - John - 5
2 - David - 4
3 - Josh - 3
3 - Sam - 3
4 - George - 2
Right now, it is just listing everyone, and giving each one a unique ID. Even if they have the same "level".
How can I fix this? Here is my code:
<?php
$sql = mysql_query("SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500");
$id = 1;
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$onrow = '';
$typeServ = '';
$player_name = urlencode($name);
if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}
if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}
if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'active';
} else {
$typeServ = '';
}
echo "<tr class=" . $typeServ . ">";
echo "<td>" . $id . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
$id++;
}
echo "</tbody>";
echo "</table>";
?>
you can create a temporary variable for the current level and check it to see if the id will be shown on the output or not.
$id = 1;
$last_player_lvl = '';
while($row = mysql_fetch_array($sql)){
///....
echo "<tr class=" . $typeServ . ">";
echo "<td>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
if($last_player_lvl == $row['level']){
$id = $id;
}else{
$id++;
}
$last_player_lvl = $row['level'];
//....
Related
I'm learning PHP, I create a page to show my data table in MySQL. I want to paging, searching, sorting with that data.
I write some conditions to search and sort. But it's not working. My Paging seem work fine.
<?php
echo "<form action = 'Activiti_Data_Table_Detail.php' method = 'post'>";
echo "<input type = 'text' name = 'valueToSearch' placeholder = 'Value to search'><br><br>";
echo "<input type = 'submit' name = 'search' value = 'Filter'><br><br>";
echo "<input type = 'submit' name = 'ASC' value = 'Ascending'><br><br>";
echo "<input type = 'submit' name = 'DESC' value = 'Descending'><br><br>";
echo "<table id = 'datatable' border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID_</th>";
echo "<th>PROC_INST_ID_</th>";
echo "<th>BUSINESS_KEY_</th>";
echo "<th>PROC_DEF_ID_</th>";
echo "<th>START_TIME_</th>";
echo "<th>END_TIME_</th>";
echo "<th>DURATION_</th>";
echo "<th>START_USER_ID_</th>";
echo "<th>START_ACT_ID_</th>";
echo "<th>END_ACT_ID_</th>";
echo "<th>SUPER_PROCESS_INSTANCE_ID_</th>";
echo "<th>DELETE_REASON_</th>";
echo "<th>TENANT_ID_</th>";
echo "<th>NAME_</th>";
echo "</tr>";
echo "</thead>";
//connect to database
$con=mysqli_connect("localhost","root","123456","activiti") or die(mysqli_connect_errno());
//define how many results per page
$result_per_page = 10;
//number of results stored in database
$sql = "SELECT * FROM act_hi_procinst";
$result = mysqli_query($con, $sql);
$num_of_results = mysqli_num_rows($result); //225
//determine number of total pages available
$number_of_pages = ceil($num_of_results/$result_per_page);
//determine current page is on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
//determine the SQL LIMIT starting number for the result on the displaying page
$this_page_first_result = ($page - 1) * $result_per_page;
if(isset($_POST['search']) & isset($_POST['ASC'])){
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search']) & isset($_POST['DESC'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['ASC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['DESC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} else {
$sql = "SELECT * FROM act_hi_procinst LIMIT" .$this_page_first_result . ',' . $result_per_page;
}
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td><a href='Data_Table_Detail.php' target='_blank'>" . $row['ID_'] . "</a></td>";
echo "<td>" . $row['PROC_INST_ID_'] . "</td>";
echo "<td>" . $row['BUSINESS_KEY_'] . "</td>";
echo "<td>" . $row['PROC_DEF_ID_'] . "</td>";
echo "<td>" . $row['START_TIME_'] . "</td>";
echo "<td>" . $row['END_TIME_'] . "</td>";
echo "<td>" . $row['DURATION_'] . "</td>";
echo "<td>" . $row['START_USER_ID_'] . "</td>";
echo "<td>" . $row['START_ACT_ID_'] . "</td>";
echo "<td>" . $row['END_ACT_ID_'] . "</td>";
echo "<td>" . $row['SUPER_PROCESS_INSTANCE_ID_'] . "</td>";
echo "<td>" . $row['DELETE_REASON_'] . "</td>";
echo "<td>" . $row['TENANT_ID_'] . "</td>";
echo "<td>" . $row['NAME_'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</form>";
//display the links to the page
for($page = 1; $page <= $number_of_pages; $page++) {
echo '' . $page . ' ';
}
mysqli_close($con);
?>
When I run this, it say: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result.
Can you help me?
Thank you!
try change all your ifs for this:
$valueToSearch = isset($_POST['search']) ? $_POST['valueToSearch'] : '';
$orderBy = isset($_POST['ASC']) ? 'ORDER BY END_TIME_ ASC' : (isset($_POST['DESC']) ? 'ORDER BY END_TIME_ DESC' : '');
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%' " . $orderBy . " LIMIT " . $this_page_first_result . ',' . $result_per_page;
I have a tree with nodes and I wanted to display every node's level in a table,
but when it does not display anything.
I tried to put a comment when I do: findlevel($_SESSION['id'], $level +1, $idtofind); at the end of the function, if I do that it works but only for the first level of the Tree.
My code:
function findlevel($parent_id, $level, $idtofind)
{
global $data, $index, $tree;
$parent_id = $parent_id === NULL ? "NULL" : $parent_id;
if (isset($index[$parent_id])) {
foreach ($index[$parent_id] as $id) {
$current_id = $data[$id]["id"];
$levell = $level+1;
if($current_id == $idtofind) {
echo $current_id . " " . $idtofind . $levell . "<br>";
return $levell;
}
findlevel($_SESSION['id'], $level + 1, $idtofind);
}
}
}
Code of my table:
$result = $conn->query("SELECT id, email, tipoutente, fullname, datainserimento, DATEDIFF(datainserimento, '$datainserimentosponsor') AS datadiff FROM utenti WHERE inseritoda = " . $_SESSION['id']);
while($row = mysqli_fetch_array($result)){
echo "<tr class=\"righe\">";
$levelnode =findlevel($_SESSION['id'], 0, $row['id']);
if($row['tipoutente'] == "GRUPPO PRIVATO" || $row['tipoutente'] == "GRUPPO NEGOZI" || $row['tipoutente'] == "GRUPPO ASSOCIAZIONI") {
echo "<td class=\"gruppi\">$progressivoutenti</td>";
echo "<td class=\"gruppi\">" . $row['fullname'] . "</td>";
echo "<td class=\"gruppi\">" . $row['email'] . "</td>";
echo "<td class=\"gruppi\">" . $row['tipoutente'] . "</td>";
echo "<td class=\"gruppi\">" . $row['datainserimento'] . "</td>";
echo "<td class=\"gruppi\">" . $row['datadiff'] . "</td>";
echo "<td class=\"gruppi\">" . $levelnode . "</td>";
//}
} else {
echo "<td class=\"dati\">$progressivoutenti</td>";
echo "<td class=\"dati\">" . $row['fullname'] . "</td>";
echo "<td class=\"dati\">" . $row['email'] . "</td>";
echo "<td class=\"dati\">" . $row['tipoutente'] . "</td>";
echo "<td class=\"dati\">" . $row['datainserimento'] . "</td>";
echo "<td class=\"dati\">" . $row['datadiff'] . "</td>";
echo "<td class=\"dati\">" . $levelnode . "</td>";
}
$progressivoutenti++;
echo "</tr>";
}
echo "</table>";
Edit:
This is the rest of the code:
$data = array();
$index = array();
$albero = array();
$query = $conn->query("SELECT * FROM utenti where utentepadre is not null order by fullname");
$query->data_seek(0);
while ($row = $query->fetch_assoc()) {
$id = $row["id"];
$parent_id = $row["utentepadre"] === NULL ? "NULL" : $row["utentepadre"];
$data[$id] = $row;
$index[$parent_id][] = $id;
}
That's how my tree looks like:
My Tree
And the result is:
The result of the php page.
The result of the variable $data:
Variable $data content
So I am trying to do this..
$userID = $_SESSION['user_session'];
$stmt = $this->db->prepare("SELECT * FROM orrs");
$stmt->bindparam(":id", $userID);
$stmt->execute();
$count = $stmt->rowCount();
echo
"<div class='table-responsive'>
<table class='table' border='1'>
<tr class='head'>
<h3>Snapshot</h3>
<th>Se</th>
<th>#s</th>
<th>Ae</th>
<th>Prt</th>
<th>Pin</th>
</tr>";
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.
Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..
I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts
//The current one
$stmt = $this->db->prepare("SELECT * FROM orrs");
//The get the count for stage '1'
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
$stage_1_count = $stage_1_stmt->rowCount();
//The get the count for stage '2'
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
$stage_2_count = $stage_2_stmt->rowCount();
You can execute these others to get the counts which you should use in place of $count in your loop.
Your while loop then becomes
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $stage_1_count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $stage_2_count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
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
Hey guys, first time using stackoverflow.
can you guys help me debug?
Heres the problem, this query is selecting all of the rows from my database, its only outputting the first one twice for some reason.
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
You are looping over the rowset, but never retrieving its value more than once. You pulled all of the values out of the first row, and cached them here:
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
Move this code into the loop, and remove the first fetch.
You need to update $rating, $description, etc. within the while loop:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating'];
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
Or, of course, you can inline $rating, etc., writing $row['rating'] instead.
Note: you probably want to run your variables through htmlspecialchars before inserting them into HTML. Otherwise, a description like <script>alert('hacked');</script> could execute a script, opening yourself up to XSS attacks.
You can also use extract. I do not recommend you do this, however, as it may cause problems and confusion for other developers:
<?php
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
extract($row);
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}
?>
The variables $rating etc are not "binded" to the expressions $row['rating'] etc. Once set, They will forever take these values unless you modify them again.
See PHP: Assignment Operators for detail.
Try to rewrite them as:
$top10_query = "SELECT * FROM kicks";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
while($row = mysqli_fetch_assoc($result)) {
$rating = $row['rating']; // <-- use the new value every time a row is fetched.
$description = $row['description'];
$completed = $row['completed'];
$userid = $row['userid'];
$posted = $row['posted'];
echo "<tr>";
echo "<td class='rating'>" . $rating . "</td>";
echo "<td class='description'>" . $description . " </td>";
echo "<td class='completed_" . $completed . "'>" . $completed . "</td>";
echo "<td class='author'>";
echo "Posted by: <a href='profile?userid=" . $userid . "'>" . $userid . "</a><br />";
echo "on "; echo $posted;
echo "</td>";
echo "</tr>";
}