Jump to content

Use of conditions (Switch/IF)


gansai

Recommended Posts


info_cond1();
info_cond2();
info_cond3();

(1) if info_cond1(), Then display DIV A
(2) if info_cond2(), Then display DIV B
(3) if info_cond3(), Then display DIV C

And neither should happen:

(4) if info_cond1() and info_cond2(), Then display DIV D
(5) if info_cond1() and info_cond3(), Then display DIV D
(6) if info_cond2() and info_cond3(), Then display DIV D
(7) if info_cond1() and info_cond2() and info_cond3(), Then display DIV D

(8) None of the above: display DIV E

Looking for the logic: only info_cond1() is allowed 
                       only info_cond1() is allowed
                       only info_cond1() is allowed

Could you suggest the logic with switch or if condition. 

<?php
info_cond1(); (output:4,5,6)
info_cond2(); (output:10,34,23)
info_cond3(); (output:3,45)

if(info_cond1())
{
	echo '<div>DIV A</div>';
}
elseif(info_cond2())
{
	echo '<div>DIV B</div>';
}
elseif(info_cond3())
{
	echo '<div>DIV C</div>';
}
elseif(info_cond1() || info_cond2())
{
	echo '<div>DIV D</div>';
}
elseif(info_cond2() || info_cond3())
{
	echo '<div>DIV D</div>';
}
elseif(info_cond1() || info_cond2() || info_cond3())
{
	echo '<div>DIV D</div>';
}
else
    echo '<div>DIV E</div>';

?>

 

Link to comment
Share on other sites

Could you ignore the php code and can I get the logic from the description:

(1) if info_cond1(), Then display DIV A
(2) if info_cond2(), Then display DIV B
(3) if info_cond3(), Then display DIV C

And neither should happen:

(4) if info_cond1() and info_cond2(), Then display DIV D
(5) if info_cond1() and info_cond3(), Then display DIV D
(6) if info_cond2() and info_cond3(), Then display DIV D
(7) if info_cond1() and info_cond2() and info_cond3(), Then display DIV D

Link to comment
Share on other sites

i would start with three separate if(){} statements to test each of the cond1, 2, and 3 values. for any that are true, i would increment a counter and set a common variable with the corresponding A, B, or C content.

after the above logic, if the counter is zero, output the E content. if the counter is one, output  whatever is in the common variable that was set in the above logic. else output the D content.

Link to comment
Share on other sites

Thank You for the logic.

If I start with three separate functions (1), (2), (3), the conditions coincide with  (4), (5), (6), (7). Should I add the counter after: 

<?php
info_cond1(); 
info_cond2();
info_cond3();

if(info_cond1())
{
	echo '<div>DIV A</div>';
}
elseif(info_cond2())
{
	echo '<div>DIV B</div>';
}
elseif(info_cond3())
{
	echo '<div>DIV C</div>';
}
....counter here...
elseif(info_cond1() || info_cond2())
{
	echo '<div>DIV D</div>';
}
elseif(info_cond2() || info_cond3())
{
	echo '<div>DIV D</div>';
}
elseif(info_cond1() || info_cond2() || info_cond3())
{
	echo '<div>DIV D</div>';
}
else
    echo '<div>DIV E</div>';

?>

 

Link to comment
Share on other sites

lol, i stared to post a reply, but the authors of this forum software made it almost impossible to compose a reply containing quotes, code, and new text, that i couldn't produce the result that i wanted.

start by learning what an if(){} statement looks like, and what it does when the condition being tested is a true value. then, write three if(){} statements, one testing each info_condx()

 

Link to comment
Share on other sites

42 minutes ago, mac_gyver said:

almost impossible to compose a reply containing quotes, code, and new text,

45 minutes ago, mac_gyver said:

i couldn't produce the result that i wanted.

:confused:  ... But not completely impossible

Three quotes, new text and two lines of code

1 hour ago, gansai said:

Could you post atleast 2 lines of sample code.

<?php
?>
Link to comment
Share on other sites

You need to clarify some things.  First off, do the info_*() functions return either a boolean or boolean compatible value?  In one post you illustrate that those functions might return either a single integer value or even an array of values.  Nothing anyone might propose is going to work unless we are clear what those functions return.

Assuming they do return boolean values, then we can proceed to the "Div" portion of this question where you apparently have 4 seperate divs of information.

What is also not clear, in the way the question is presented is whether or not the first section is expected to be exclusive of the 2nd section.  I'm going to assume that it is.

So... you want to determine if you should present a 'D' div, and only if there is no criteria that satisfies that would you present an A, B, or C div.

 

Quote

1) if info_cond1(), Then display DIV A
(2) if info_cond2(), Then display DIV B
(3) if info_cond3(), Then display DIV C

And neither should happen:

(4) if info_cond1() and info_cond2(), Then display DIV D
(5) if info_cond1() and info_cond3(), Then display DIV D
(6) if info_cond2() and info_cond3(), Then display DIV D
(7) if info_cond1() and info_cond2() and info_cond3(), Then display DIV D

When you concentrate on the 'D' section first, you see that 3 out of the 4 involve info_one === true AND some other combination of cond2 or cond3.

 

<?php

if (info_cond1() && (info_cond2() || info_cond3()) {
    echo $div4;
} else if (info_cond2() && info_cond3()) {
    echo $div4;
} else if (info_cond1()) {
  echo $div1;
} else if (info_cond2()) {
  echo $div2;
} else if (info_cond3()) {
  echo $div3;
}
// It's possible that none of these conditions are true as well.  What do you do then?

 

The first 2 if-then tests could be combined into one using OR.  It would be essentially the same if the initial condion was:

if ((info_cond1() && (info_cond2() || info_cond3()) || (info_cond2() && info_cond3())) {
    echo $div4;
else if ...

Code like this indicates that the programmer isn't interested in maintainability, testability, unit testing, structure or code design.  My original solution was meant to illustrate the point.

I don't know if this is a homework question, but when I see something like this,  it seems pretty obvious there has to be some better design than a string of chained if-then-else statements.  It's brittle code that can be completely broken by even the smallest adjustment to the required logic.

 

Link to comment
Share on other sites

//Get the count of true conditions
$conditionCount = 0;
$conditionCount += (info_cond1()) ? 1 : 0;
$conditionCount += (info_cond2()) ? 1 : 0;
$conditionCount += (info_cond3()) ? 1 : 0;

//If only one condition, set correct div
if($conditionCount==1)
{
    //Only one condition is true
    if(info_cond1()) { echo '<div>DIV A</div>'; }
    if(info_cond2()) { echo '<div>DIV B</div>'; }
    if(info_cond3()) { echo '<div>DIV C</div>'; }
}
elseif($conditionCount>1)
{
    //Multiple conditions are true
    echo '<div>DIV D</div>';
}
else
{
    //No conditions are true
    echo '<div>DIV E</div>';
}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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