In PHP how do I ensure both conditions are met in an if statement

I have two lines of working code which both work separately.

<?php if(!empty($gallery_link)) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>	
<?php if(!empty($multi_size)) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>	

Open in new window


One the same tab I show both a video and downloadable file attachments.
The first function checks if the video link was inserted, if not then it hides the tab.
The second one checks if any file attachments are present, if not it hides the tab.

What I need one if function to do is to check if either one or the other is present, and if yes then show the tab, otherwise remove it.


I tried this:
<?php if((!empty($multi_size)) && (!empty($gallery_link))) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>	

Open in new window

but it doesn't work properly. With this the tab is removed even if video link is present and file attachements are absent or vice versa.

thanks
badwolfffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris MillardCommented:
Try:-

<?php if((!empty($multi_size)) || (!empty($gallery_link))) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>
0
badwolfffAuthor Commented:
Thanks. It doesn't work. Now even if neither variable is present, the tab is not hidden at all.
0
badwolfffAuthor Commented:
In my case both conditions have to be met for the tab to be hidden.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Chris MillardCommented:
Actually I think my previous statement of || is an 'or' statement. If the && isn't working, can you nest the ifs as in:-

<?php
     if(!empty($multi_size)) {
          If(!empty($gallery_link))
               { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; }
          }
 ?>
0
Chris MillardCommented:
Or perhaps:-

