I am building a leaderboard which is built by joining two different databases, the most important is the wallet column which is what I'll use to rank the players. I need to add a column ranked from the player with most money to least amount of money. What is the best way to do this?
$result = mysqli_query($conn, "SELECT * FROM darkrp_player inner join playerinformation on (darkrp_player.uid = playerinformation.uid)");
Basically, you need to order by on your wallet column to short for showing most money to least amount of money of the user i.e DESC ordering. But if you want to get the rank number value of each user also apart from ordering you can do it this way with ROW_NUMBER() window function
SELECT *, rank FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY wallet DESC) as rank
FROM darkrp_player INNER JOIN playerinformation ON (darkrp_player.uid = playerinformation.uid)
) t
Expected Output: on $result variable
id other........columns wallet rank
1111 xyz............abc 100 1
2222 xyz............abc 90 2
3333 xyz............abc 80 3
alright these work! here's my code for anyone that might across something similar
<table class="table" data-order='[[ 0, "asc" ]]'>
<thead>
<tr>
<th>Place</th>
<th>Avatar</th>
<th>Name</th>
<th>Salary</th>
<th>Wallet</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("bla bla bla");
$result = mysqli_query($conn, "SELECT * FROM darkrp_player inner join playerinformation on (darkrp_player.uid = playerinformation.uid) ORDER BY wallet DESC");
$rank = 1;
while ($row = mysqli_fetch_assoc($result)):
?>
<?php
$steamid2 = $row['steamID'];
$slice = substr($steamid2, strpos($steamid2, ":") + 1);
$n = substr($slice, 0, 1);
$x = substr($slice, strpos($slice, ":") + 1);
$steamid64 = 76561197960265728 + 2 * $x + $n;
$json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=keykeykeykeykeyk&steamids='.$steamid64.'');
$parsed = json_decode($json);
?>
<tr id="rank<?php echo $rank++; ?>">
<td><?php echo $rank - 1; ?></td>
<td><?php foreach($parsed->response->players as $player){
echo "<img src='" . $player->avatarmedium . "'>";
} ?></td>
<td><?php foreach($parsed->response->players as $player){
echo "" . $player->personaname . "";
} ?></td>
<td><?php echo $row['salary']; ?></td>
<td><?php echo $row['wallet']; ?></td>
<?php endwhile; ?>
</tr>
</tbody>
</table>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.6.0/dt-1.13.1/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.6.0/dt-1.13.1/datatables.min.js"></script>
<script>
$(".table").DataTable();
</script>
Related
if(isset($_GET['buyer_id'])){
$buyer_id = $_GET['buyer_id'];
$buyer_query = mysqli_query($conn, "SELECT * FROM new_entry_table where buyer_id = '$buyer_id'");
if(mysqli_num_rows($buyer_query) > 1){
$i = 1;
while($show = mysqli_fetch_array($buyer_query)){
$v_p_store = $show['per_vihicle_price'];
$store_sum = $v_p_store + #$store_sum;
//**************************************
$vihicle_id = $show['vihicle_id'];
$select_vihicle = mysqli_query($conn, "SELECT * from vihicles where id = '$vihicle_id'");$store_vihicle = mysqli_fetch_array($select_vihicle);
$mine_id = $show['id'];
$select_mines = mysqli_query($conn, "SELECT * from mines where mine_id = '$mine_id'");$store_mine = mysqli_fetch_array($select_mines);
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $show['date']; ?></td>
<td><?php echo $store_vihicle['vihicle_type'] . " " . $store_vihicle['vihicle_no']; ?></td>
<td><?php echo $store_mine['mine_name'] . " # " . $store_mine['mine_no']; ?></td>
<td><?php echo "KG- " . $show['weight']; ?></td>
<td><?php echo "RS- " . $show['price']; ?></td>
<td><?php echo "RS- " . $show['per_vihicle_price']; ?></td>
<td><?php echo "RS- " . $store_sum; ?></td>
<td>0</td>
</tr>
<?php }
}
}
PHP I wanna fetch data from two table like from buyer_record and from hr_recieve_payment.
There are two different tables in database i want to fetch data from them in one single table in a sequence through date or time?
first of all please use prepared statements to avoid SQL injections. please read
as I understand you want to fetch data from vihicles and mines by buyer right?
in that case, you can fetch both this data in SQL where you are getting buyer information
$sql = "
SELECT t.id,v.vihicle_type, m.mine_name, t.weight,t.price, t.per_vihicle_price, t.per_vihicle_price FROM new_entry_table t
left join vihicles v on t.vihicle_id = v.id
left join mines m on m.mine_id=t.id
where t.buyer_id = '{$buyer_id}' order by t.date desc";
Sir My post is for that one buyer purchase something from me on loan iam going to add this loan into table but whenever they pay the loan iwant to show that loanPayed under the purchased prices in the same page.
the database tables are different.
I have at the moment 2 results in my MySQL Database with same user_id and I want echo all in my HTML table with PDO, but it shows everytime only 1 result, not all.
<?php
$querytest = "SELECT o.output_valu,
p.amount,
p.amount_all,
p.order_id,
p.datetime
FROM allusers a
INNER JOIN order_history o
ON a.account_number = o.account_number
INNER JOIN paymentall p
ON o.output_vl_id = p.output_vl_id
WHERE a.account_number = :account_num
ORDER BY p.datetime";
$statementtest = $conn->prepare($queryoutgo);
$statementtest->bindParam(':account_num', $account_num);
$statementtest->execute();
$test_result = $statementtest->fetchAll();
foreach ($test_result as $row) {
$outputtest = $row['output_valu'];
}
?>
<table>
<tr>
<th>Test</th>
</tr>
<tr>
<td><?php echo $outputtest; ?></td>
</tr>
</table>
With print_r($test_result); it shows my 2 results in array, but why not with my code?
I worked always with mysqli not PDO in the past, maybe someone here can help me :)
In your foreach block you overwrite $outputtest every iteration. This means only the last result will be displayed. Depending on if you want to show each result on a separate row or if you want all the results together in one cell, you should either create the cells in the foreach or concatenate all the results together.
EDIT:
What I think you want is this:
$querytest = "SELECT o.output_valu, p.amount, p.amount_all, " .
"p.order_id, p.datetime " .
"FROM allusers a inner join order_history o " .
"ON a.account_number = o.account_number " .
"INNER JOIN paymentall p " .
"ON o.output_vl_id = p.output_vl_id " .
"WHERE a.account_number =:account_num " .
"ORDER BY p.datetime ";
$statementtest = $conn->prepare($queryoutgo);
$statementtest->bindParam(':account_num', $account_num);
$statementtest->execute();
$test_result = $statementtest->fetchAll();
?>
<table>
<tr>
<th>Test</th>
</tr>
<?php foreach($test_result as $row) { ?>
<tr><td><?= $row['output_valu']; ?></td></tr>
<?php } ?>
</table>
I am trying to record the attendance of my students that attend my courses. Courses are different lengths and times and I just need to record if a student is (P)resent (L)ate (A)sent. I record the attendance in 1 table and display the records in a pivot table based on the date attended. I am a newby and just can't workout this code to include all the details I need to show. id, bid, fullname, nickname, company_idno, (P)(L)(A).
Please could someone look at my code and tell me how to add this information to the pivot table.
This is what I want to show
This is where I store the information
This is table1
This is table2
At the moment I achieved the look I want but use 2 tables and use CSS to fix the widths of table 1 and place table 2 next to it.
I realize this is terrible practice and of course, I get odd results across different platforms, especially iOS which put a 47px gap between the 2 tables which I can't seem to remove also.
I want just want table 2 to show all the information. I can only show 3 fields, id, date, pla. How to add fullname, nickname and company_idno ??
Table 1
<table id="tblplanames" >
<td id="tdplabc">sid</td>
<td id="tdplabc">bid</td>
<td id="tdplacid" style="text-align: center">Cid</td>
<td id="tdplafn" style="text-align: center">Fullname</td>
<td id="tdplann" style="text-align: center">Nickname</td>
<?php
$sql13="SELECT * FROM students WHERE classno='$id' ORDER BY bluecard_no ASC ";
$sql_row13=mysqli_query($link,$sql13);
while($sql_res13=mysqli_fetch_assoc($sql_row13)) {
$stsid=$sql_res13["id"];
$stidno=$sql_res13["company_idno"];
$stbluecard_no=$sql_res13["bluecard_no"];
$stfullname=$sql_res13["fullname"];
$stnickname=$sql_res13["nickname"];
?>
<tr>
<td id="tdplabc"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stsid; ?></td>
<td id="tdplabc"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stbluecard_no; ?></td>
<td id="tdplacid"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stidno; ?></td>
<td id="tdplafn"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stfullname; ?></td>
<td id="tdplann"><a href=edit_student.php?id=<?php echo $stsid ?>><?php echo $stnickname; ?></td>
</tr>
<?php
}
?>
</table>
Table 2
<?php
$id = $_GET['id'];
$sql = "SELECT DISTINCT date
FROM attendance
WHERE classno = $id
ORDER BY DATE";
$res = $link->query($sql); // mysqli query
while ($row = $res->fetch_row()) {
$dates[] = $row[0];
}
/***********************************
* Table headings *
************************************/
$emptyRow = array_fill_keys($dates,'');
// format dates
foreach ($dates as $k=>$v) {
$dates[$k] = date('d-M', strtotime($v));
}
$heads = "<table id='tblpla'>\n";
$heads .= "<tr><td>sid</td><td>" . join('</td><td>', $dates) . "</td></tr>\n";
/***********************************
* Main data *
************************************/
$sql = "SELECT date, sid, pla, bluecard_no
FROM attendance
WHERE classno = $id
ORDER BY bluecard_no";
$res = $link->query($sql);
$sid='';
$tdata = '';
while (list($d, $sn, $s, $bcn) = $res->fetch_row()) {
if ($sid != $sn) {
if ($sid) {
$tdata .= "<tr><td>$sid</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$sid = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$sid</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$tdata .= "</table\n";
echo $heads;
echo $tdata;
?>
SELECT c.olumns
, y.ou
, a.ctually
, w.ant
FROM students s
JOIN attendance a
ON a.classno = s.classno
WHERE s.classno = :id
ORDER
BY s.bluecard_no ASC
, a.date";
public function get_members_for_attendence()
{
$date = $this->input->post('choose_date');
$sql = 'SELECT a.date, c.member_name,c.member_join_date,c.member_payment_date,c.member_exp_date,c.member_payment_date,c.member_contact, c.member_reg_id,
CASE
WHEN a.status = "absent" THEN "Absent"
WHEN a.status = "present" THEN "Present"
ELSE "Not Taken"
END as attendence_status
FROM member_reg AS c
LEFT JOIN attendence AS a ON a.member_reg_id = c.member_reg_id AND a.date = "'.$date.'"';
$query = $this->db->query($sql);
return $query->result();
}
For CodeIgniter 3
it worked for me
I have a table with a bunch of users who have a certain amount of points. I would like to arrange the users from highest points first to the lowest. However ORDER BY PTS DESC doesn't work.
<tr>
<th id="users_th1"><img src="<?php echo mysql_result($r_TEAMS, $i, 'LOGO'); ?>"/> <p><?php echo mysql_result($r_TEAMS, $i, 'NAME'); ?></p></th>
<th id="users_th2">Points Value</th>
</tr>
<?php
$q_users = 'Select * from POINTS LEFT JOIN USERS on USERS.UID = POINTS.UID where TID = '.mysql_result($r_TEAMS, $i, 'TID');
$r_users = mysql_query($q_users, $connection) or die(mysql_error());
$n_users = mysql_num_rows($r_users);
for($k = 0; $k <$n_users; $k++){
?>
<tr>
<td class="person"><?php echo mysql_result($r_users, $k, 'NAME'); ?></td>
<td><?php echo mysql_result($r_users, $k, 'POINTS.PTS'); ?></td>
</tr>
<?php
}
}
This is just guesswork, but I see you're doing a JOIN between the table USER and the table POINTS. Perhaps you have a field called PTS in both tables, so if you want to order the results by that field you should indicate to which table the one you're referring to belongs.
So, do it this way,
$q_users = "
SELECT *
FROM POINTS
LEFT JOIN USERS
ON USERS.UID = POINTS.UID
WHERE <table name>.TID = " . mysql_result($r_TEAMS, $i, 'TID') . "
ORDER BY <table name>.PTS DESC";
Did you try:
$q_users = 'Select * from POINTS LEFT JOIN USERS on USERS.UID = POINTS.UID where TID = '.mysql_result($r_TEAMS, $i, 'TID').' ORDER BY PTS DESC;';
You could also write if it won't sort or if it won't query.
I have a table which has a field isClaimed that has only two fixed values = CLAIMED or NOT CLAIMED. I have to calculate the total of each field.
FYI, assume this is my table:
name | isClaimed
Aye | NOT CLAIMED
Ian | CLAIMED
Jan | NOT CLAIMED
Zen | NOT CLAIMED
Pom | CLAIMED
Total of unclaimed: 3
Total of claimed: 2
And please check my code below:
<?php
$sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending
$result2 = mysql_query($sql);
if($result2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {
$firstname = $row2["Firstname"];
$lastname = $row2["Lastname"];
$middlename = $row2["Middlename"];
$barangay = $row2["BarangayName"];
$level = $row2["LevelName"];
$allowance = $row2["Allowance"];
$isClaimed = $row2["isClaimed"];
?>
<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?> </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>
<?php
// Exit looping
}
?>
<tr>
<td colspan="4" class="spec">Total of unclaimed allowances</td>
<td></td>
</tr>
<tr>
<td colspan="4" class="spec">Total of claimed allowances</td>
<td></td>
</tr>
I have tried the tutorial from here: http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/
But i can't get it to work in php.
From the tutorial you linked....
$sql = "SELECT
SUM(IF(sca.isClaimed = "CLAIMED", 1,0)) AS claimedTotal,
SUM(IF(sca.isClaimed = "NOT CLAIMED", 1,0)) AS notClaimedTotal,
pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName,
school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId
ORDER BY pro.LevelId, pro.ScholarId";
And then
echo $row2["claimedTotal"];
and
echo $row2["notClaimedTotal"];
Note that I used the table sca for for the isClaimed value, just a guess...not sure of your table structure, maybe you will need to change sca to reflect the correct table.
<?php
$claimedCount = 0;
$unclaimedCount= 0;
$sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending
$result2 = mysql_query($sql);
if($result2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {
$firstname = $row2["Firstname"];
$lastname = $row2["Lastname"];
$middlename = $row2["Middlename"];
$barangay = $row2["BarangayName"];
$level = $row2["LevelName"];
$allowance = $row2["Allowance"];
$isClaimed = $row2["isClaimed"];
?>
<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?> </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>
<?php
if($row2["isClaimed"] == "CLAIMED")
$claimedCount++;
elseif($row2["isClaimed"] == "NOT CLAIMED")
$unclaimedCount++;
// Exit looping
}
?>
<tr>
<td colspan="4" class="spec">Total of unclaimed allowances</td>
<td><?php echo $unclaimedCount;?></td>
</tr>
<tr>
<td colspan="4" class="spec">Total of claimed allowances</td>
<td><?php echo $claimedCount;?></td>
</tr>
Note: I have not checked you query. I have just mentioned my suggestion about getting the count that suits your current structure. Moreover, its highly recommended to start using mysqli_* instead of mysql.