In the following code snippet while loop does not stops while condition become false.
public function isCircularDependancy($taskId, $newParentId){
global $pdoHandlerObject;
$task = $this->getTaskById($taskId);
if($newParentId == 0){ // This is root task there is no parent
return false;
}
if($task->parent_id == $newParentId){ // Parent is not changed
return false;
}
$currentParentId = $newParentId;
while($currentParentId != 0){
if($currentParentId == $task->id){
return true;
}
$currentParent = $this->getTaskById($currentParentId);
$currentPatentId = $currentParent->parent_id;
}
return false;
}
Related
For true I'm receiving "Successfully UNENLISTED(1)!" and for false I'm receiving "Successfully ENLISTED(1)!".
dump($swap) will return true and still execute the IF else while if it returns false it still execute as true
Am i not understanding something specific to CakePHP?
//to grab database entry
public function swapSub($user_id_main, $user_id_from) {
$result = $this->find()
->where(['Enlist.user_id_main'=>$user_id_main,'Enlist.user_id_for'=>$user_id_from])
->first();
dump($result);
if($result['active']){
$result['active'] = 0;
if($this->save($result)){
return false;
}
} else {
$result['active'] = 1;
if($this->save($result)){
return true;
}
}
}
//display true and false statement. //$swap == TRUE/does not work either
$swap = $this->Enlist->swapSub($user_id_main, $user_id_from);
dump($swap);
if($swap){
$this->Flash->success(__('Successfully ENLISTED(1)!'));
} else {
$this->Flash->success(__('Successfully UNENLISTED(1)!'));
}
I want to check if the user is using the default settings. In the example below, I'm trying to check if all "foreached" items return true. If a single foreached item doesn't return true, return false on the whole function.
private function is_using_default_settings() {
// returns a huge array with settings
$merged_preset = $this->options_merged();
foreach($merged_preset as $preset) {
if($preset[5] == 1) {
$section = 'general';
} elseif($preset[5] == 2) {
$section = 'advanced';
} elseif($preset[5] == 3) {
$section = 'technical';
}
$option = get_option($section);
if($preset[3] == $option[$preset[0]] && !is_null($preset[1])) {
return true;
}
}
return false;
}
I've been brainstorming for the past few days to get this sorted on my own, but sadly cannot get it to work. What is the best approach to this?
you can check when is false and block the full foreach then return value, if all is true return value true
try this:
private function is_using_default_settings() {
$returnValue = true;
$merged_preset = $this->options_merged();
foreach($merged_preset as $preset) {
if($preset[5] == 1) {
$section = 'general';
} elseif($preset[5] == 2) {
$section = 'advanced';
} elseif($preset[5] == 3) {
$section = 'technical';
}
$option = get_option($section);
if($preset[3] != $option[$preset[0]] || is_null($preset[1])) {
$returnValue = false;
break;
}
}
return $returnValue;
}
You should return false when any check fails in the foreach, otherwise return true.
function check()
{
foreach($arr as $v)
{
//check fails
if(fail of the check)
return false;
}
return true;
}
I have a script like this:
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1){
$something = $db->prepare('SELECT cookie FROM users WHERE id = ?');
$something->execute(array($_SESSION["Id"]));
$num_row = $something->fetch();
$_SESSION["cookie"] = $num_row['cookie'];
if ( $_SESSION["cookie"] != $_COOKIE['login']) ){
// jump to following else statement (outer else statement)
}
} else {
/* here - this block should be execute when
- That inner if-statement is true
OR
- That outer if-statement is false
*/
}
As you see, I need to execute if-statement and if inner if-statement is true then execute else-statement. How can I do that?
I think that is a algorithm problem and not php. Anyway, there are several ways to do. For example:
$innerCondition = false;
$outerCondition = false;
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1) {
$outerCondition = true;
// ...
if ($_SESSION["cookie"] != $_COOKIE['login']) {
$innerCondition = true;
// ...
}
}
if ($innerCondition || !$outerCondition) {
// ...
}
Try something like this:
$doElse = true;
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1){
$doElse = false;
$something = $db->prepare('SELECT cookie FROM users WHERE id = ?');
$something->execute(array($_SESSION["Id"]));
$num_row = $something->fetch();
$_SESSION["cookie"] = $num_row['cookie'];
if ( $_SESSION["cookie"] != $_COOKIE['login']) ){
$doElse = true;
}
else {
//rest of the logic
}
}
if ($doElse) {
// here - this block should be execute when inner if-statement is true
}
Creating a block builder that loops through blocks pulled form database in order.
if( loop_blocks()) {
while( loop_blocks()) {
if( have_block('standard-content-block')) {
echo 'standard-content-block';
}
elseif( have_block('executive-intro-block')) {
echo 'executive intro block';
}
}
}
My function loop_blocks pulls the blocks from the database in order and set the array as a global variable:
function loop_blocks() {
global $db;
$page_id = get_page_id();
$GLOBALS['loop_position'] = 0;
$loop_position = $GLOBALS['loop_position'];
$stm = $db->prepare("SELECT * FROM page_blocks WHERE page_id = :id ORDER BY `block_order` ASC");
$stm->execute(array(':id' => $page_id));
$res = $stm->fetchAll();
$GLOBALS['block_loop'] = $res;
if(!$res) {
return false;
} elseif(!$GLOBALS['block_loop'][$loop_position]) {
return false;
} else {
return true;
}
}
The function have_block gets the current loop position and determines whether the name as determined, exists in the array and increases the loop position:
function have_block($block_name) {
$loop_position = $GLOBALS['loop_position'];
if(!$GLOBALS['block_loop'][$loop_position]) {
return false;
} elseif(!$GLOBALS['block_loop'][$loop_position][block_name] = $block_name) {
return false;
} else {
$GLOBALS['loop_position'] = $loop_position+1;
return true;
}
}
This returns an infinite loop however and I can't figure out a way to move the while loop onto the next step?
EDIT I'm using a while loop because the function have_block will set-up a global variable for the current block id. This will then be used within a function called the_element. Such as:
if( loop_blocks()) {
while( loop_blocks()) {
if( have_block('standard-content-block')) {
the_element('heading');
}
}
}
If I don't use the function have_block to set this up, then I'd need to pass the block id from the foreach loop into every element as a second argument.
I fixed this by, as #Jim, noted I was re-setting the loop_position within loop_blocks() which was why the while loop was repeating infinitely. It was then a simple case of an error when typing:
} elseif(!$GLOBALS['block_loop'][$loop_position][block_name] = $block_name) {
return false;
Should have been:
} elseif($GLOBALS['block_loop'][$loop_position][block_name] != $block_name) {
return false;
Note that I had the exclamation point in the incorrect place.
This now works perfectly as I need.
I have a function, as shown in the code below, that should return true if the code is executed correctly. Nothing new.
The problem is that the function calls other functions, each returns true or false.
I'm trying to figure out how to construct the logic to validate the output of the function checking the code of the function itself, and the returns of the other functions. The other functions might be called or not, depending on the $this->conf['functionName'] parameter, which is also a boolean.
public function execute() {
$return = false;
if ($this->conf['functionOne']) {
$this->functionOne();
}
if ($this->conf['functionTwo']) {
$this->functionTwo();
}
if ($this->conf['functionThree']) {
$this->functionThree();
}
return $return;
}
I would do this
public function execute() {
$function_list = ['functionOne', 'functionTwo', 'functionThree'];
$return = true;
foreach ($function_list as $function) {
if ($this->conf[$function]) {
if (!$this->{$function}() && $return) {
$return = false;
}
}
}
return $return;
}
It's not quite clear but I think you mean something like this...
public function execute() {
$res1 = $res2 = $res3 = true;
if($this->conf['functionOne']){
$res1 = $this->functionOne();
}
if($this->conf['functionTwo']){
$res2 = $this->functionTwo();
}
if($this->conf['functionThree']){
$res3 = $this->functionThree();
}
return ($res1 && $res2 && $res3);
}
Do you mean this:
public function execute() {
$return = false;
if($this->conf['functionOne']){
$return = $this->functionOne();
}
if($this->conf['functionTwo']){
$return = $this->functionTwo();
}
if($this->conf['functionThree']){
$return = $this->functionThree();
}
return $return;
}
Is this works ?
public function execute() {
$f1 = $f2 = $f3 = false ;
if($this->conf['functionOne']){
$f1 = $this->functionOne();
}
if($this->conf['functionTwo']){
$f2 = $this->functionTwo();
}
if($this->conf['functionThree']){
$f3 = $this->functionThree();
}
if($f1=== true && $f2===true && $f3===true)
return true;
else
return false ;
}