0

So I have this piece of code that I am trying to work with. What I would like to do is to run the code only when $formname == 4 and when it's not, I don't want the code to run. I tried using an if statement on the whole block of code but for some reason that did not work for me. I'm also pretty sure that there is a better way to write this code since all of my if statements have the same condition, so any suggestions on condensing the code would be greatly appreciated. I am fairly new to programming so pardon my lack of knowledge, and thanks in advance for any help.

<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php if($formname == 4 AND $formID == null){ echo "style='display:none'" ;} ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/overview.php';  
    </div>
</div>

<?php $a++; ?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php if($formname == 4 AND $formID == null){ echo "style='display:none'" ;} ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/abroad.php'; 
        ?>
    </div>
</div>

<?php $a++;?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php if($formname == 4 AND $formID == null){ echo "style='display:none'" ;} ?> >
    <div class="tabs-content3">
        <?php 
            include 'Subpages/safety.php';  
        ?>
    </div>
</div>

<?php $a++; ?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php if($formname == 4 AND $formID == null){ echo "style='display:block'" ;} ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/faqs.php';  
        ?>
    </div>
</div>
2
  • share complete code and from where the variable $formname is coming ? Also tell me about $formID variable, that when it is NULL and when it is set? Also explain the relation between these two variables. Commented Feb 12, 2014 at 6:34
  • $formname depends on what link the user uses to get to the page, since there are multiple links for the page. Commented Feb 12, 2014 at 7:09

3 Answers 3

1

You look for something like this?

<?php if($formname == 4 AND $formID == null):?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>">
    <div class="tabs-content3">
        <?php 
            include 'Subpages/overview.php'; 
         ?> 
    </div>
</div>

<?php $a++; ?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" >
    <div class="tabs-content3">
        <?php 
            include 'Subpages/abroad.php'; 
        ?>
    </div>
</div>

<?php $a++;?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" >
    <div class="tabs-content3">
        <?php 
            include 'Subpages/safety.php';  
        ?>
    </div>
</div>

<?php $a++; ?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>">
    <div class="tabs-content3">
        <?php 
            include 'Subpages/faqs.php';  
        ?>
    </div>
</div>
<?php endif; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Try the following:

<?php
$sty = "";

if($formname == 4 && $formID == null)
{ $sty = "style='display:none'"; }
?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php echo $sty; ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/overview.php';  
        ?>
    </div>
</div>

<?php $a++; ?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php echo $sty; ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/abroad.php'; 
        ?>
    </div>
</div>

<?php $a++;?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php echo $sty; ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/safety.php';  
        ?>
    </div>
</div>

<?php
$a++;
if($formname == 4 && $formID == null)
{ $sty = "style='display:block'"; }
?>
<div class="tabcontent2" id="tab_content_<?php echo $a; ?>" <?php echo $sty; ?>>
    <div class="tabs-content3">
        <?php 
            include 'Subpages/faqs.php';  
        ?>
    </div>
</div>

1 Comment

Yeah that works too. There's less repetition with this one, so thanks!
0

You Might had forgot the closing of PHP tag after:

 include 'Subpages/overview.php';

For display of the div you might use simple if else for example

<?php
       if(($formname == 4) && ($formID == null)){
      ?>

YOUR HTML TO DISPLAY

 <?php } ?>

Similarly you can display as many if conditions as you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.