geek_10101010
geek_10101010

Reputation: 90

CodeIgniter 3: Multiple condition in if statement not working in Php

I'm new with CodeIgniter. I have an if/if else statement in my view file and the codition after || is not working

Here's a code from my view

<?php if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips")){
?>


<section id="profilephp-header" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
    <div class="overlay">
        <div class="container">
            <div class="row">
                <div class="sec-title text-center wow animated fadeInDown">
                    <img src="<?php echo base_url('assets/img/parallax/profile-text.png'); ?>" alt="">
                </div>
            </div>
        </div>
    </div>
</section>
<?php 
}else if(($destination["site"]=="block_view/profile/profile")||($destination["site"]=="block_view/profile/slips")){?>
    <section id="profilephp-header2" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
        <div class="overlay">

        </div>
    </section>
<?php
}?>

It seems like the if statement is only accepting one condition. All the conditions in my view files is like this.

Upvotes: 1

Views: 1296

Answers (3)

user9170602
user9170602

Reputation:

replace your <?php if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips")){ ?> with && condition you might got confused with the two.

Upvotes: 0

Rafalon
Rafalon

Reputation: 4515

As I said in my comment, $destination["site"] is always either

  • not "block_view/profile/profile"
    or
  • not "block_view/profile/slips"
    so your if statement is always true.

You might want to write

if (($destination["site"]!="block_view/profile/profile")&&($destination["site"]!="block_view/profile/slips"))

instead of

if (($destination["site"]!="block_view/profile/profile")||($destination["site"]!="block_view/profile/slips"))

(replace || - OR - with && - AND)


Addition to my answer :

You seemed - as per your question - to believe that !a || !b is the same as !(a || b).

You have to know that when you negate a big statement like this, you have to revert && and || operands as you distribute the negation.

Basic example :

  • I want to find a sock which is not red or blue (!(red || blue))
    is the same as
  • I want to find a sock which is neither red nor blue (!red && !blue)

Upvotes: 3

Chris Forrence
Chris Forrence

Reputation: 10104

$destination['site'] has one value, but in your first if-statement, you're checking if it's either not equal to one thing, or it's not equal to another thing. That'll always be true!

It looks like you were intending to show that section if the destination site was neither of those, so you could rewrite it as this:

<?php if (!in_array($destination['site'], ['block_view/profile/profile', 'block_view/profile/slips'])): ?>
<section id="profilephp-header" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
    <div class="overlay">
        <div class="container">
            <div class="row">
                <div class="sec-title text-center wow animated fadeInDown">
                    <img src="<?php echo base_url('assets/img/parallax/profile-text.png'); ?>" alt="">
                </div>
            </div>
        </div>
    </div>
</section>
<?php else: ?>
    <section id="profilephp-header2" style="background-image: url(<?php echo base_url('assets/img/parallax/header-profile.jpg'); ?>);" class="parallax">
        <div class="overlay">

        </div>
    </section>
<?php endif; ?>

Upvotes: 2

Related Questions