Conditional statement issues with HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dubsies
Forum Newbie
Posts: 2
Joined: Wed Jul 15, 2009 8:12 pm
Location: Cincinnati, OH - USA

Conditional statement issues with HTML

Post by dubsies »

Hello all, I'm a PHP noob who's having issues with conditional statements. I ran into a problem in an include that has my navbar. I set up a simple if statement to show a selected class when that page is selected and that's working fine. However I wanted to add another condition to show a subnav and that's where the problem lies (line 13). I know about escaping quotes, but even when considering that I still can't get my code to work entirely. It's doing what I want it to, but the PHP still shows up in the source code which I know is incorrect. I've attached my code. Thanks in advance for your help.

Code: Select all

<div id="mainNav">
        <ul id="navLinks"> 
            <li><a href="/" <?php if ($varSection == "home") {echo "class=\"reverse\"";} ?>>home</a></li>
            <li><a href="/services/" <?php if ($varSection == "services") {echo "class=\"reverse\"";} ?>>services</a></li>
            <li><a href="/blog/" <?php if ($varSection == "blog") {echo "class=\"reverse\"";} ?>>blog</a></li>
            <li><a href="/links/" <?php if ($varSection == "links") {echo "class=\"reverse\"";} ?>>links</a></li>
            <li><a href="/about/" <?php if ($varSection == "about") {echo "class='selected'";} ?>>about</a></li>
            <li><a href="/contact/" <?php if ($varSection == "contact") {echo "class='selected'";} ?>>contact</a></li>
        </ul>
        <div id="blogSubscription"><a href="/" class="linkText">Subscribe to the blog</a><a href="/"><img src="/_media/icon_RSS.gif" class="iconRSS" alt="RSS icon" /></a></div>
    </div>
    <!-- END id mainNav -->
    <?php if ($varSection == "services") { 
        echo 
        "<div class='subnavCleaner'>&nbsp;</div>
        <div id='subnav'>
            <ul>
                <li><a href='/services/' <?php if ($varSection == 'services' && $varPage == 'default') {echo 'class=\'reverse\'';} ?>>about</a></li>
                <li><a href='/services/personal-liability.php' <?php if ($varSection == 'services' && $varPage == 'personalLiability') {echo 'class=\'reverse\'';} ?>>Personal Liability</a></li>
                <li><a href='/services/civil-litigation.php' <?php if ($varSection == 'services' && $varPage == 'civilLitigation') {echo 'class=\'reverse\'';} ?>>Civil Litigation</a></li>
                <li><a href='/services/estate-planning.php' <?php if ($varSection == 'services' && $varPage == 'estatePlanning ') {echo 'class=\'reverse\'';} ?>>Estate Planning</a></li>
            </ul>
        </div>"; 
    } else {
        echo "";
    }
    ?>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Conditional statement issues with HTML

Post by Reviresco »

You are echoing the php tags within the "echo". To simplify it, just exit the php tag after the first if statement. Also, notice where I changed single quotes to double and changed the spacing inside the links.

Code: Select all

<?php if ($varSection == "services") {
?>
        <div class="subnavCleaner">&nbsp;</div>
        <div id="subnav">
            <ul>
                <li><a href="/services/"<?php if ($varSection == 'services' && $varPage == 'default') {echo ' class="reverse"';} ?>>about</a></li>
                <li><a href="/services/personal-liability.php"<?php if ($varSection == 'services' && $varPage == 'personalLiability') {echo ' class="reverse"';} ?>>Personal Liability</a></li>
                <li><a href="/services/civil-litigation.php"<?php if ($varSection == 'services' && $varPage == 'civilLitigation') {echo ' class="reverse"';} ?>>Civil Litigation</a></li>
                <li><a href="/services/estate-planning.php"<?php if ($varSection == 'services' && $varPage == 'estatePlanning') {echo ' class="reverse"';} ?>>Estate Planning</a></li>
            </ul>
        </div>
<?php
    } else {
        echo "";
    }
?>
dubsies
Forum Newbie
Posts: 2
Joined: Wed Jul 15, 2009 8:12 pm
Location: Cincinnati, OH - USA

Re: Conditional statement issues with HTML

Post by dubsies »

Reviresco,
Thank you very much for your help. My problem is fixed. I appreciate your answer and resisting the urge to make fun of me for not seeing the obvious problem. I have quite a bit to learn still.

Thanks again,
EDW
Post Reply