While Loop doesn't end [RESOLVED]

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

While Loop doesn't end [RESOLVED]

Post 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.
Last edited by vitinho444 on Mon Dec 26, 2011 11:01 am, edited 1 time in total.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: While Loop doesn't end ...

Post 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.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: While Loop doesn't end ...

Post 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.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: While Loop doesn't end ...

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: While Loop doesn't end ...

Post 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!
The indelible lord of tl;dr
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: While Loop doesn't end [RESOLVED]

Post by vitinho444 »

xaleph don't you want to be credited?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: While Loop doesn't end [RESOLVED]

Post by Xaleph »

Hey there, no, i don`t need to be credited, it`s your project.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: While Loop doesn't end [RESOLVED]

Post by vitinho444 »

But you helped.. ok ..
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Coding”