计算比赛脚本中的位置

时间:2012-11-25 02:17:41

标签: php mysql

我被建议再次提出这个问题,但更深入。

这是我的剧本:

    <?php
    //Loggedin
    if($_SESSION['login']!=1)
    {
        print "You must be logged in.";
        include($root . 'footer.php');
        exit;
    }
    //Check banned account
    elseif($ui['level']=="2"){
        print "Sorry but your account is banned.";
        include($root . 'footer.php');
        exit;
    }
    //Check email verified
    elseif($ui['email_check']=="0"){
        print "Sorry but your account has not been verified, to verify your account now please visit <a href='index.php?index=verify&email=".$ui['email']."'>THIS LINK</a>.";
        include($root . 'footer.php');
        exit;
    }
date_default_timezone_set('America/New_York');
$country= $ui['country'];
$dates=mysql_query("SELECT * FROM `contest` WHERE `countries` LIKE '%$country%'");
$timestamp = time();

    $getcontests = $os_DB->query("SELECT * FROM contest WHERE date_1 <= '$timestamp' AND date_2 >= '$timestamp' AND countries LIKE '%$country%'");
        $num = $os_DB->num($getcontests);

            if($num == 0){
            print"<td colspan='4'>There are currently no active contests</td>";
            }
            else
            {

    while ($dat = mysql_fetch_array($dates)) {
        $tname = preg_replace('/\s+/', '', $dat['name']);
        $places="(SELECT * FROM `".$tname."_contest` WHERE `username` <> 'cassa' ORDER BY `completed` DESC LIMIT ".$dat['rewards'].")";
        $results=mysql_query($places) or die(mysql_error());




        $reward = array("".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4'].",".$dat['reward_5'].",".$dat['reward_6'].",".$dat['reward_7'].",".$dat['reward_8'].",".$dat['reward_9'].",".$dat['reward_10']."");

        $rewards = implode(",", $reward);

        $rewardsa = explode(",", $rewards);
        $i=0;
        $a=1;

            // Offers Contest

        if(time() <= $dat['date_2'] && time() >= $dat['date_1'] && $dat['type'] == offer) {
            print" <table width ='100%'><tr><th align='center'><font size='4'>{$dat['name']}</font></th><th align='right'><font size='1'>".date("m/d/Y h:i A", $dat['date_1'])."-".date("m/d/Y h:i A", $dat['date_2'])."</font></th></tr></table><br />".$dat['desc']."<br /><font size='1' color='white'>You must complete offers worth at least ".$dat['min_points']." points or $".$dat['min_cash']." to count towards contest!<br /><br />
            You must also complete at least ".$dat['min_offers']." offers in order to be eligible for winnings.</font><br /><br />";
            print" <table width ='100%'><tr><th align='left'>Place</th><th align='center'>User</th><th align='right'>Prize</th><th align='right'>Completed</th></tr>";

        if(mysql_num_rows($results) == 0){
            foreach($rewardsa as $rewa){
                if(!empty($rewa['$i'])){
                    if($dat['r_type'] == points){
                        print"  <tr><td align='left'>{$a}</td><td align='center'>......</td><td align='right'>{$rewardsa[$i]} points</td><td align='right'>--</td></tr>";
                    }
                if($dat['r_type'] == cash){
                    print"  <tr><td align='left'>{$a}</td><td align='center'>......</td><td align='right'>$".$rewardsa[$i]."</td><td align='right'>--</td></tr>";
                }
            $i++;
            $a++;
                }
            }
        }


    while ($place = mysql_fetch_array($results)) {
        if($dat['r_type'] == points){
            print"  <tr><td align='left'>{$a}</td><td align='center'>{$place['username']}</td><td align='right'>{$rewardsa[$i]} points</td><td align='right'>{$place['completed']}</td></tr>";
        }
        if($dat['r_type'] == cash){
            print"  <tr><td align='left'>{$a}</td><td align='center'>{$place['username']}</td><td align='right'>$".$rewardsa[$i]."</td><td align='right'>{$place['completed']}</td></tr>";
        }
        $i++;
        $a++;

    }
    ///Line I am working with///
    $getyou= mysql_query("SELECT COUNT(*) AS Place, t.*
    FROM ".$tname."_contest t
    GROUP BY t.id
    HAVING Place <= 3 OR username = '".$ui['username']."'");
        $youu = mysql_fetch_array($getyou);
        print"  <tr><td align='left'>{$youu['Place']}</td><td align='center'>You</td><td align='right'>---</td><td align='right'>{$youu['completed']}</td></tr>";
        }
    }
        }

?>  
        </table>

使用此脚本,我希望能够向已登录的用户显示他们目前在当前获奖者的竞赛中所处的位置。

这就是我想要的表格。

-----------------------------------------
| Place |    User   | Prize | Completed |
|   1   | Someuser1 | $5.00 |     5     |
|   2   | Someuser2 | $2.50 |     3     |
|   3   | Someuser3 | $1.25 |     2     |
|   20  |    You    |  ---  |     1     |
-----------------------------------------

这就是它的外观

-----------------------------------------
| Place |    User   | Prize | Completed |
|   1   | Someuser1 | $5.00 |     5     |
|   2   | Someuser2 | $2.50 |     3     |
|   3   | Someuser3 | $1.25 |     2     |
|   1   |    You    |  ---  |     1     |
-----------------------------------------

这是我的表结构。

    Column   |  Type | Null |  Default
--------------------------------------
    id       |int(11)|  No  |   
    username |text   |  No  |   
    completed|int(11)|  No  |   

正如您所看到的,它全部来自一个表,并且该位置不是由数据库定义的,而是由脚本本身定义的。

希望这可以澄清超过我的上一个问题。

编辑:使用肖恩的代码,这就是我得到的。

-----------------------------------------
| Place |    User    | Prize | Completed |
|   1   |   kikkat   | $5.00 |     1     |
|   2   |xXchris744Xx| $2.50 |     1     |
|   3   |  kira423   | $1.25 |     1     | /// This line is me
|   7   |    You     |  ---  |     1     | /// But it shows my current place as 7
-----------------------------------------

1 个答案:

答案 0 :(得分:1)

这可以通过嵌套查询来完成 -

$getyou= mysql_query("SELECT 
                        (SELECT count(*)+1 AS rank FROM contest WHERE completed >
                          (SELECT completed FROM contest WHERE username = '".$ui['username']."' ORDER BY completed DESC LIMIT 1)) as Place,
                        c.* FROM contest c WHERE username = '".$ui['username']."'");
$youu = mysql_fetch_array($getyou);
    print"  <tr><td align='left'>{$youu['Place']}</td><td align='center'>You</td><td align='right'>---</td><td align='right'>{$youu['completed']}</td></tr>";

以下是查询如何从内到外工作 -

第一个(内部)SELECT获得completed

username =$ui['username']金额
SELECT completed FROM contest WHERE username = '".$ui['username']."' ORDER BY completed DESC LIMIT 1

第二个(中间)SELECT使用completed金额并执行已完成更多行的所有行count,向{{1}添加1 },并将其另存为用户count

Place

第3个/最后一个(外部)SELECT count(*)+1 AS rank FROM contest WHERE completed > '#' // # represents the `completed` amount for the `$ui['username']` that we got in the 1st SELECT 现在只获取SELECT

的行数据
username =$ui['username']

编辑

要在一个或多个具有相同号码SELECT Place, c.* FROM contest c WHERE username = '".$ui['username']."' // Place was created/defined in early SELECTS, now we just get the rest of the data using c.* 时添加tiebreaker,您还需要按completed订购查询。因此,将您的第一个查询更改为 -

id

$places="(SELECT * FROM `".$tname."_contest` WHERE `username` <> 'cassa' ORDER BY `completed`,`id` DESC LIMIT ".$dat['rewards'].")"; --- ORDER BY id添加到第二个查询

>=

编辑#2 试试这个新查询。前一个没有选择确切的行,而是$getyou= mysql_query("SELECT (SELECT count(*)+1 AS rank FROM contest WHERE completed >= -- (SELECT completed FROM contest WHERE username = '".$ui['username']."' ORDER BY completed DESC LIMIT 1) ORDER BY `completed`,`id`) as Place, ------------------------- c.* FROM contest c WHERE username = '".$ui['username']."'"); 的最后一个。

tied

此新查询使用$getyou= mysql_query("SELECT Place, c.* FROM (SELECT @Place:=@Place+1 AS Place, c.* FROM contest c, (SELECT @Place := 0) r ORDER BY completed DESC, id ASC ) c WHERE username = '".$ui['username']."'"); 创建临时新列Place,然后使用SELECT @Place := 0获取用户“地点”。