Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function keeps resetting vars..and will not stop 1

Status
Not open for further replies.

max2474

Programmer
May 10, 2012
40
GB
Managed to stay away for a while..lol. Lots of progress and overcome many problems, but cant work this one out..

Have not really used functions before, but now I have a need. Have written this :

PHP:
<?php
	function paylvlbonus($userid,$amount)
	{
		[COLOR=red]$t1paid=0;[/color]
		[COLOR=red]$t2paid=0;[/color]
		while (($t1paid==0)||($t2paid==0))
		{
		echo "<br />userid is ".$userid." t1paid is ".$t1paid." t2paid is ".$t2paid;
			$pb= mysql_query("SELECT l1refer, teirlvl FROM members
			WHERE userid = '$userid' LIMIT 1");
			while($paub = mysql_fetch_array($pb))
			{
				$referrer=$paub['l1refer'];
				$teirlvl=$paub['teirlvl'];
			}
			if ($userid == '1005')
			{
			echo "reached here";
			[COLOR=red]$t1paid=1;[/color]
			[COLOR=red]$t2paid=1;[/color]
			echo " t1paid is ".$t1paid." t2paid is ".$t2paid;
			}
			$userid=$referrer;
		}
	}
?>

and am having a problem with the two vars $t1paid and $t2paid. There is more to this function that I have deleted to debug.

When this function is called, the output is as follows:

screen said:
userid is 1006 t1paid is 0 t2paid is 0
userid is 1005 t1paid is 0 t2paid is 0reached here t1paid is 1 t2paid is 1
userid is 1005 t1paid is 0 t2paid is 0reached here t1paid is 1 t2paid is 1
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
userid is 1004 t1paid is 0 t2paid is 0
...
...
repeated until error-too many results.
nb - 1005 does refer to 1004, and 1004s referrer is 1004, so that part is not the issue.

Why are t1paid and t2paid changing back to 0 - as soon as they both hit 1, the function should have stopped? And, just noticed, how come 1005 came up twice?

My first thought was local/global vars, but the vars here are not used anywhere else, and so should be local.

Any pointers would be appreciated.
 
please post the code you are using to call the function.
 
there is nothing wrong with the code you have posted. so we are left only with the possibility that the procedural code that calls this function is looping over a series of userIDs. thus causing the continued output.

use this function to get more information. hopefully you will see the function ending properly

Code:
<?php 
function paylvlbonus($userid, $amount) {
    $t1paid = 0;
    $t2paid = 0;
    while (($t1paid == 0) || ($t2paid == 0)):
        echo <<<HTML
---<br />
userid is $userid<br/>
t1paid is $t1paid<br/>
t2paid is $t2paid<br/>
HTML;
        $pb = mysql_query("SELECT l1refer, teirlvl FROM members WHERE userid = '" .mysql_real_escape_string($userid) ."' LIMIT 1");
        $paub = mysql_fetch_assoc($pb) or die(mysql_error());
        $referrer = $paub['l1refer'];
       	$teirlvl = $paub['teirlvl'];
        if ($userid == '1005'):
            echo "reached here";
            $t1paid = 1;
            $t2paid = 1;
            echo <<<HTML
after query: <br/>
t1paid is $t1paid <br/>
t2paid is $t2paid<br/>
ending the cycle<br/>
---
HTML;
        endif;
        $userid = $referrer;
    endwhile;
	
	echo "function ends here <br/>";
}
?>

also consider the usage of break; and return.

break will break out of a control structure (like a while loop); whereas return will immediately exit the function (and pass back a value, if required).

The code posted does neither of these things, but does print a string stating that the function has ended.
 
hmm.... strange. the output of that was:
screen said:
---
userid is 1005
t1paid is 0
t2paid is 0
reached hereafter query:
t1paid is 1
t2paid is 1
ending the cycle
---function ends here
---
userid is 1004
t1paid is 0
t2paid is 0
---
userid is 1004
t1paid is 0
t2paid is 0
---

etc
etc

Calling the function -
PHP:
			if 	(($attimediff<=28)&&($_SESSION[plan]>=$atp))
			{
				$atupdatefastcount= mysql_query("UPDATE members SET fastcount = fastcount+1, fastcountamount = fastcountamount+$amounttopay
				WHERE userid = '$atw' LIMIT 1");  
				$atupdatemyfastcount= mysql_query("UPDATE members SET fastcountamount = fastcountamount+$amounttopay
				WHERE userid = '1001' LIMIT 1");
				if ($_SESSION[rfcount][$atf]==5) /* then this one makes it 6! */
				{
					$atupdatefc= mysql_query("UPDATE members SET fastcountamount = 0, tottokens = tottokens+$attotfastpay, tokens = tokens+$attotfastpay, newtokens = newtokens+$attotfastpay
					WHERE userid = '$atw' LIMIT 1");
					
					[COLOR=red]paylvlbonus($atw,$attotfastpay);[/color]
					
					$atpayme=mysql_query("UPDATE members SET fastcountamount = fastcountamount-$attotfastpay, tottokens = tottokens+$attotfastpay, tokens=tokens+$attotfastpay, newtokens = newtokens+$attotfastpay
					WHERE userid = '1001' LIMIT 1");
					
					$atmycount=mysql_query("UPDATE members SET bonus = bonus+$attotfastpay, bonustot=bonustot+1
					WHERE userid = '1004' LIMIT 1");					
				}
			}
atw is the users ID, attotfastpay an the amount paid. This works without the function call.

I dont know if this is relevant, this is where the function is added to the page:

PHP:
<?php
	session_start();
	require("phpsnips/connectdb.php");
	require("phpsnips/getnews.php");
	require("joinfunction/payteirbonus.php");
.....
As I have said, I have not used functions before, but I believe that this shouldnt run until it is called, even if its near the top of the whole page?

This is a simple payment type system. I decided to add a bonus structure to it, which is what this function would achieve. Without the "bonus", the whole script works fine.
 
Meant to highlight that last bit... The function is in a file called payteirbonus.php
 
Just thought... there is a possibility that this isnt the call causing the issue, i added the call on a few php pages that are all called from one page... let me take off some and I will get the proper call.... unless you spot something. BRB
 
ok... that wasnt the call thats causing the issue... it this:

PHP:
	$mppaycount=0;
	while ($mppaycount <= 8) [COLOR=green]/* for counting an array of users up from 0 to 8*/[/color]
	{
		$mpref=$_SESSION[refs][$mppaycount]; [COLOR=green]/*this is the userid to use from 0 to 8*/[/color]
		$mprefpay=$_SESSION[refpay][$mppaycount]; [COLOR=green]/*this is the amount to give that user */[/color]

		$mppayrefs=mysql_query("UPDATE members SET tottokens = tottokens+$mprefpay, tokens = tokens+$mprefpay, newtokens = newtokens+$mprefpay
			WHERE userid = '$mpref' LIMIT 1"); [COLOR=green] /*this adds the amount to the users db total*/[/color]
		$mppayrefs=mysql_query("UPDATE members SET tottokens = tottokens+$mprefpay, tokens = tokens+$mprefpay, newtokens = newtokens+$mprefpay
			WHERE userid = '1001' LIMIT 1"); [COLOR=green] /*this provides me with the same info*/[/color]
		[COLOR=red]paylvlbonus($mpref,$mprefpay);[/color] [COLOR=green]/*this sends the userid and the amount to the function */[/color]
		$mppaycount++; [COLOR=green]/* move to the second user */[/color]
	}

btw - the script works as expected in other calls, so its definately here where the problem lies.


the function goes to the userid, pays a bonus on to of the above (that is missing for degugging). Each user has a referrer, which the script should go to next until the userid = 1005. (1005's referrer is 1004, 1004's is 1004)

This process is then repeated 8 times ($mppaycount++;)

Wanted to explain it better!
 
update - took the echos out if the function and added them to the call...

PHP:
	echo "<br />got here";
		paylvlbonus($mpref,$mprefpay);
		$mppaycount++;
	echo " mppcount is ".$mppaycount;

this gives :
screen said:
got here mppcount is 1
got here
Fatal error: Maximum execution time of 60 seconds exceeded in \\FLIPIN\PUBLIC\xampp\htdocs\metcol\metcolsite\joinfunction\payteirbonus.php on line 9

The fatal error assumably being the repeats of 1004. it looks like the fail is on the second call.
 
Because the error occurs by the function being called multiple times, do you think I need to add something like this:
PHP:
[COLOR=red]global $pt1paid;[/color]
[COLOR=red]global $pt2paid;[/color]
$pt1paid=0;
$pt2paid=0;
or
PHP:
[COLOR=red]static $pt1paid;[/color]
[COLOR=red]static $pt2paid;[/color]
$pt1paid=0;
$pt2paid=0;
to the top of paylvlbonus.php?

If so, which vars will need to be done?
 
FOUND IT!!!! lm*ao.

It was in the loop. As 1005s referrer is 1004, the function gets called again with this number which can never end. I just need to add a condition to the loop saying that if mpref=1005, then count=9 to stop the loop.

Thanks for your help. took a lot of echo statements to get it! :)
 
The function will go on forever until the criteria are met. If the userid is not 1005 going into the function then the function will continue forever. Variables within functions do not maintain state. Unless they are told to do so.
If it is your aim to have the function maintain state then prepend the static keyword to the two paid variables.
However surely it would be better for the function to pass back some data (like the value of the paid variables ) and for the conditional to be part of the calling scope. But I have no idea of the business aim you are trying to achieve nor the larger codebase you are using.
 
Its mammoth.. Its a huge membership website with benefits, payouts, referral bonuses, and tones and tones (and tones) more. I should really have a team building it rather than trying to do it on my own as will take months. Already have about 80 pages..lol. And that's before I get to the content! Not the worlds quickest typer, so recon just that will take 3 to 4 months on its own.

Good news is that I can see the light with regard to the function of the site. Only have about 3 more tasks I need the site to perform, so maybe about a week :) (though I do keep adding new ones...lol)

Will show you when its up and running. Best guess is October/November.

Thanks very much for support... fair chance I'll need more soon. :)
 
not a problem.

since you have a way to go with the build, consider refactoring your code at an early stage (now) into entity mapped objects. I suspect that will greatly simplify your structures
 
Have just done a little research on mapped objects.. seems to be a sensible idea, though when publishing your site, do you neeed to make sure the host supports the entity map programme?

I have just noticed another peculiarity with my database, but will post on the sql forum.
 
nope. the mapping is done entirely in your code.

let's say you have an entity of 'dog'. and the properties of the 'dog' that you care about are race, sex, weight and colour. you then have the following columns in your db table

dogID
dogRace,
dogSex,
dogWeight,
dogColour

and an entity as so

Code:
class dog{
 public $fields = array('dogID', 'dogRace', 'dogSex', 'dogWeight', 'dogColour');
 public $table = 'dogs';
 public $primary = 'dogID';
 public $permittedRaces = array('','');//etc
 public $permittedSexes = array('m','f');

}

and so on. I make all my entity classes inherit a base class that has a few standard things in it like save, update, insert, delete, load etc etc.
 
Thanks for this. I will look into this more when I have finished the stage i am currently on. Do you know of any online sites that give more info? Was very difficult to find any relevant info on google..most of the results I looked at were either questions or were talking about using specific software.
 
about entity mapping to objects? it's a basic object oriented programming technique. Just read up on oop and all will be clear. when you're ready, post back here and I will share my base class (from which all other entities are inheritors) to get you started.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top