Jump to content

Incrementing a value in a class from different methods?


Go to solution Solved by Ansego,

Recommended Posts

Hi guys,

 

Purpose of flag: Lets me know when all validations have been done and passed before I enter data into the database.

Having some troubles trying to increment a value:

protected $FlagCount;
public function set_FlagCount(){

		//self::$counter++;
	$this->FlagCount++;
	//return $this;
}
public function get_FlagCount(){
	return $this->FlagCount;
}
// FULL NAME
public function set_FullName($sFullName){
	if (preg_replace("/[^A-Za-z\s]/", "", $sFullName) == $sFullName && filter_var($sFullName, FILTER_SANITIZE_STRING) && strlen($sFullName) >= 2) {
		$FullName = filter_var($sFullName, FILTER_SANITIZE_STRING);
		$this->set_FlagCount();
		$this->FlagCount++;
		$this->FlagCount = $this->FlagCount + 1;
		return true;
	} else {
		return false;
	}
}

If you are instantiating an object of a class that contains those functions then upon calling set_FullName() if the if condition evaluates to true then the object var $this->FlagCount will be incremented 3 times.

 

You haven't shown enough code or where you are attempting to see $this->FlagCount and it has not been incremented.

Hi.

 

What I am trying to achieve here is a count; IF the method was successful increment by 1. So If I check Name passed; flag + 1 | Email passed; flag + 1 and so on throughout the checks. The count should ie be 10 and then the flag is to tell me that all fields have passed their checks then go to the next stage of INSERT into the db. But I am unable to get a simple counter system to work.

// AUTO LOAD SEEK.INC CLASS
function __autoload($classname) {
    $filename = "./inc/". $classname .".inc";
    include_once($filename);
}
$Seek = new seek();
// COLLECTING AND SETTING VARIBLES
if (isset($_POST['Fullname'])) {
	$sFullName = $_POST['Fullname'];
} else {
	$sFullName = "";	
}

if (isset($_POST['Email'])){
	$sEmail = $_POST['Email'];
} else {
	$sEmail = "";
}
// TEST
echo "FCNT:" . $Seek->get_FlagCount();
echo " CNT" . seek::$counter;
        <em class="frmreq"><?php if ($Seek->set_FullName($sFullName) == false && isset($_POST["seek_legalaid"])){ echo "Required!"; } ?></em>
        <input class="FormFieldInput" type="text" name="Fullname" id="Fullname" placeholder=" Full Name" value="<?php if (isset($_POST["Fullname"])) { echo $_POST["Fullname"]; }; ?>"/> 
        
        <em class="frmreq"><?php if ($Seek->set_Email($sEmail) == false && isset($_POST["seek_legalaid"])){ echo "Required!"; } ?></em>
        <input class="FormFieldInput" type="text" name="Email" id="Email" placeholder=" Email" value="<?php if (isset($_POST["Email"])) { echo $_POST["Email"]; }; ?>" />

protected $FlagCount;
public static $counter = 0;

protected $Testing;

// ++++++++++++++++++++++++++++++++++++++++++++++++ CONSTRUCT
    public function __construct() {
		
				$this->set_FlagCount();
        //echo "Seek class successfuly!";
		//$FlagCount = 1;
		//self::$counter++;

//new seek();
//new seek();	
		
    }
// FULL NAME
public function set_FullName($sFullName){
	if (preg_replace("/[^A-Za-z\s]/", "", $sFullName) == $sFullName && filter_var($sFullName, FILTER_SANITIZE_STRING) && strlen($sFullName) >= 2) {
		$FullName = filter_var($sFullName, FILTER_SANITIZE_STRING);
		$this->set_FlagCount(); // TEST
		$this->FlagCount++; // TEST
		$this->FlagCount = $this->FlagCount + 1; // TEST
		return true;
	} else {
		return false;
	}
}

public function get_FullName(){
	return $this->Fullname;
}
// EMAIL
public function set_Email($sEmail){
	if (filter_var($sEmail, FILTER_VALIDATE_EMAIL) && filter_var($sEmail, FILTER_SANITIZE_EMAIL)){
		$Email = filter_var($sEmail, FILTER_SANITIZE_EMAIL);
                $this->set_FlagCount(); // TEST
                $this->FlagCount++; // TEST
                $this->FlagCount = $this->FlagCount + 1; // TEST
		return true;
	} else {
		return false;
	}
//$this->Email = $sEmail;
//return $this;
}
public function get_Email(){
	return $this->Email;
}
  • Solution

All good, went old school on it; Did not fix the problem but was a solution. I am curious why one is unable to jump from method to method and share a variable...

$flagcnt = 0;

if ($Seek->set_FullName($sFullName) == true){$flagcnt = $flagcnt + 1;}
if ($Seek->set_Email($sEmail) == true){$flagcnt = $flagcnt + 1;}
...
...
...
...
...
if ($flagcnt == 7){$Seek->dbsql();}
Edited by Ansego
This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.