Jump to content
MakeWebGames

Code takes longer than 30 sec to run.


Will

Recommended Posts

I'm making a lottery mod where users can buy tickets and there is a draw every week. The code which checks the numbers::


<?php
/*#####################################################
-LOTTO
-COPYRIGHT William Hector
-For use on The Crime Life only.
######################################################*/
require "mysql.php";
require "global_func.php";
global $c;
// Password
if ($_GET['pass'] != '#########################')
{
die("Go away");
}
$lo=mysql_query("SELECT * FROM lotto WHERE numbers=''",$c) or die(mysql_error());
$l=mysql_fetch_array($lo);
// Number generation
$na=rand(1,40);
$sa = "|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|";

	$ga= "|" . $na . "|";
	$fa = str_replace($ga, "|", $sa);
	$nb=rand(1,39);
	$d = explode("|", $fa);
	$anb=$d[$nb];

	$gb= "|" . $anb . "|";
	$fb = str_replace($gb, "|", $fa);
	$nc=rand(1,38);
	$d3 = explode("|", $fb);
	$anc=$d3[$nc];

	$gc= "|" . $anc . "|";
	$fc = str_replace($gc, "|", $fb);
	$nd=rand(1,37);
	$d4 = explode("|", $fc);
	$and=$d4[$nd];

	$gd= "|" . $and . "|";
	$fd = str_replace($gd, "|", $fc);
	$ne=rand(1,36);
	$d5 = explode("|", $fd);
	$ane=$d5[$ne];

	$ge= "|" . $ane . "|";
	$fe = str_replace($ge, "|", $fd);
	$nf=rand(1,35);
	$d6 = explode("|", $fe);
	$anf=$d6[$nf];

$number=$na . "|" . $anb . "|" . $anc . "|" . $and . "|" . $ane . "|" . $anf . "|";
// Ticket check: 
$week=$l['weekno'];
echo "Week number:";
echo $week;
$tic = mysql_query("SELECT * FROM lottotic WHERE weekno = {$week}",$c) or die(mysql_error());
if (mysql_num_rows($tic) == 0)
{
die("Oh noes!");
}

while($ti=mysql_fetch_array($tic))
{
$tn=$ti['numbers'];
$t = explode("|", $tn);
	if ($na==$t[0] || $na==$t[1] || $na==$t[2] || $na==$t[3] || $na==$t[4] || $na==$t[5])
	{
		$ck1=1;
	}
	else
	{
		$ck1=0;
	}
	if ($anb==$t[0] || $anb==$t[1] || $anb==$t[2] || $anb==$t[3] || $anb==$t[4] || $anb==$t[5])
	{
		$ck2=1;
	}
	else
	{
		$ck2=0;
	}
	if ($anc==$t[0] || $anc==$t[1] || $anc==$t[2] || $anc==$t[3] || $anc==$t[4] || $anc==$t[5])
	{
		$ck3=1;
	}
	else
	{
		$ck3=0;
	}
	if ($and==$t[0] || $and==$t[1] || $and==$t[2] || $and==$t[3] || $and==$t[4] || $and==$t[5])
	{
		$ck4=1;
	}
	else
	{
		$ck4=0;
	}
	if ($ane==$t[0] || $ane==$t[1] || $ane==$t[2] || $ane==$t[3] || $ane==$t[4] || $ane==$t[5])
	{
		$ck5=1;
	}
	else
	{
		$ck5=0;
	}
	if ($anf==$t[0] || $anf==$t[1] || $anf==$t[2] || $anf==$t[3] || $anf==$t[4] || $anf==$t[5])
	{
		$ck6=1;
	}
	else
	{
		$ck6=0;
	}
	$nrt=$ck1+$ck2+$ck3+$ck4+$ck5+$ck6;
	if ($nrt>2)
	{
	$id=$ti['id'];
	$userid=$ti['userid'];
		mysql_query("INSERT INTO lottowin VALUES('', '{$week}', '{$userid}', '{$nrt}', '{$id}')",$c) or die(mysql_error());
	}
}
}

