Page 1 of 1

While Loop doesn't end [RESOLVED]

Posted: Sun Dec 25, 2011 9:40 pm
by vitinho444
Hey guys.

So.. some of you may know that i'm making a Browser Based Game about Business..

Basicly its a game where you can open your own business and it's just like real world, with taxes and stuff.

Well.. i was coding a simple system that allow players to check their previous payments.

Here's the code:

Code: Select all


if(isset($_POST['playername']))
	{
		echo "<form method='POST' action='gerir.php'> <input type='submit' value='Go Back' name='submit'> </form>";
		$player = $_POST['playername'];
		
		 $query = mysql_query("SELECT * FROM `pagamentos` WHERE `username`='$player' ORDER BY `data` DESC");  // this will select all payments from the payments table of couse it will only select the player ones.
		$result = mysql_fetch_array($query); 
		$num = mysql_numrows($query); 
		
		$i=0;
		
		// So here it's the problem, i got only 1 field (1 payment) so it should go to the while loop one time and then stop... but nop.. it never stops presenting the same payment.
		while ($i < $num ) { 

			
			$data = mysql_result($query,$i,"data");
			$pagamento = mysql_result($query,$i,"pagamento");
			$irc = mysql_result($query,$i,"irc");
			$seg_social_gerente = mysql_result($query,$i,"seg_social_gerente");
			$seg_social_empregados = mysql_result($query,$i,"seg_social_empregados");
			$agua = mysql_result($query,$i,"agua");
			$luz = mysql_result($query,$i,"luz");
			$gas = mysql_result($query,$i,"gas");
			
			$lucro = mysql_result($query,$i,"lucro");
			$ganhos = mysql_result($query,$i,"ganhos");
			$gastos = mysql_result($query,$i,"gastos");
			
			
			
			echo "<br><u>Pagamento de $data</u>";
			echo "<br>Pagamento efectuado: $pagamento";
			echo "<br>Irc: $irc";
			echo "<br>Segurança Social do Gerente: $seg_social_gerente";
			echo "<br>Segurança Social de Empregados: $seg_social_empregados";
			echo "<br>Água: $agua";
			echo "<br>Luz: $agua";
			echo "<br>Gás: $gas";
			echo "<br>Lucro: $lucro";
			echo "<br>Prejuizo: $gastos";
			
			
			}
			
	}
	else
	{
	echo '<meta http-equiv="refresh" content="0; URL=gerir.php">';
	}
I really dont know what's messing things up :S

Hope you guys can help me.

Credits:
If you help, you got credited.

Re: While Loop doesn't end ...

Posted: Sun Dec 25, 2011 10:58 pm
by Xaleph
Doesnt seem that hard, your while loop is like this:

if $i is smaller then $num, do the loop

However, inside the loop, you never increment the $num so $num will allways be the same and $i doesn`t change at all. So if $i = 1 and $num is 2, this loop will run indefinitely. Because if neither $i nor $num change, it allways matches the criteria. What you should do is increment either both or at the very least one of the two to break out the loop. So incrementing the $i is a good idea for now since i reckon $num should a "constant" number.

Re: While Loop doesn't end ...

Posted: Sun Dec 25, 2011 11:11 pm
by vitinho444
F*ck!!!!!!
I ALWAYS FORGET THAT PART

DAMN HEAD DAMN MIND!!!

thank you XD

Just give me a name please so i can include you in the credits.

Re: While Loop doesn't end ...

Posted: Sun Dec 25, 2011 11:22 pm
by Xaleph
Haha we`ve all been there, it`s a small thing to forget if you`re in the "zone" :)

Good luck with your project.

Re: While Loop doesn't end ...

Posted: Mon Dec 26, 2011 1:46 am
by Jackolantern
Glad you got it figured out! Don't forget to mark your thread as [resolved] by editing the first post and adding it to the title!

Re: While Loop doesn't end [RESOLVED]

Posted: Mon Dec 26, 2011 11:01 am
by vitinho444
xaleph don't you want to be credited?

Re: While Loop doesn't end [RESOLVED]

Posted: Mon Dec 26, 2011 3:22 pm
by Xaleph
Hey there, no, i don`t need to be credited, it`s your project.

Re: While Loop doesn't end [RESOLVED]

Posted: Mon Dec 26, 2011 7:45 pm
by vitinho444
But you helped.. ok ..