This new code works for me, but not quite, the problem now is that all users have the sameness ranking, all have 1°
<?php
mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die(mysql_error());
mysql_select_db($mysql_db) or die(mysql_error());
$sql = "SELECT ID, Name, username, Wins, Loses, Draws, (Wins + Loses) AS points FROM users WHERE username='$_GET[user]' ORDER BY points DESC";
$result = mysql_query($sql) or die(mysql_error());
if( !$result ){
echo 'SQL Query Failed';
}else{
$rank = 0;
$last_score = false;
$rows = 0;
while( $row = mysql_fetch_array( $result ) ){
$rows++;
if( $last_score!= $row['Wins'] ){
$last_score = $row['Wins'];
$rank = $rows;
}
echo "rank ".$rank." is ".$row['Name']." with point ".$row['Wins']."";
}
}
?>
I NEED
Example:
ID username Wins Loses
1 demo 12 12 RANKIG= 1°
2 demo2 1 3 RANKIG= 2°
Try your query as:
$result=mysql_query("SELECT ID, Name, username, Wins, Loses, Draws, (Wins + Loses) AS points, count(points) AS Ranking FROM `users` WHERE username='$_GET[user]' ORDER BY CONVERT(points, INT) DESC");
Related
I use this code below to determine the position of each student result based on the percentage score of their subject, the program assigned their position as (1st, 2nd, 2nd, 4th ...)
My challenge now is that whenever the percentage score is 100, the system position the student as last in the class instead of 1st position.
Below is the code:
<?php
session_start();
include("DB/config.php");
ini_set('max_execution_time', 1000000);
$class_arm= $_SESSION['clas'];
$queris = "select * from setup";
$result3 = mysqli_query($con,$queris) or die(mysqli_error($con));
$rws3 = mysqli_fetch_assoc($result3);
$ses3=$rws3['section'];
$term3=$rws3['term'];
$query_sj = "SELECT subj FROM sec_result where class='$class_arm' and year='$ses3' and term='$term3' and tsc<>'' and tsc is not null and tsc <> '0'";
$result_sj = mysqli_query($con,$query_sj) or die(mysqli_error($con));
while($row_sj = mysqli_fetch_array($result_sj)){
$query = "SELECT distinct studid,subj,tsc FROM sec_result where class='$class_arm' and year='$ses3' and term='$term3' and subj='".$row_sj['subj']."' ORDER BY tsc DESC";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
/*$row2 = mysqli_fetch_array( $result );
echo $row2['tsc'];
exit;*/
if( !$result ){
echo 'SQL Query Failed';
}else{
$rank = 0;
$last_score = false;
$rows = 0;
while( $row = mysqli_fetch_array( $result ) ){
$rows++;
if( $last_score!= $row['tsc'] ){
$last_score = $row['tsc'];
$nuval = $row['tsc'];
$rank = $rows;
}
mysqli_query($con,"UPDATE sec_result SET pos = '$rank' where studid='".$row['studid']."' and year='$ses3' and term='$term3' and subj='".$row_sj['subj']."'") or die(mysqli_error($con));
}
}
}
?>
My expectation is that the student that scores 100 percent should be given the first position.
Please, I need your help to resolve this, Thanks.
My expectation is that the student that scores 100 percent should be given the first position.
I need to save the count(activity_type) group by user_posted_to into a variable from mysql query
$query = "
SELECT user_posted_to, COUNT(*)
FROM activity_log_table
WHERE
post_type = 'discussion'
AND activity_type = 'Like' ";
$query .= "AND activity_timestamp >= CURRENT_DATE - INTERVAL 7 DAY AND activity_type <= CURRENT_DATE ";
$query .= "GROUP BY user_posted_to ORDER BY activity_timestamp DESC LIMIT 25";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$user_posted_to = (int)$row['user_posted_to'];
$timestamp = time();
// I need to insert the count into this table for number_assert
$query2 = "INSERT INTO top_weekly (user_id, number_assert, timestamp) VALUES ($user_posted_to, $timestamp)";
$result2 = mysqli_query($connection, $query2);
$confirm_query2 = confirm_query($result2);
if($confirm_query2) {
echo "success query 2";
} else {
echo "failed query 2";
}
}
i expect to save the count() group by into a php variable and to be able to use it later on the page
You want to alias the « COUNT(*) » to something else, like « cnt ». Then you can access the column by name after fetching, as demonstrated below :
$query = "
SELECT
user_posted_to,
COUNT(*) as cnt
FROM activity_log_table
WHERE
post_type = 'discussion'
AND activity_type = 'Like'
AND activity_timestamp >= CURRENT_DATE - INTERVAL 7 DAY
AND activity_type <= CURRENT_DATE
GROUP BY user_posted_to
ORDER BY activity_timestamp DESC
LIMIT 25";
$result = mysqli_query($connection, $query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "user_posted_to: " . $row["user_posted_to"]. " - Count: " . $row["cnt"] . "<br>";
}
} else {
echo "0 results";
}
Add and AS label to the COUNT(*) then you will be able to readout that label in the row you get.
$query = "SELECT user_posted_to, COUNT(*) AS varCount FROM activity_log_table WHERE post_type = 'discussion' AND activity_type = 'Like' ";
$nummerOfCount = $row['varCount']
I'm working on a project where I have to show the leaderboard of players. But when wrote the below code, It shows the ranks with 2, 4, 6, 8, ... But not the odd numbered ranks. Can anyone tell me "What's wrong in this ?"
$query1_string = "CREATE VIEW Leaderboard AS SELECT Name, Points, PhoneNo
FROM user ORDER BY Points DESC";
$query2_string = "set #rank = 0";
$query3_string = "SELECT #rank := #rank + 1 as Rank, Name, Points
FROM Leaderboard";
$query5_string = "DROP VIEW Leaderboard";
// Doing the queries
$query1 = mysqli_query($con, $query1_string) or die(mysqli_error($con));
$query2 = mysqli_query($con, $query2_string) or die(mysqli_error($con));
$query3 = mysqli_query($con, $query3_string) or die(mysqli_error($con));
// Initializing the count
$count = 0;
//Making an array of strings including Rank, Name and Points of the Top 5 Players
while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM))) {
$row = mysqli_fetch_array($query3, MYSQL_NUM);
$results[$count] = $row[0] . " " . $row[1] . " " . $row[2];
$count++;
}
// Dropping the view.
$end_query = mysqli_query($con, $query5_string) or die(mysqli_error($con));
//Returning the array
$leader = implode("\n", $results);
echo $leader;
You are executing the query3 two times and displying the data retrieved from second execution
while (($count < 5) && (mysqli_fetch_array($query3, MYSQL_NUM))) {
$row = mysqli_fetch_array($query3, MYSQL_NUM);
I'm trying to design a PHP browser game just for fun and practice. I'm currently working on the combat script and I encountered an endless loop. I can't figure out the reason why it's happening. Anyways, here's the code:
<?php
session_start();
// Script PHP pentru simularea luptelor single player
/*
1 = Magic
2 = Attack
3 = Defence
5 = Maximum HP
6 = Current HP
4 = Gold coins
17 = Experience
*/
require_once("config.php");
$username = $_SESSION['username'];
$query = ("SELECT id FROM users WHERE username = '$username'");
$user_id = mysql_query($query) or die (mysql_error());
// Momentan monstrii sunt alesi in mod aleatoriu
$query = ("SELECT id FROM monsters ORDER BY RAND() LIMIT 1");
$monster_id = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM user_stats WHERE stat_id = 6 AND user_id = '$user_id'");
$player_hp = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 5 AND monster_id = '$monster_id'");
$monster_hp = mysql_query($query) or die (mysql_error());
if ($player_hp <= 0)
{
$query = ("SELECT value FROM user_stats WHERE stat_id = 4 AND user_id = '$user_id'");
$player_gc = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 4 AND user_id = '$monster_id'");
$monster_gc = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM user_stats WHERE stat_id = 17 AND user_id = '$monster_id'");
$player_exp = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 17 AND monster_id = '$monster_id'");
$monster_exp = mysql_query($query) or die (mysql_error());
$player_gc = $player_gc + $monster_gc;
$player_exp = $player_exp + $monster_exp;
// item drop trebuie facut
echo "Congratulations! You won the battle and you gained " . $monster_gc . " gold coins and " . $monster_exp . " experience points.";
}
elseif ($monster_hp <= 0)
{
echo "You lost the battle.";
}
else
{
$query = ("SELECT value FROM user_stats WHERE stat_id = 1 AND user_id = '$user_id'");
$player_magic = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 1 AND monster_id = '$monster_id'");
$monster_magic = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM user_stats WHERE stat_id = 2 AND user_id = '$user_id'");
$player_attack = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 2 AND monster_id = '$monster_id'");
$monster_attack = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM user_stats WHERE stat_id = 3 AND user_id = '$user_id'");
$player_defence = mysql_query($query) or die (mysql_error());
$query = ("SELECT value FROM monster_stats WHERE stat_id = 3 AND monster_id = '$monster_id'");
$monster_defence = mysql_query($query) or die (mysql_error());
$turn = rand(0,1);
while (($player_hp > 0) && ($monster_hp > 0) &&($turn < 6))
{
$turn++;
// Player turn
if ($turn % 2 == 0)
{
echo $monster_hp . "<br>";
$monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
var_dump ($monster_hp);
}
// Monster turn
else
{
echo $player_hp . "<br>";
$player_hp -= ($monster_attack + $monster_magic / 2 - $player_defence - $player_magic / 2);
var_dump ($player_hp);
}
}
}
?>
The while is down in the else. I know that MySQL is depreciated. Thanks!
Are you after this (operator is -= not -):
// Player turn
if ($turn % 2 == 0)
{
$monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
}
// Monster turn
else
{
$player_hp -= ($monster_attack + $monster_magic / 2 - $player_defence - $player_magic / 2);
}
-edit- if your variable is Resource id #5 etc, it means you haven't correctly retried your MySQL data from PHP. Try this:
$query = ("SELECT value FROM user_stats WHERE stat_id = 3 AND user_id = '$user_id'");
$player_defence = mysql_query($query) or die (mysql_error());
$player_defence = mysql_fetch_assoc($player_defence);
$player_defence = $player_defence['value'];
You'll need to do that (or something similar) for every query result you're getting. It converts your query result to an associate array then to the variable you need.
Manual: http://php.net/manual/en/function.mysql-fetch-assoc.php
In your while loop you never actually CHANGE the HPs of the monsters and players:
$monster_hp - ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
Did you mean to use something like this instead:
$monster_hp -= ($player_attack + $player_magic / 2 - $monster_defence - $monster_magic / 2);
The -= operator is really a quick way of programming equals itself minus...:
// The following lines are identical in logic:
$var-=1
$var=$var-1;
Good day,
As mention in the topic, I'm creating a ajax function where the php will directly update the status then if the status is 1 (or approve), it will compare between 2 table (tblcompany and tblinternapplication) and doing insert new company if the company not in the list. I tried test one by one it function well but after combine it doesn't add any new company when the person application approved (or set to 1) even the status in tblinternapplication updated. Below is my code.
<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>
<?php
$id = $_GET['id'];
$status =$_GET['status'];
$sql="UPDATE tblinternapplication set status_approval =
".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
$result = mysql_query($sql);
$querysel = "SELECT i.company_code, c.company_name as cn, i.company_name as ic,
c.company_branch as cb, i.company_branch as ib, FROM tblcompany c,
tblinternapplication i WHERE i.id = '$id' ";
$resultsel = mysql_query($querysel, $connection);
$queryselc = "SELECT
company_name, company_branch,
company_address, post_code,
company_city, company_state,
company_country,
company_phone, company_fax,
company_url FROM tblinternapplication WHERE id = '$id' ";
$resultselc = mysql_query($queryselc, $connection);
if ($status == 1){
while($rowsel = mysql_fetch_array($resultsel)){
if($rowsel['company_code'] == NULL){
if(($rowsel['cn'] != $rowsel['ic']) OR ($rowsel['ib'] != $rowsel['cb'])){
while($rowselc = mysql_fetch_array($resultselc)){
$query = "INSERT INTO tblcompany (
company_name, company_branch,
company_address, post_code,
company_city, company_state, company_country,
company_phone, company_fax,
company_url
) VALUES (
'{$rowselc['company_name']}', '{$rowselc['company_branch']}',
'{$rowselc['company_address']}','{$rowselc['post_code']}',
'{$rowselc['company_city']}','{$rowselc['company_state']}',
'{$rowselc['company_country']}',
'{$rowselc['company_phone']}','{$rowselc['company_fax']}',
'{$rowselc['company_url']}'
)";
$resultc = mysql_query($query, $connection);
}
}
}
}
}
?>
Just to share the answer using my own method. Basically I remove 2-level nested while and make the first query row match then the second is to search for result. Hope this will help others.
<?php
$id = $_GET['id'];
$status = $_GET['status'];
$sql="UPDATE tblinternapplication set status_approval =
".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
$result = mysql_query($sql);
$querysel = "SELECT i.company_code, i.company_name, i.company_branch, c.company_name,
c.company_branch FROM tblinternapplication i, tblcompany c WHERE i.company_name =
c.company_name AND i.company_branch = c.company_branch AND i.id = '$id' ";
$resultsel = mysql_query($querysel, $connection);
$queryselc = "SELECT * FROM tblinternapplication where id = '$id'";
$resultselc = mysql_query($queryselc, $connection);
if ($status == 1){
if(mysql_num_rows($resultsel) == 0){
while($rowselc = mysql_fetch_array($resultselc)){
$query = "INSERT INTO tblcompany (
company_name, company_branch,
company_address, post_code,
company_city, company_state, company_country,
company_phone, company_fax,
company_url
) VALUES (
'{$rowselc['company_name']}', '{$rowselc['company_branch']}',
'{$rowselc['company_address']}','{$rowselc['post_code']}',
'{$rowselc['company_city']}','{$rowselc['company_state']}',
'{$rowselc['company_country']}',
'{$rowselc['company_phone']}','{$rowselc['company_fax']}',
'{$rowselc['company_url']}'
)";
$resultc = mysql_query($query, $connection);
}
}
}
?>
if anyone have recommendation welcome to leave comments.
Thank you.