$lid=$l['id'];
mysql_query("UPDATE lotto SET numbers='$number' WHERE id=$lid",$c) or die(mysql_error());
$nxtwk=$week+1;
mysql_query("INSERT INTO lotto VALUES('', '{$nxtwk}', '0', '', '')",$c) or die(mysql_error());
//money
$money=$l['jackpot'];
$mth=($money/100);
$sipt=($mth*60);
$topt=($mth*20);
$tept=($mth*10);

//6 winners
$siwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=6",$c) or die(mysql_error());
{
if (mysql_num_rows($siwin) == 0)
{
	mysql_query("UPDATE lotto SET jackpot=jackpot+$sipt WHERE weekno=$nxtwk",$c) or die(mysql_error());
}
else
{
	$siners=mysql_num_rows($siwin);
	$each=$sipt/$siners;
	while($w=mysql_fetch_array($siwin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 6 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		{
			if (mysql_num_rows($an)>0)
			{
				mysql_query("UPDATE lottoan SET six=six+1, sixmon=sixmon+$each WHERE userid=$userid",$c) or die(mysql_error());
			}
			else
			{
				mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 1, '{$each}', 0, 0, 0, 0, 0, 0)",$c) or die(mysql_error());
			}
		}
	}
}
}
//5balls
$towin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=5",$c) or die(mysql_error());
$toners=mysql_num_rows($towin);
$each=$topt/$toners;
	while($w=mysql_fetch_array($towin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 5 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET five=five+1, fivemon=fivemon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 1, '{$each}', 0, 0, 0, 0)",$c) or die(mysql_error());
		}

	}
//4balls
$fowin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=4",$c) or die(mysql_error());
$foners=mysql_num_rows($fowin);
$each=$tept/$foners;
	while($w=mysql_fetch_array($fowin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 4 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET four=four+1, fourmon=fourmon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 1, '{$each}', 0, 0)",$c) or die(mysql_error());
		}
	}
//3 balls
$thwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=3",$c) or die(mysql_error());
	while($w=mysql_fetch_array($thwin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		mysql_query("UPDATE users SET money=money+100 WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won /$100 on the lottery with 3 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET three=three+1, threemon=threemon+10 WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 0, 0, 1, 10)",$c) or die(mysql_error());
		}
	}

