Page 1 of 2

variables losing value

Posted: Mon May 28, 2007 9:05 am
by kguerrero
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a variable that is losing it's value and I am very confused about it ... I would appreciate any help I can get.

Here is the code:

Code: Select all

session_start();
define('GARDENPARTY_DIR', 'path on my server');
include(GARDENPARTY_DIR . 'libs/gardenmem_setup.php');


$garparmem=& new GardenMember;

$garparmem->identity = ($_REQUEST['viewkey']); // this is the value that is "disappearing"


// these two lines recently added to experiment with the variable that is "losing" it's value.
$_SESSION[identity] = $garparmem->identity;
$identity = $_SESSION[identity];


$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'enter';

switch($_action) {
	case 'enter':
		$today = date("Y-m-d H:i:s");
                // please note the assignment of $garparmem->identity is working and all actions in this case work as intended.
		$meminfo = $garparmem->logMemberIn ($garparmem->identity);
		$partyID = $meminfo[0]["PartyID"];
		$partyInfo = $garparmem->getPartyInfo($partyID);
		$hostSubsidy = $partyInfo[0]["PartyHostSubsidy"];
		$hostSubsidyPercent = ((100-$hostSubsidy)/100);
		$ItemIDs = $garparmem ->getPartyItems ($partyID);
		$itemCount = count($ItemIDs); 
		for($i=0;$i<$itemCount;$i++)
		{
			$ItemID=$ItemIDs[$i]["ItemID"];
			$MenuData = $garparmem ->getPartyMenuDetails ($ItemID);
			$totalPrice = $totalPrice + $MenuData[$i]["ItemPrice"];
		}
		$WillAttend = $garparmem ->getRepliedList ($partyID);
		$showComing = count($WillAttend);
		$NoReplyData = $garparmem ->getNotRepliedList ($partyID);
		$adjustedPrice = $hostSubsidyPercent*$totalPrice;
		$MemViewKey = $_SESSION[identity]; // this line was recently added to help experiment with the variable that is losing its value aka Hack.
$garparmem->dsplyReplyPage($NoReplyData,$WillAttend,$meminfo,$partyInfo,$MenuData,$itemCount,$adjustedPrice,$hostSubsidy,$showComing);
		break;

// the following case is where I lose the variable value;
	case 'reply':
		$willAttend = $_POST["response"];
		$Message = $_POST["message"];
		$carpool = $_POST["CarPool"];
		if ($carpool != 1){
			$carpool =0;
		}
		$emailme = $_POST["emailMe"];
		if ($emailme != 1){
			$emailme =0;
		}
		$garparmem->updateMemStatus($identity,$willAttend,$Message,$carpool,$emailme,$identity);		
		echo "Updated ... go check it out";	
		break;
	}

?>
If I place a var_dump anywhere in the "reply" Case the value of "$garparmem->identity" is NULL. What I mean by this is: as shown in the above I have assigned "$garparmem->identity" to variables like $identity or a Session variable and performed a var_dump on those variables but the value returns as "NULL".

Hopefully, all of that made sense.

And Yes, I am a PHP Noob

TIA




feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 28, 2007 9:12 am
by superdezign
Well, your code is no fun to read, but from the looks of it the problem may be in the class itself. Maybe... I'll assume, for now, that the class is fine.

Do $identity and $_SESSION['identity'] have the value that you want them to?

(And.. I'm not sure, but... your "$_SESSION[identity]" has no quotes in it's name... typo?)

Posted: Mon May 28, 2007 9:41 am
by kguerrero
Thanks for responding so quickly.

Yes. I can do a var_dump anywhere in the entry case or prior to the switch and everything is correct. It is just the reply case where a var_dump will show NULL.

The class was copied from a different class that is similar ... (i.e. Host versus member) ... the Host code works on numerous php pages all though the log in process is different I can access member variables any where in the switch

Here is the class code (I am only posting the pertinent/example stuff).

Code: Select all

class GardenMember {

    function GardenMember() {

		// instantiate the sql object
        $this->sql =& new GardenMember_SQL;
		// instantiate the template object
        $this->tpl =& new GardenMember_Smarty;

    }


    function dsplyReplyPage ($NoReplyData,$WillAttend,$meminfo,$partyInfo,$MenuData,$itemCount,$adjustedPrice,$hostSubsidy,$showComing){
	$this->tpl->assign('showComing',$showComing);
	$this->tpl->assign('NoReplyData',$NoReplyData);
	$this->tpl->assign('WillAttend',$WillAttend);
	$this->tpl->assign('meminfo',$meminfo[0]);
	$this->tpl->assign('partyInfo',$partyInfo[0]);
        $this->tpl->assign('MenuData', $MenuData);
        $this->tpl->assign('itemCount', $itemCount);
        $this->tpl->assign('memberPrice', $adjustedPrice);
        $this->tpl->assign('hostSubsidy', $hostSubsidy);
	$this->tpl->display('memberresponse.tpl');        
    }