<?php if((!empty($multi_size) && !empty($gallery_link))) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>
0
badwolfffAuthor Commented:
:(
Believe me I tried that too. It seems logical but it doesn't do it either.
As I said separately they work but not combined.
thanks
0
badwolfffAuthor Commented:
I just had a thought. Maybe I'm getting it wrong at the source?

Well the two divs that contain the video and the file attachements, respectively are:


<div id="vendor_video" class="cmsms_post_cont col-lg-6 col-sm-6 col-xs-12">

    <h1>Video</h1>
    
    <?php $parsed = parse_url($gallery_link); //echo "<pre>"; print_r($parsed['host']); exit; ?>
    
    <?php if($parsed['host'] == 'www.youtube.com') { ?>
    <?php  $gallery_link = substr($gallery_link, -11); ?>
    <?php $link =  "https://www.youtube.com/v/"."$gallery_link"; ?>			
        <div class="cmsms_video_wrap">				
            <iframe width="522" height="284" src="<?php echo $link; ?>"></iframe>
            <!--<iframe  width="522" height="284" src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=0"></iframe>	-->				
        </div>
    <?php } else { ?>
            <div class="cmsms_video_wrap">				
                <iframe width="522" height="284" src="<?php echo $gallery_link; ?>"></iframe>						
            </div>
    <?php }  ?>
</div>

Open in new window

<div class="multiple_files_section col-lg-6 col-sm-6 col-xs-12">
    <h1>Documenti Allegati</h1>
        <ul>
            <?php foreach($multi_size as $file) { ?><li>
                <?php if($file['extension']=='pdf') { ?><i class="fa fa-file-pdf-o"></i><?php  } else if($file['extension']=='txt') { ?><i class="fa fa-file-text"></i><?php } else if($file['extension']=='zip') { ?><i class="fa fa-file-archive-o"></i><?php } ?>
                <div class="file_name" style="width:300px; float:left; position:relative;left:48px;"><?php echo $file['file_name']; ?></div>
                <a href="download.php?filename=/<?php echo $file['path']; ?>"><span class="file_download" style="float:right;"><i class="fa fa-download"></i></span></a>
                <div class="file_size"><?php echo $file['file_size']; ?></div></li>	
            <?php } ?>
        </ul>
</div>

Open in new window


Am I putting the conditions before the wrong variables?
0
Julian HansenCommented:
If I understand you correctly a tab is displayed if

$multi_size contains data OR $gallery_link contains data

What does your $multi_size and $gallery_link data look like.

Can you post the result of these statements
<?php
echo '<pre>' . print_r($multi_size,true) . '</pre>';
echo '<pre>' . print_r($gallery_link) . '</pre>';

Open in new window

0
Chris MillardCommented:
Quite possibly. Where are you attempting to put the if statements?
0
badwolfffAuthor Commented:
This is the current condition of the code:
		<ul class="nav nav-tabs">
            <li class="active"><a href="#tab-description" data-toggle="tab" aria-expanded="true">L'azienda  </a></li> 
			
   			<!--<?php if(!empty($gallery_link)) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>
   			<?php if(!empty($multi_size)) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>-->	
            
            <!--<?php
                 if(!empty($multi_size)) {
                      If(!empty($gallery_link))
                           { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; }
                      }
            ?>-->             
            
<?php
echo '<pre>' . print_r($multi_size,true) . '</pre>';
echo '<pre>' . print_r($gallery_link) . '</pre>';
?>           
            
            <li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>
            <li class=""><a href="#tab-review" data-toggle="tab" aria-expanded="false">Recensioni e voti</a></li>
         </ul>
		<div class="tab-content">
            <div class="tab-pane active" id="tab-description">
            	<!--<h1>Il territorio</h1>-->
                    <p><?php echo $company_desc; ?>

</p>
			</div>
			<div class="tab-pane" id="tab-specification">
              
			  
			                  
            <div id="vendor_video" class="cmsms_post_cont col-lg-6 col-sm-6 col-xs-12">
            
                <h1>Video</h1>
                
                <?php $parsed = parse_url($gallery_link); //echo "<pre>"; print_r($parsed['host']); exit; ?>
                
                <?php if($parsed['host'] == 'www.youtube.com') { ?>
                <?php  $gallery_link = substr($gallery_link, -11); ?>
                <?php $link =  "https://www.youtube.com/v/"."$gallery_link"; ?>			
                    <div class="cmsms_video_wrap">				
                        <iframe width="522" height="284" src="<?php echo $link; ?>"></iframe>
                        <!--<iframe  width="522" height="284" src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=0"></iframe>	-->				
                    </div>
                <?php } else { ?>
                        <div class="cmsms_video_wrap">				
                            <iframe width="522" height="284" src="<?php echo $gallery_link; ?>"></iframe>						
                        </div>
                <?php }  ?>
            </div>
            
            <div class="multiple_files_section col-lg-6 col-sm-6 col-xs-12">
                <h1>Documenti Allegati</h1>
                    <ul>
                        <?php foreach($multi_size as $file) { ?><li>
                            <?php if($file['extension']=='pdf') { ?><i class="fa fa-file-pdf-o"></i><?php  } else if($file['extension']=='txt') { ?><i class="fa fa-file-text"></i><?php } else if($file['extension']=='zip') { ?><i class="fa fa-file-archive-o"></i><?php } ?>
                            <div class="file_name" style="width:300px; float:left; position:relative;left:48px;"><?php echo $file['file_name']; ?></div>
                            <a href="download.php?filename=/<?php echo $file['path']; ?>"><span class="file_download" style="float:right;"><i class="fa fa-download"></i></span></a>
                            <div class="file_size"><?php echo $file['file_size']; ?></div></li>	
                        <?php } ?>
                    </ul>
            </div>

Open in new window

0
Marco GasiFreelancerCommented:
There something wrong somewhere but not here. Following code just works:

<?php
//$multi_size='multi_size';
//$gallery_link='gallery_link';
//$multi_size='multi_size';
//$gallery_link='';
$multi_size='';
$gallery_link='gallery_link';
if ( !empty( $multi_size ) && !empty( $gallery_link ) )
{
	echo 'Both are true!';
}
 else
{
	 echo 'At least one is false';
}
?>

Open in new window


Try it, commenting and uncommenting appropriately and you'll see the output is the expected one. As you can see I deleted some uneeded parenthesis, but this doesn't matter: if you can't get your code work, look at it carefully (or post it here so we can help you with fresh eyes) and you should find the reason of the failing.
0
badwolfffAuthor Commented:
Hi Marco,

Your solution doesn't work. I tried it this way:

            <?php
                if(!empty( $multi_size ) && !empty($gallery_link))
                        {
                      echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>';
                }
            else{}
            ?>
0
Chris MillardCommented:
Just as a test, can you try this and let me know what messages you get...

<?php
     if(!empty($multi_size)) {
          echo 'multi_size is not empty';
          if(!empty($gallery_link))
               { echo 'gallery_link is not empty'; }
          }
 ?>

Then try the other way around and let me know what messages you get

<?php
     if(!empty($gallery_link)) {
          echo 'gallery_link is not empty';
          if(!empty($multi_size))
               { echo 'multi_size is not empty'; }
          }
 ?>
0
badwolfffAuthor Commented:
Hi Chris,

thanks.

Same links as before.
Both empty: http://demo.missionfamily.org/index.php?route=product/vendorstore&vendor_id=23

No video, but with attachments: http://demo.missionfamily.org/index.php?route=product/vendorstore&vendor_id=16

Video and attachments: http://demo.missionfamily.org/index.php?route=product/vendorstore&vendor_id=29


I have the first piece of code in right now.
0
badwolfffAuthor Commented:
by the way if you click on the "Video & allegati" tab after the output you can see what the contents of the tab are
0
badwolfffAuthor Commented:
Let me know when you are ready and I will insert the second piece of code
0
Chris MillardCommented:
Let me just backtrack a little. You need the tab to be shown if either the video OR the attachments are present - it doesn't matter which, just so long as one is present. Therefore you need the OR operand, not AND. Try this (I got the parentheses wrong in my earlier example)

<?php if(!empty($multi_size) || !empty($gallery_link)) { echo '<li class=""><a href="#tab-specification" data-toggle="tab" aria-expanded="false">Video & allegati</a></li>'; } ?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.