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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.