	function getNotRepliedList ($partyID){
		// reset error message
		$this->error = null;
		// test if user is host
		$query = "SELECT * FROM `tblPartyMembers` WHERE `PartyID` = '$partyID' AND `Replied` = 0";
		$result = mysql_query($query);
		$nrows = mysql_num_rows($result);
		if (mysql_num_rows($result) < 1){
			$this->error = 'party_not_found';
		return false;
		}
		while ($row = mysql_fetch_assoc($result)) {
					$this->inviteNoReply_array[] = $row;
			}
		return $this->inviteNoReply_array;
	}

}
Yes the Quotes in the Session variable is a typo ... thanks for catching that. I am curious as to why it worked with out them ...

Thanks again

Posted: Mon May 28, 2007 9:46 am
by superdezign
I don't see $identity anywhere in the class. I also don't see updateMemStatus(). Now I'm completely lost as to what you are attempting to do.

Posted: Mon May 28, 2007 10:06 am
by kguerrero
Sorry ... I tried to avoid copying the entire class since it is >800 lines.

Here is the updateMemStatus() part.

Code: Select all

function updateMemStatus($identity,$willAttend,$Message,$carpool,$emailme,$identity){
		$_query = "UPDATE `tblPartyMembers` SET `WillAttend` = '$willAttend', `Replied`= 1, `message` = '$Message',`carpool` = $carpool, `emailMe` = $emailme WHERE `Viewkey` = '$identity'";
		if (!mysql_query($_query))
		{
			die('Error: ' . mysql_error());
		}
	}
I did not declare the variable $identity could that be the problem? (Probably a MOTO statement).

Here is my goal ... A Party Member recieves an email with a viewkey (which is unique) they click on this viewkey to view their invitation. This viewkey is assigned to

Code: Select all

$garparmem->identity = ($_REQUEST['viewkey']);
This is used to obtain the Invitee's information ... all of this works so, far.

The display looks great etc ... When the Party member hits "Reply Now" Variables are posted ...

Through troubleshooting (ie var_dump ($_POST)) all variables for the form are posted correctly except for the vital $identity.

If it is an issue with declaring the variable I am curious as to why it worked in the Entry Action ... but I will go ahead and try now.

Thanks again

Posted: Mon May 28, 2007 10:13 am
by kguerrero
Just to make sure I am communicating :-) since this is the latest "Hacked Code"

$identity in the UpdateMemFunction pass was at one time derived from $garparmem->identity. I discovered that $garparmem->identity was losing it's value when entering the Reply Case.

Thanks

Posted: Mon May 28, 2007 10:36 am
by superdezign
At no point during the "reply" case are you even using $garparmem->identity.

Code: Select all

$garparmem->updateMemStatus($identity,$willAttend,$Message,$carpool,$emailme,$identity);
You are just using $identity, which is assigned the value of $_SESSION['identity'].
What does $garparmem->identity's value matter, then?

Posted: Mon May 28, 2007 10:57 am
by kguerrero
I really appreciate your help and patience and I apologize for my lack of clarity.

When I initially tested the code it was like this:



At one point I had in my code

Code: Select all

case 'reply':
		$identity = $garparmem->identity;
		$willAttend = $_POST["response"];
		$Message = $_POST["message"];
		$carpool = $_POST["CarPool"];
		if ($carpool != 1){
			$carpool =0;
		}
		$emailme = $_POST["emailMe"];
		if ($emailme != 1){
			$emailme =0;
		}
//update tblPartyMembers
		$garparmem->updateMemStatus($identity,$willAttend,$Message,$carpool,$emailme);		
		echo "Updated ... go check it out";	
		break;

When this code did not update the DB. I changed it to this:

Code: Select all

case 'reply':
		$identity = $garparmem->identity;
		var_dump ($identity);
		die;
This produced a NULL result

So, I did this ... just for grins ...

Code: Select all

case 'reply':
		$identity = $garparmem->identity;
		var_dump ($garparmem->identity);
		die;
Which resulted in NULL so, I felt my primary focus should be on why $garparmem->identity is suddenly Null in the reply case. When it is fine in the 'Enter' Case.

I did make a work around for this ... but I am still wondering what is going on.

My work around consists of assigning the Viewkey value to an HTML hidden input. When the reply case is initiated through the posting of the form the viewkey is re-assigned via "$_POST".

I added $MemViewKey in the function dsplyReplyPage. And assigned it from Enter Case

Code: Select all

$MemViewKey = $garparmem->identity;
		$garparmem->dsplyReplyPage($NoReplyData,$WillAttend,$meminfo,$partyInfo,$MenuData,$itemCount,$adjustedPrice,$hostSubsidy,$showComing,$MemViewKey);
