Jump to content

Simple if, then, else


smordue

Recommended Posts

I am a real newbie at PHP and coding in general.

 

I am trying to write a statement that will show a list item if anything is in a particular line of a flat file, but not show it if it is blank. It is for a navigation menu list.

 

Something along the lines of this logic:

 

if

  line 5 of data.txt contains "about"

  then echo <li>About</li>

else

  show nothing

endif

 

Anybody know how to do this?

Link to comment
https://forums.phpfreaks.com/topic/184914-simple-if-then-else/
Share on other sites

You can't have nested PHP tags..

 

<?php
$file = file('data.txt');
if(strpos($lines[4], 'about') !== false)
{
     $class = (strpos($_SERVER['PHP_SELF'], 'about.php') ? 'active' : null;
     echo "<li class='$class'><a href='about_us.php'>About Us</a></li>";
}
?>    

Link to comment
https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976529
Share on other sites

Hmm, that throws this:

 

Parse error: syntax error, unexpected ';' in /nav.php on line 9

 

<div id="menu" class="container">
<ul>
        <li class="<?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'active';?>"><a href="index.php">Home</a></li>
<?php
$file = file('basedata.txt');
if(strpos($lines[18], 'about') !== false)
{
     $class = (strpos($_SERVER['PHP_SELF'], 'about.php') ? 'active' : null;
     echo "<li class='$class'><a href='about_us.php'>About Us</a></li>";
}
?> 
        <li class="<? if (strpos($_SERVER['PHP_SELF'], 'services.php')) echo 'active';?>"><a href="services.php">Services</a></li>

        <li class="<? if (strpos($_SERVER['PHP_SELF'], 'ourpeople.php')) echo 'active';?>"><a href="ourpeople.php">Our People</a></li>

        <li class="<? if (strpos($_SERVER['PHP_SELF'], 'photos.php')) echo 'active';?>"><a href="photos.php">Gallery</a></li>

        <li class="<? if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'active';?>"><a href="contact.php">Contact us</a></li>

</ul>
</div><!--end menu-->

 

I really appreciate the help.

Link to comment
https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976535
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.