按地点奖励不同金额(PHP)

时间:2012-04-26 21:45:16

标签: php database arrays foreach

我要做的是为GPT网站创建比赛脚本,我希望能够使用数据库中定义的不同金额奖励获胜者。例如,第一个位置将得到reward_1在数据库中的内容,依此类推。所以我想尝试将其发送到mysql_query("UPDATE members SET points=points+'$reward' WHERE username='$username'")的位置,但我需要$reward为每个获胜用户提供不同的信息。

澄清
当这个特定的脚本运行时,如果日期是> =比赛的结束日期(在代码中它说< =因为如果不是我不能测试它),它会向用户发放奖励)但基本上当它给出奖励时我希望能够使用1个查询来执行此操作,但我希望该查询能够向正确的用户提供正确的奖励。例如,完成最多优惠的用户应该获得竞赛表中的奖励_1表示哪个奖励为1000,第二个地方的用户将获得奖励列出的行奖励_2,如500.我现在设置的方式它会给所有用户提供相同的奖励金额,而不是每个用户的金额不同。我希望有所帮助!

以下是我的代码(请注意,我还没有添加任何功能来尝试执行此操作,因为我不知道从哪里开始)

更新
我正在使用的是在所有被阻止的行之后,奖励金额将来自dbtable比赛。

我不确定我是否已经给出了足够的描述我想要获得的内容如果我需要澄清任何事情请告诉我,所有的帮助表示赞赏!

好吧所以我一直在研究这个问题,尝试使用数组,请原谅这种邋iness,因为我只是想让事情按照我需要的方式运行!这是新代码:

<?
include_once"config.php";
date_default_timezone_set('UTC');

$query= "SELECT * FROM raffle WHERE amount>='1' ORDER BY Rand() LIMIT 2";
$result = mysql_query($query);
$raff=mysql_query("SELECT SUM(amount) FROM raffle");
$raffle=mysql_fetch_row($raff);
$amount= $raffle[0] / 2;
$dates= mysql_query("SELECT * FROM contest");
$stats= mysql_query("SELECT * FROM stats WHERE type='today'");
$statu= mysql_fetch_array($stats);

// while ($row = mysql_fetch_array($result)){
 // $uid = $row["id"]; 
 // $username = $row["user_id"];  
 // echo "User ID = $uid 
    // <br />User Name = $username 
   // <hr />"; 
// $winners= mysql_query("UPDATE members SET points=points+'".$amount."' WHERE username='".$username."'");
// $statusu1= mysql_query("UPDATE stats SET new='".$statu['new']."' WHERE type='yesterday'");
// $statusu2= mysql_query("UPDATE stats SET cashouts='".$statu['cashouts']."' WHERE type='yesterday'");
// $statusu3= mysql_query("UPDATE stats SET complete='".$statu['complete']."' WHERE type='yesterday'");
// $announce= mysql_query("INSERT INTO comment (text, home, date, text1)
// VALUES ('Rewards Cube System','',NOW(),'".$username." just won ".$amount." points in the daily raffle, Congrats!')");
 // }
 // $clear= mysql_query("UPDATE raffle SET amount='0'");
 // $clear1= mysql_query("UPDATE stats SET new='0' WHERE type='today'");
 // $clear2= mysql_query("UPDATE stats SET cashouts='0' WHERE type='today'");
 // $clear3= mysql_query("UPDATE stats SET complete='0' WHERE type='today'");
// print" DONE";



while ($dat = mysql_fetch_array($dates)) {
$places= mysql_query("SELECT * FROM members ORDER BY entries_".$dat['type']." DESC LIMIT ".$dat['rewards']."");
if ($dat['rewards'] == 1){
$reward = array ('1' => $dat['reward_1']);
}
else if ($dat['rewards'] == 2){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2']);
}
else if ($dat['rewards'] == 3){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3']);
}
else if ($dat['rewards'] == 4){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4']);
}
else if ($dat['rewards'] == 5){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5']);
}
else if ($dat['rewards'] == 6){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5'], '6' => $dat['reward_6']);
}
else if ($dat['rewards'] == 7){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5'], '6' => $dat['reward_6'], '7' => $dat['reward_7']);
}
else if ($dat['rewards'] == 8){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5'], '6' => $dat['reward_6'], '7' => $dat['reward_7'], '8' => $dat['reward_8']);
}
else if ($dat['rewards'] == 9){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5'], '6' => $dat['reward_6'], '7' => $dat['reward_7'], '8' => $dat['reward_8'], '9' => $dat['reward_9']);
}
else if ($dat['rewards'] == 10){
$reward = array ('1' => $dat['reward_1'], '2' => $dat['reward_2'], '3' => $dat['reward_3'], '4' => $dat['reward_4'], '5' => $dat['reward_5'], '6' => $dat['reward_6'], '7' => $dat['reward_7'], '8' => $dat['reward_8'], '9' => $dat['reward_9'], '10' => $dat['reward_10']);
}
while ($place = mysql_fetch_array($places)) {
if(time() <= strtotime($dat['date_2'])) {
foreach($reward as $rew)
print" {$dat['name']}  {$place['username']} {$rew} <br>";
}
else {
print" no contests ready for rewards given";
}
}
}
?>

它给了我想要的结果,但它为每个重复它所以它给了我:

