I was suggested to ask this question again but with more depth.
Here is my script:
<?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>
With this script I want to be able to show the logged user the place that they currently stand in the contest under the current winners.
This is what I want the table to look like.
-----------------------------------------
| Place | User | Prize | Completed |
| 1 | Someuser1 | $5.00 | 5 |
| 2 | Someuser2 | $2.50 | 3 |
| 3 | Someuser3 | $1.25 | 2 |
| 20 | You | --- | 1 |
-----------------------------------------
This is how it looks
-----------------------------------------
| Place | User | Prize | Completed |
| 1 | Someuser1 | $5.00 | 5 |
| 2 | Someuser2 | $2.50 | 3 |
| 3 | Someuser3 | $1.25 | 2 |
| 1 | You | --- | 1 |
-----------------------------------------
here is my table structure.
Column | Type | Null | Default
--------------------------------------
id |int(11)| No |
username |text | No |
completed|int(11)| No |
As you can see it is all coming from one table and the place isn’t defined by the database, but by the script itself.
Hopefully this can clarify more than my last question.
Edit: With Sean’s code this is what I get.
-----------------------------------------
| 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
-----------------------------------------
This can be done with a nested query –
Here is how the query works from the inside out-
The 1st (inside)
SELECTgets thecompletedamount for theusername =$ui['username']The 2nd (middle)
SELECTuses thatcompletedamount and does acountof all the rows that have more completed, adds a1to thatcount, and saves it as the usersPlace.The 3rd/Last (outside)
SELECTnow just gets the row data forusername =$ui['username']Edit
To add a
tiebreakerwhen one or more have the same numbercompleted, you need to order your query by theidas well. So change your first query to –And add
ORDER BY idand>=to the 2nd queryEdit #2
Try this new query. The previous one was not selecting the exact row, but the last of the
tied.This new query creates a temporary new column
Place, usingSELECT @Place := 0, and then we get the users ‘Place’ using@Place:=@Place+1 AS Place.