Now for the reply case I have:

Code: Select all

case 'reply':
		$identity = $_POST["MemViewKey"];
		$willAttend = $_POST["response"];
		$Message = $_POST["message"];
		$carpool = $_POST["CarPool"];
		if ($carpool != 1){
			$carpool =0;
		}
		$emailme = $_POST["emailMe"];
		if ($emailme != 1){
			$emailme =0;
		}
		$garparmem->updateMemStatus($identity,$willAttend,$Message,$carpool,$emailme);		
		echo "Updated ... go check it our";	
		break;
	}
This does produce the desired result but I am still wondering what happend to $garparmem->identity's value.

Posted: Mon May 28, 2007 12:43 pm
by superdezign
Well, somewhere along the line, it's being nullified. I'd echo it's value after every line of code until you find the line where the value changes to null.

Posted: Mon May 28, 2007 1:11 pm
by kguerrero
Yes ... that is pretty much what I did ... thus the code "evolution" I chose var_dump since when I performed an echo on the Nullified array I recieved nothing back. With var_dump I could at least get NULL.

It is Nullified for some reason when it enters the reply case of the switch. I can add an echo on the last line of the enter case. and everything is fine it returns array. If I add an echo on the first line of the reply case the screen is blank so I chose to var_dump at that point I get NULL so for some reason it is nullified when it enters that case.

The reply case is initiated when the form is submitted. Before my "work around" I did not reference the $identity value in the HTML form.

Does the act of initiating a different $_REQUEST reset the variable?

Becuase, that's the only thing I can see happening but I am confused since I leveraged previous working code which would reference variables from outside of a Switch Case

Code: Select all

$partyID = $_SESSION['partyID'];

	$memID = $garparhost->lastInvitee($partyID);
	
	$_action = "";
	$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'enter';


	switch($_action) {
		case 'enter':
			$garparhost->updateInvitee($partyID);
			$garparhost->isPartyFull($partyID);
			$nEmail = $garparhost->nInvitees;
			$garparhost->displayEmvite();
			break;
		case 'submit':
			for($i=0;$i<count($_REQUEST[invitemEmail]);$i++)
			{	
					if($_REQUEST[invitemEmail][$i]!="")
					{
						// Check Validity of Email Address.
						if(!checkEmail($_REQUEST[invitemEmail][$i])) { 
							$garparhost->err='Invalid_email';
						}
						$tag='yes';
						$emails[]=$_REQUEST[invitemEmail][$i];
						$rtype[]=$type[$i];
					}
			}
			// Check for Redundant email addresses on this page.
			for($i=0;$i<count($emails);$i++)
			{
				for($j=0;$j<count($emails);$j++)
				{
					if ($j!=$i && $emails[$i] == $emails[$j])
					{
						$garparhost->err="redundant_address_init";
					}
				}
			}
			
			if(!checkEmail($email)) { 
				$garparhost->err='Invalid_email';
			}
	
			if ($tag!="yes"){
				$garparhost->err="address_empty";
			}
			if ($garparhost->err==""){
				for($i=0;$i<count($emails);$i++)
				{
					$m = 1;
					$memID = $memID + $m;
					$iEmail = $invitemEmail[$i];
					$garparhost->addInvitee($iEmail,$memID,$partyID);
				}
				$garparhost->updateInvitee($partyID);
			}else{	
				$garparhost->isPartyFull($partyID);
				$nEmail = $garparhost->nInvitees;
				$garparhost->displayEmvite();
			}
			break;
	}
In this example $partyID was assigned outside the Switch ... but utilized in both cases.

Thanks again

Posted: Mon May 28, 2007 1:27 pm
by superdezign
Then I'm as clueless as you are. If you want to send me the full documents to take a look at (censor your passwords and such, first), I'll be happy to take a look and see what it is.

Posted: Mon May 28, 2007 10:55 pm
by volka
Which version of php do you use?

Posted: Tue May 29, 2007 8:43 am
by kguerrero
Volka - PHP 5.2.0

SuperDezign ... I will send you some files once I get some breathing room ... I would like to comment them out a bit thanks for the offer to check the code and again for your earlier support

Posted: Tue May 29, 2007 8:48 am
by superdezign
kguerrero wrote:Volka - PHP 5.2.0

SuperDezign ... I will send you some files once I get some breathing room ... I would like to comment them out a bit thanks for the offer to check the code and again for your earlier support
PHP5? I didn't know that you could use a constructor named after the class in PHP5. I thought it had to be __construct.

Posted: Tue May 29, 2007 9:05 am
by kguerrero
I had no idea ... I did check on php.net and according to them:
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
But, I should clean up my stuff and bring it to the latest standard.