find rank from mysql database

Discussion in 'PHP' started by saad_sinpk, Oct 4, 2011.

  1. #1
    i have make some game script i need some help in it, i want that it show rank here is my table of game and user


    --
    -- Table structure for table `join_survivor`
    --

    CREATE TABLE IF NOT EXISTS `join_survivor` (
    `join_id` int(11) NOT NULL AUTO_INCREMENT,
    `join_user` int(11) DEFAULT NULL,
    `join_pool` int(11) DEFAULT NULL,
    `join_event` int(11) DEFAULT NULL,
    `join_win` int(11) DEFAULT NULL,
    `join_pick` int(11) DEFAULT NULL,
    `join_fight` int(11) DEFAULT NULL,
    `join_done` int(11) NOT NULL,
    `join_result_win` int(11) NOT NULL,
    `join_result_lost` int(11) NOT NULL,
    `join_curr_round` int(11) NOT NULL,
    `join_round_method` int(11) NOT NULL,
    `join_result_method` int(11) NOT NULL,
    `end_event` int(11) NOT NULL,
    `join_type` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
    `join_cur_point` int(11) NOT NULL,
    `join_old_point` int(11) NOT NULL,
    `join_fin_round` int(11) NOT NULL,
    `join_event_next` int(11) NOT NULL,
    PRIMARY KEY (`join_id`),
    UNIQUE KEY `join_id` (`join_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2317 ;

    --
    -- Dumping data for table `join_survivor`
    --

    INSERT INTO `join_survivor` (`join_id`, `join_user`, `join_pool`, `join_event`, `join_win`, `join_pick`, `join_fight`, `join_done`, `join_result_win`, `join_result_lost`, `join_curr_round`, `join_round_method`, `join_result_method`, `end_event`, `join_type`, `join_cur_point`, `join_old_point`, `join_fin_round`, `join_event_next`) VALUES
    (1060, 2, 331, 16, 70, 1, 59, 1, 70, 69, 0, 0, 1, 0, 'survivor', 0, 0, 2, 0);


    i will only use this 2 command in WHERE situation join_user = 2 AND join_pool = $idvar
    if this situation meet then it should show rank and rank order like who has more join_cur_point

    i try alot to find problem but i could not find it please help me out and find this problem solution
     
    saad_sinpk, Oct 4, 2011 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    You query should be from what I can tell your after is:
    
    SELECT * join_survivor WHERE join_user = 2 AND join_pool = '$idvar'
    
    Code (markup):
    Looking at your database setup, I guess your using realtionalship databases to cross reference user ids or something similar?
     
    HuggyEssex, Oct 4, 2011 IP
  3. saad_sinpk

    saad_sinpk Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes friend i am using realtionalship database, but i need to display that if id user 2 have more point then id user 3 then it will display like this
    rank 1 for id user 2
    rank 2 for id user 3
    and if 3 have more point then user 2 then 3 will become number 1
     
    saad_sinpk, Oct 5, 2011 IP
  4. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #4
    For this you need to have 2 tables
    1. user info table where user_id should be primary key
    2. Game info table where u will be storing game_id, user1_ID, user2_ID,user1_pont,user2_point,time to start game,current time,status of game(playing,over) other info related to game.

    when game status is over in table 2
    then get for both userd point through one query
    and compare them then display your result based on that.

    Sheetal
     
    SheetalCreation, Oct 5, 2011 IP
  5. saad_sinpk

    saad_sinpk Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i have all this in only one table join_survivor and if for example i use 2 table then how will i show rank i want to show rank of unlimited user and there is only one thing, and one round,
     
    saad_sinpk, Oct 6, 2011 IP
  6. saad_sinpk

    saad_sinpk Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i find this code from internet
    function getUserRank($userId){
            $sql1       = "SET @rownum := 0";
     
            $sql2       =   "SELECT rank, correct FROM (
                            SELECT @rownum := @rownum + 1 AS rank, correct, uid
                            FROM quiz_user ORDER BY correct DESC
                            ) as result WHERE uid=$uid";
     
            // here model.php is a class for database connectivity
            include_once "model.php";
            global $dbconfig; //configuration of database that I assigned in config file
            $md = new Model($dbconfig);
     
            $md->connectDb();
            mysql_query($sql1); /*as mysql_query function can execute one query at a time */
            $result = mysql_query($sql2);
            $rows = '';
            $data = array();
            if (!empty($result))
                $rows      =  mysql_num_rows($result);
            else
                $rows      =  '';
     
            if (!empty($rows)){
                while ($rows = mysql_fetch_assoc($result)){
                    $data[]   = $rows;
                }
            }
     
            $md->closeDb();
     
            //rank of the user
            if (empty($data[0]['rank']))
                return 1;
            return $data[0]['rank'];
    }
    
    Code (markup):
    and i use like this
    function getUserRank($userId){
            $sql1       = "SET @rownum := 0";
     
            $sql2       =   "SELECT rank, correct FROM (
                            SELECT @rownum := @rownum + 1 AS rank, correct, uid
                            FROM quiz_user ORDER BY correct DESC
                            ) as result WHERE uid=$uid";
     
            // here model.php is a class for database connectivity
            include_once "model.php";
            global $dbconfig; //configuration of database that I assigned in config file
    
            mysql_query($sql1); /*as mysql_query function can execute one query at a time */
            $result = mysql_query($sql2);
            $rows = '';
            $data = array();
            if (!empty($result))
                $rows      =  mysql_num_rows($result);
            else
                $rows      =  '';
     
            if (!empty($rows)){
                while ($rows = mysql_fetch_assoc($result)){
                    $data[]   = $rows;
                }
            }
     
     
            //rank of the user
            if (empty($data[0]['rank']))
                return 1;
            return $data[0]['rank'];
    }
    getUserRank;
    Code (markup):
    i have change table also simple table to explain u guys

    CREATE TABLE IF NOT EXISTS `quiz_user` (
    `uid` BIGINT UNSIGNED NOT NULL ,
    `participated` SMALLINT UNSIGNED NULL DEFAULT 0 ,
    `correct` SMALLINT UNSIGNED NULL DEFAULT 0 ,
    `wrong` SMALLINT UNSIGNED NULL DEFAULT 0 ,
    `created` DATETIME NULL ,
    `updated` DATETIME NULL ,
    PRIMARY KEY (`uid`) )
    ENGINE = InnoDB

    but still i am getting error

    Fatal error: Cannot redeclare getuserrank() (previously declared in /functions.php:84) in /file.php on line 231
     
    saad_sinpk, Oct 8, 2011 IP