Monthly Offer Contest Faiz66 2500
Monthly Offer Contest Faiz66 1000
Monthly Offer Contest Faiz66 500
Monthly Offer Contest taras 2500
Monthly Offer Contest taras 1000
Monthly Offer Contest taras 500
Monthly Offer Contest admin 2500
Monthly Offer Contest admin 1000
Monthly Offer Contest admin 500
Monthly Referral Contest taras 2500
Monthly Referral Contest taras 1000
Monthly Referral Contest taras 500
Monthly Referral Contest kira423 2500
Monthly Referral Contest kira423 1000
Monthly Referral Contest kira423 500
Monthly Referral Contest Faiz66 2500
Monthly Referral Contest Faiz66 1000
Monthly Referral Contest Faiz66 500 

每位获胜者都会重复3次奖励,但我需要看起来像这样:

Monthly Offer Contest Faiz66 2500
Monthly Offer Contest taras 1000
Monthly Offer Contest admin 500
Monthly Referral Contest taras 2500
Monthly Referral Contest kira423 1000
Monthly Referral Contest Faiz66 500 

也许阵列不是最好的解决方案,但如果是,我想我需要知道如何使它看起来像上面的表而不是它现在给我的第一个。

1 个答案:

答案 0 :(得分:0)

在使用此代码休息并阅读其他一些方法后,我终于想出了一个可行的解决方案。

它可能不是最好的,但确实有效。我愿意接受任何人可能拥有的任何其他解决方案!

<?
include_once"config.php";
include_once"test/includes.php";
date_default_timezone_set('America/New_York');

$dates=mysql_query("SELECT * FROM contest");
$timestamp = time();

while ($dat = mysql_fetch_array($dates)) {
$places="(SELECT * FROM `".$dat['name']."` ORDER BY `completed` DESC LIMIT ".$dat['rewards'].")";
$results=mysql_query($places);

if ($dat['rewards'] == 1){
$reward = "".$dat['reward_1']."";
}
else if ($dat['rewards'] == 2){
$reward = "".$dat['reward_1'].",".$dat['reward_2']."";
}
else if ($dat['rewards'] == 3){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3']."";
}
else if ($dat['rewards'] == 4){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4']."";
}
else if ($dat['rewards'] == 5){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4'].",".$dat['reward_5']."";
}
else if ($dat['rewards'] == 6){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4'].",".$dat['reward_5'].",".$dat['reward_6']."";
}
else if ($dat['rewards'] == 7){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4'].",".$dat['reward_5'].",".$dat['reward_6'].",".$dat['reward_7']."";
}
else if ($dat['rewards'] == 8){
$reward = "".$dat['reward_1'].",".$dat['reward_2'].",".$dat['reward_3'].",".$dat['reward_4'].",".$dat['reward_5'].",".$dat['reward_6'].",".$dat['reward_7'].",".$dat['reward_8']."";
}
else if ($dat['rewards'] == 9){
$reward = "".$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']."";
}
else if ($dat['rewards'] == 10){
$reward = "".$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']."";
}

$rewardsa = explode(",", $reward);
$i=0;

// Offers Contest

if(time() <= $dat['date_2'] && $dat['type'] == offer) {
while ($place = mysql_fetch_array($results)) {
print" {$dat['name']}  {$place['username']}  {$rewardsa[$i]}<br>";
if($dat['r_type'] == points){
$winners= mysql_query("UPDATE members SET points=points+'".$rewardsa[$i]."' WHERE username='".$place['username']."'") or die(mysql_error());
$shout=mysql_query("INSERT INTO shout (id, username, datetime, shout) VALUES ('','".$shoutname."','".$timestamp."','".$place['username']." has just won ".$rewardsa[$i]." points in the ".$dat['name']."')") or die(mysql_error());
}
if($dat['r_type'] == cash){
$winners= mysql_query("UPDATE members SET cash=cash+'".$rewardsa[$i]."' WHERE username='".$place['username']."'") or die(mysql_error());
$shout=mysql_query("INSERT INTO shout (id, username, datetime, shout) VALUES ('','".$shoutname."','".$timestamp."','".$place['username']." has just won $".$rewardsa[$i]." in the ".$dat['name']."')") or die(mysql_error());
}
$i++;
if($winners && $shout){
$delete=mysql_query("DROP TABLE `".$dat['name']."`");
}
}
}

// Referral Contest

if(time() <= $dat['date_2'] && $dat['type'] == referral) {
while ($place = mysql_fetch_array($results)) {
print" {$dat['name']}  {$place['username']}  {$rewardsa[$i]}<br>";
if($dat['r_type'] == points){
$winners= mysql_query("UPDATE members SET points=points+'".$rewardsa[$i]."' WHERE username='".$place['username']."'") or die(mysql_error());
$shout=mysql_query("INSERT INTO shout (id, username, datetime, shout) VALUES ('','".$shoutname."','".$timestamp."','".$place['username']." has just won ".$rewardsa[$i]." points in the ".$dat['name']."')") or die(mysql_error());
}
if($dat['r_type'] == cash){
$winners= mysql_query("UPDATE members SET cash=cash+'".$rewardsa[$i]."' WHERE username='".$place['username']."'") or die(mysql_error());
$shout=mysql_query("INSERT INTO shout (id, username, datetime, shout) VALUES ('','".$shoutname."','".$timestamp."','".$place['username']." has just won $".$rewardsa[$i]." in the ".$dat['name']."')") or die(mysql_error());
}
$i++;
if($winners && $shout){
$delete=mysql_query("DROP TABLE `".$dat['name']."`");
}
}
}

}
?>