Jump to content

Question of effecient code!


raDiO_acTivE

Recommended Posts

Hello again

 

This is another question to do with the same script that i talked about in my previous thread but this is just a question on using less code and making it more efficient.

 

So the Weekly cron im running this script in ( a raffle script ) is the following piece of code.

 

<?php

$select = $db->query('SELECT * FROM `raffle`');
$this = $db->num_rows($select);
$that = $db->fetch_row($select);
$win = rand(1,$this);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = %d",$win);
$win2 = $db->query($win1);
$user = $db->fetch_row($win2);
$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",$that['pot'],$user['userid']);
$db->query($earnings);
event_add($user['userid'],"You won the weekly lottery and were credited \${$that['pot']}",$c);  

$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES (%d, unix_timestamp(),%u,%d)",
'',
$userid,
$that['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");

?>

 

That there is the code that runs every friday night. I was thinking of expanding it so it would choose three random winners however, is there i way i can do this without having to run three of everything..Like instead of doing something like this.

<?php
//Query to select three winners.
$select = $db->query('SELECT * FROM `raffle`');
$this = $db->num_rows($select);
$that = $db->fetch_row($select);
$win = rand(3,$this);
$win1 = sprintf("SELECT `userid` FROM `raffle` WHERE `id` = %d",$win);
$win2 = $db->query($win1);

//Three winners
$user = $db->fetch_row($winnerq1);
$user2 = $db->fetch_row($winnerq1);
$user3 = $db->fetch_row($winnerq1);

//How much they win forumla
$1win = 7/10 * $that['pot'] ;
$2win = 2/10 * $that['pot'] ;
$3win = 1/10 * $that['pot'] ;

//Send money to first winner
$earnings = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$1win,
$user['userid']);
$db->query($earnings);

//Send money to 2nd winner
$earn = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$2win,
$user['userid']);
$db->query($earn);

//Send money to 3rd winner
$ea = sprintf("UPDATE `users` SET `money` = `money` + %d WHERE `userid` = %u",
$3win,
$user['userid']);
$db->query($ea);

//Event Ads
event_add($user['userid'],"You won the weekly lottery and were credited \$$1win",$c);  
event_add($user2['userid'],"You won the weekly lottery and were credited \$$2win",$c);  
event_add($user3['userid'],"You won the weekly lottery and were credited \$$3win",$c);  


//Raffle Log
$insert = sprintf("INSERT INTO `rafflelog` (rID,rDATE,rUSER,rAMNT) VALUES (%d,%d,%u,%d)",
'';
unix_timestamp();
$user;
$that['pot']);
$db->query($insert);


$db->query("TRUNCATE TABLE `raffle`");
$db->query("UPDATE `users` SET `lottery` = 0");
?>

 

Im no advanced php coder but i try my best. So im not sure if there is a way to do this without having 3 of everything.

 

If you can help me i appreciate it =)

 

Thankyou

 

PS. That second piece of code is not tested at all. Im certain it will not work.

Link to comment
Share on other sites

SELECT `userid` FROM `raffle` ORDER BY RAND() LIMIT 3

 

will give you three random 'userid's

 

And yes, you could use arrays and for loops to remove some of the redundant code

Using that query gives three random userids as you said, however, i need to specify the three users as each winner wins a different amount out of the pot, they do not all receive the same amount?

Link to comment
Share on other sites

Hmmm im having a bit of trouble here.

 

Could someone post the base code of a for() loop with a counter?

 

Ive been trying with the following base code.

 

<?php
//Your starting number & Ending number
$start_point = 0;
$end_pont = 5;


for($i = $start_point; $i < $end_point; $i++) 
//What we do here is the following:
// $i = $start_point == your starting number
// $i = $end_point == your ending number
// $i++ == counts to that number to stop


//Start the loop
{
//echo out your restult

echo $i.'<br>';

//close the loop
}
?>

 

Is that correct?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.