$anw = mysql_query("SELECT * FROM lottoan WHERE week = {$week}",$c) or die(mysql_error());
	while($w=mysql_fetch_array($anw))
	{
	$userid=$w['userid'];
	$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
	$ur=mysql_fetch_array($usr);
	if ($w['six'] > 0)
	{
	$tic=$w['six'];
	$mon=$w['sixmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 6 balls right with $tic ticket(s)",$c);
	}
	if ($w['four'] > 0)
	{
	$tic=$w['four'];
	$mon=$w['fourmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 4 balls right with $tic ticket(s)",$c);
	}
	if ($w['five'] > 0)
	{
	$tic=$w['five'];
	$mon=$w['fivemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 5 balls right with $tic ticket(s)",$c);
	}
	if ($w['three'] > 0)
	{
	$tic=$w['three'];
	$mon=$w['threemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 3 balls right with $tic ticket(s)",$c);
	}
	}
?>

Produced an out of memory problem when there where over 1 million tickets. To fix this I replaced it with:

 


<?php
/*#####################################################
-LOTTO
-COPYRIGHT William Hector
-For use on The Crime Life only.
######################################################*/
require "mysql.php";
require "global_func.php";
global $c;
// Password
if ($_GET['pass'] != '#########################')
{
die("Go away");
}
$lo=mysql_query("SELECT * FROM lotto WHERE numbers=''",$c) or die(mysql_error());
$l=mysql_fetch_array($lo);
// Number generation
$na=rand(1,40);
$sa = "|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|";

	$ga= "|" . $na . "|";
	$fa = str_replace($ga, "|", $sa);
	$nb=rand(1,39);
	$d = explode("|", $fa);
	$anb=$d[$nb];

	$gb= "|" . $anb . "|";
	$fb = str_replace($gb, "|", $fa);
	$nc=rand(1,38);
	$d3 = explode("|", $fb);
	$anc=$d3[$nc];

	$gc= "|" . $anc . "|";
	$fc = str_replace($gc, "|", $fb);
	$nd=rand(1,37);
	$d4 = explode("|", $fc);
	$and=$d4[$nd];

	$gd= "|" . $and . "|";
	$fd = str_replace($gd, "|", $fc);
	$ne=rand(1,36);
	$d5 = explode("|", $fd);
	$ane=$d5[$ne];

	$ge= "|" . $ane . "|";
	$fe = str_replace($ge, "|", $fd);
	$nf=rand(1,35);
	$d6 = explode("|", $fe);
	$anf=$d6[$nf];

$number=$na . "|" . $anb . "|" . $anc . "|" . $and . "|" . $ane . "|" . $anf . "|";
// Ticket check: 
$week=$l['weekno'];
echo "Week number:";
echo $week;
$ticyuy = mysql_query("SELECT COUNT(*) FROM lottotic WHERE weekno = {$week}",$c) or die(mysql_error());
$ticyu = mysql_fetch_array($ticyuy);
$tot=$ticyu[0];
if ($ticyu[0] == 0)
{
die("Oh noes!");
}
for ($y=0; $y<10; $y++)
{
$ticyuy = mysql_query("SELECT COUNT(*) FROM lottotic WHERE weekno = {$week}",$c) or die(mysql_error());
$ticyu = mysql_fetch_array($ticyuy);
$tot=$ticyu[0];
$div=$tot/10;
$start=$div*$y;
$tic = mysql_query("SELECT * FROM lottotic WHERE weekno = {$week} LIMIT {$start},{$div}",$c) or die(mysql_error());
while($ti=mysql_fetch_array($tic))
{
$tn=$ti['numbers'];
$t = explode("|", $tn);
	if ($na==$t[0] || $na==$t[1] || $na==$t[2] || $na==$t[3] || $na==$t[4] || $na==$t[5])
	{
		$ck1=1;
	}
	else
	{
		$ck1=0;
	}
	if ($anb==$t[0] || $anb==$t[1] || $anb==$t[2] || $anb==$t[3] || $anb==$t[4] || $anb==$t[5])
	{
		$ck2=1;
	}
	else
	{
		$ck2=0;
	}
	if ($anc==$t[0] || $anc==$t[1] || $anc==$t[2] || $anc==$t[3] || $anc==$t[4] || $anc==$t[5])
	{
		$ck3=1;
	}
	else
	{
		$ck3=0;
	}
	if ($and==$t[0] || $and==$t[1] || $and==$t[2] || $and==$t[3] || $and==$t[4] || $and==$t[5])
	{
		$ck4=1;
	}
	else
	{
		$ck4=0;
	}
	if ($ane==$t[0] || $ane==$t[1] || $ane==$t[2] || $ane==$t[3] || $ane==$t[4] || $ane==$t[5])
	{
		$ck5=1;
	}
	else
	{
		$ck5=0;
	}
	if ($anf==$t[0] || $anf==$t[1] || $anf==$t[2] || $anf==$t[3] || $anf==$t[4] || $anf==$t[5])
	{
		$ck6=1;
	}
	else
	{
		$ck6=0;
	}
	$nrt=$ck1+$ck2+$ck3+$ck4+$ck5+$ck6;
	if ($nrt>2)
	{
	$id=$ti['id'];
	$userid=$ti['userid'];
		mysql_query("INSERT INTO lottowin VALUES('', '{$week}', '{$userid}', '{$nrt}', '{$id}')",$c) or die(mysql_error());
	}
}
}

$lid=$l['id'];
mysql_query("UPDATE lotto SET numbers='$number' WHERE id=$lid",$c) or die(mysql_error());
$nxtwk=$week+1;
mysql_query("INSERT INTO lotto VALUES('', '{$nxtwk}', '0', '', '')",$c) or die(mysql_error());
//money
$money=$l['jackpot'];
$mth=($money/100);
$sipt=($mth*60);
$topt=($mth*20);
$tept=($mth*10);

//6 winners
$siwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=6",$c) or die(mysql_error());
{
if (mysql_num_rows($siwin) == 0)
{
	mysql_query("UPDATE lotto SET jackpot=jackpot+$sipt WHERE weekno=$nxtwk",$c) or die(mysql_error());
}
else
{
	$siners=mysql_num_rows($siwin);
	$each=$sipt/$siners;
	while($w=mysql_fetch_array($siwin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 6 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		{
			if (mysql_num_rows($an)>0)
			{
				mysql_query("UPDATE lottoan SET six=six+1, sixmon=sixmon+$each WHERE userid=$userid",$c) or die(mysql_error());
			}
			else
			{
				mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 1, '{$each}', 0, 0, 0, 0, 0, 0)",$c) or die(mysql_error());
			}
		}
	}
}
}
//5balls
$towin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=5",$c) or die(mysql_error());
$toners=mysql_num_rows($towin);
$each=$topt/$toners;
	while($w=mysql_fetch_array($towin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 5 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET five=five+1, fivemon=fivemon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 1, '{$each}', 0, 0, 0, 0)",$c) or die(mysql_error());
		}

	}
//4balls
$fowin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=4",$c) or die(mysql_error());
$foners=mysql_num_rows($fowin);
$each=$tept/$foners;
	while($w=mysql_fetch_array($fowin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 4 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET four=four+1, fourmon=fourmon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 1, '{$each}', 0, 0)",$c) or die(mysql_error());
		}
	}
//3 balls
$thwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=3",$c) or die(mysql_error());
	while($w=mysql_fetch_array($thwin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		mysql_query("UPDATE users SET money=money+100 WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won /$100 on the lottery with 3 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET three=three+1, threemon=threemon+10 WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 0, 0, 1, 10)",$c) or die(mysql_error());
		}
	}

$anw = mysql_query("SELECT * FROM lottoan WHERE week = {$week}",$c) or die(mysql_error());
	while($w=mysql_fetch_array($anw))
	{
	$userid=$w['userid'];
	$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
	$ur=mysql_fetch_array($usr);
	if ($w['six'] > 0)
	{
	$tic=$w['six'];
	$mon=$w['sixmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 6 balls right with $tic ticket(s)",$c);
	}
	if ($w['four'] > 0)
	{
	$tic=$w['four'];
	$mon=$w['fourmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 4 balls right with $tic ticket(s)",$c);
	}
	if ($w['five'] > 0)
	{
	$tic=$w['five'];
	$mon=$w['fivemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 5 balls right with $tic ticket(s)",$c);
	}
	if ($w['three'] > 0)
	{
	$tic=$w['three'];
	$mon=$w['threemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 3 balls right with $tic ticket(s)",$c);
	}
	}
?>

This one produces the error that the execution time has exceeded 30 seconds.

Is there anyway to optimize this code so it runs faster? I have tried dividing it into two pages but the but which checks the tickets still takes longer than 30 seconds.

Any help will be appreciated.

Thanks in advance.

Link to comment
Share on other sites

Re: Code takes longer than 30 sec to run.

 

<?php
/*#####################################################
-LOTTO
-COPYRIGHT William Hector
-For use on The Crime Life only.
######################################################*/
require "mysql.php";
require "global_func.php";
global $c;
// Password
if ($_GET['pass'] != '#########################')
{
die("Go away");
}
$lo=mysql_query("SELECT * FROM lotto WHERE numbers=''",$c) or die(mysql_error());
$l=mysql_fetch_array($lo);
// Number generation
$na=rand(1,40);
$sa = "|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|";

	$ga= "|" . $na . "|";
	$fa = str_replace($ga, "|", $sa);
	$nb=rand(1,39);
	$d = explode("|", $fa);
	$anb=$d[$nb];

	$gb= "|" . $anb . "|";
	$fb = str_replace($gb, "|", $fa);
	$nc=rand(1,38);
	$d3 = explode("|", $fb);
	$anc=$d3[$nc];

	$gc= "|" . $anc . "|";
	$fc = str_replace($gc, "|", $fb);
	$nd=rand(1,37);
	$d4 = explode("|", $fc);
	$and=$d4[$nd];

	$gd= "|" . $and . "|";
	$fd = str_replace($gd, "|", $fc);
	$ne=rand(1,36);
	$d5 = explode("|", $fd);
	$ane=$d5[$ne];

	$ge= "|" . $ane . "|";
	$fe = str_replace($ge, "|", $fd);
	$nf=rand(1,35);
	$d6 = explode("|", $fe);
	$anf=$d6[$nf];

$number=$na . "|" . $anb . "|" . $anc . "|" . $and . "|" . $ane . "|" . $anf . "|";
// Ticket check: 
$week=$l['weekno'];
echo "Week number:";
echo $week;
$ticyuy = mysql_query("SELECT COUNT(*) FROM lottotic WHERE weekno = {$week}",$c) or die(mysql_error());
$ticyu = mysql_fetch_array($ticyuy);
$tot=$ticyu[0];
if ($ticyu[0] == 0)
{
die("Oh noes!");
}
for ($y=0; $y<10; $y++)
{
$ticyuy = mysql_query("SELECT COUNT(*) FROM lottotic WHERE weekno = {$week}",$c) or die(mysql_error());
$ticyu = mysql_fetch_array($ticyuy);
$tot=$ticyu[0];
$div=$tot/10;
$start=$div*$y;
$tic = mysql_query("SELECT * FROM lottotic WHERE weekno = {$week} LIMIT {$start},{$div}",$c) or die(mysql_error());

// Set all defaults to 0
$ck1 = $ck2 = $ck3 = $ck4 = $ck5 = $ck6 = 0;

while($ti=mysql_fetch_assoc($tic))
{
	$tn=$ti['numbers'];
	$t = explode("|", $tn);
	if (in_array($na, $t)) {
		$ck1 = 1;
	}
	if (in_array($anb, $t)) {
		$ck2 = 1;
	}
	if (in_array($anc, $t)) {
		$ck3 = 1;
	}
	if (in_array($and, $t)) {
		$ck4 = 1;
	}
	if (in_array($ane, $t)) {
		$ck5 = 1;
	}
	if (in_array($anf, $t)) {
		$ck6 = 1;
	}
	$nrt=$ck1+$ck2+$ck3+$ck4+$ck5+$ck6;
	if ($nrt>2)
	{
		$id=$ti['id'];
		$userid=$ti['userid'];
		$temp_values .= ', ("", "'.$week.'", "'.$userid.'", "'.$nrt.'", "'.$id.'")';
	}
}
// Construct query in the loop and execute outside
mysql_query("insert into `lottowin` VALUES " . substr($temp_values, 1)) or die(mysql_error());
}

$lid=$l['id'];
mysql_query("UPDATE lotto SET numbers='$number' WHERE id=$lid",$c) or die(mysql_error());
$nxtwk=$week+1;
mysql_query("INSERT INTO lotto VALUES('', '{$nxtwk}', '0', '', '')",$c) or die(mysql_error());
//money
$money=$l['jackpot'];
$mth=($money/100);
$sipt=($mth*60);
$topt=($mth*20);
$tept=($mth*10);

//6 winners
$siwin = mysql_query("SELECT count(*) FROM lottowin WHERE week = {$week} AND nomatch=6",$c) or die(mysql_error());
$siwin_arr = mysql_fetch_array($siwin);

if ($siwin_arr[0] == 0)
{
mysql_query("UPDATE lotto SET jackpot=jackpot+$sipt WHERE weekno=$nxtwk",$c) or die(mysql_error());
}
else
{
	$siwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=6",$c) or die(mysql_error());
$siners=mysql_num_rows($siwin);
$each=$sipt/$siners;
while($w=mysql_fetch_array($siwin))
{
 	// These dont appear to be used except in taht event_add
	//$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
	//$ur=mysql_fetch_array($usr);
	$userid=$w['userid'];
	$monn=money_formatter($each);
	mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
	// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 6 Balls right",$c);
	$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());

	if (mysql_num_rows($an)>0)
	{
	 	$upd_users[] = $userid;
	}
	else
	{
	 	$upd_vals[] = "('', '{$week}', '{$userid}', 1, '{$each}', 0, 0, 0, 0, 0, 0)";
	}

}
// Update outside
mysql_query("UPDATE lottoan SET six=six+1, sixmon=sixmon+$each WHERE userid IN (".implode("'", $upd_users).")")or die(mysql_error());
mysql_query("INSERT INTO lottoan VALUES " . implode("," $upd_vals),$c) or die(mysql_error());	
}

//5balls
$towin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=5",$c) or die(mysql_error());
$toners=mysql_num_rows($towin);
$each=$topt/$toners;
	while($w=mysql_fetch_array($towin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 5 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET five=five+1, fivemon=fivemon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 1, '{$each}', 0, 0, 0, 0)",$c) or die(mysql_error());
		}

	}
//4balls
$fowin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=4",$c) or die(mysql_error());
$foners=mysql_num_rows($fowin);
$each=$tept/$foners;
	while($w=mysql_fetch_array($fowin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		$userid=$w['userid'];
		$monn=money_formatter($each);
		mysql_query("UPDATE users SET money=money+{$each} WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery with 4 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET four=four+1, fourmon=fourmon+$each WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 1, '{$each}', 0, 0)",$c) or die(mysql_error());
		}
	}
//3 balls
$thwin = mysql_query("SELECT * FROM lottowin WHERE week = {$week} AND nomatch=3",$c) or die(mysql_error());
	while($w=mysql_fetch_array($thwin))
	{
		$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
		$ur=mysql_fetch_array($usr);
		mysql_query("UPDATE users SET money=money+100 WHERE userid=$userid",$c) or die(mysql_error());
		// event_add($userid,"Congratualtions {$ur['username']}, you have won /$100 on the lottery with 3 Balls right",$c);
		$an=mysql_query("SELECT * FROM lottoan WHERE userid=$userid AND week={$week}",$c) or die(mysql_error());
		if (mysql_num_rows($an)>0)
		{
			mysql_query("UPDATE lottoan SET three=three+1, threemon=threemon+10 WHERE userid=$userid",$c) or die(mysql_error());
		}
		else
		{
			mysql_query("INSERT INTO lottoan VALUES('', '{$week}', '{$userid}', 0, 0, 0, 0, 0, 0, 1, 10)",$c) or die(mysql_error());
		}
	}

$anw = mysql_query("SELECT * FROM lottoan WHERE week = {$week}",$c) or die(mysql_error());
	while($w=mysql_fetch_array($anw))
	{
	$userid=$w['userid'];
	$usr = mysql_query("SELECT * FROM users WHERE userid={$w['userid']}",$c) or die(mysql_error());
	$ur=mysql_fetch_array($usr);
	if ($w['six'] > 0)
	{
	$tic=$w['six'];
	$mon=$w['sixmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 6 balls right with $tic ticket(s)",$c);
	}
	if ($w['four'] > 0)
	{
	$tic=$w['four'];
	$mon=$w['fourmon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 4 balls right with $tic ticket(s)",$c);
	}
	if ($w['five'] > 0)
	{
	$tic=$w['five'];
	$mon=$w['fivemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 5 balls right with $tic ticket(s)",$c);
	}
	if ($w['three'] > 0)
	{
	$tic=$w['three'];
	$mon=$w['threemon'];
	$monn=money_formatter($mon);
	event_add($userid,"Congratualtions {$ur['username']}, you have won {$monn} on the lottery by getting 3 balls right with $tic ticket(s)",$c);
	}
	}
?>

 

Try that so far, i took out a couple of loops... its hard to understand with those variable names though :P

You can also try putting set_time_limit(0); at the top, to see if it completes at all

I only edited up to the 5 balls part

Link to comment
Share on other sites

  • 7 months later...
Guest Anonymous

Re: Code takes longer than 30 sec to run.

Never set set_time_limit(0) unless you have very good access to the server and are prepared to shutdown/restart certain services.

Your initial problem looks like a simple one - reading too many rows from the database.

Easy to fix.... But not now, I'm too tired. I'll look more closely tomorrow.

Link to comment
Share on other sites

Guest Anonymous

Re: Code takes longer than 30 sec to run.

Right I'm not going to rewrite your code for you, however I will point out some areas where you went wrong and hopefully construct a nice SQL statement to fetch all winning tickets.

First, number generation

 

<?php
$used_numbers = array();
$number_list  = array();

for ($i = 0; $i < 6; $i++)
{
do
{
	$number = mt_rand(1, 49);		// pick minumum and maximum to suit
}
while (isset($used_numbers[$number]));

$used_numbers[$number] = true;
$number_list[] = $number;
}

sort($number_list);

$number_list_string = implode(",", $number_list);
?>

 

Notice, I've used a comma separator instead of a the vertical bar Why? Personal preference and the fact that there are some great SQL functions for manipulating CSV (comma separated values).

Now we come to the possibly more interesting part... How to select all possible winning combinations.

Well I had to think on this one a little, and I'm not 100% happy about the speed of this, but it does work.

 

SELECT id, owner,
IF(FIND_IN_SET(3,numbers),1,0)+
IF(FIND_IN_SET(4,numbers),1,0)+
IF(FIND_IN_SET(6,numbers),1,0)+
IF(FIND_IN_SET(8,numbers),1,0)+
IF(FIND_IN_SET(33,numbers),1,0)+
IF(FIND_IN_SET(41,numbers),1,0)+
IF(FIND_IN_SET(44,numbers),1,0)
AS matches, numbers
FROM lottery
HAVING matches >= 3

 

This assumes the following:

1. we are searching for the numbers 3, 4, 6, 8, 33, 41 and 44

2. we need at least 3 matches number

3. our table definition is as follows:

 

CREATE TABLE IF NOT EXISTS `lottery`
(
 `id` int(10) unsigned NOT NULL,
 `owner` int(10) unsigned NOT NULL,
 `numbers` varchar(17) COLLATE latin1_general_ci NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM;

 

Where id is the ticket ID #, owner is the user ID # owning said ticket, and numbers is a comma separated list of 6 numbers.

Putting all this into code is simple enough, so I will leave you with that, however I will be interested to hear of better - faster ways of selecting the same rowset. i.e. can anyone optimize this query further?

Link to comment
Share on other sites

Re: Code takes longer than 30 sec to run.

Looking at your alternative number generation; I'm wondering whether when it is generating the 6 numbers, it cannot produce the same one twice.

Since posting the topic I have changed my own number generation to this:

 

$lottoNum = array ();
$number='';
/* Add the numbers..*/
for($counter = 1; $counter <=40; $counter++)
   {
       array_push($lottoNum,$counter);
   }


for($index =1; $index <= 6; $index++)
   {
/* Pick random values */
       shuffle($lottoNum);
       $number=$number.$lottoNum[0]."|";
       array_shift($lottoNum);
   }

 

For the "Lucky Dip", it has to generate around 50,000 numbers; so it needs to be quick.

Which version do you think is quicker; does yours ensure the same isn't picked twice?

Link to comment
Share on other sites

Guest Anonymous

Re: Code takes longer than 30 sec to run.

Yes, my version generates 6 unique numbers, however I did not look at it as an entity that needed high levels of optimization.

My version is however a fair bit faster than yours, only 1 loop, no shuffling, no array manipulation etc.

Link to comment
Share on other sites

Guest Anonymous

Re: Code takes longer than 30 sec to run.

No offense, but if the above is a demonstration of your SQL and algorithmic skills, I'd stay clear of SPs...

Experiment with maybe 1000 tickets. There are plenty of good functions in SQL. Always try and reduce the number of rows you are searching, use the query optimizer.

SPs have there place but in this case I doubt there is any benefit (unless of course you want to start mucking around with temporary tables)

There are better ways of doing it - I'm sure my sample query can be well optimized with a little thinking.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...