smordue Posted December 12, 2009 Share Posted December 12, 2009 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 More sharing options...
Alex Posted December 12, 2009 Share Posted December 12, 2009 $file = file('data.txt'); if(strpos($file[4], 'about') !== false) { echo "<li>About</li>"; } file strpos Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976148 Share on other sites More sharing options...
smordue Posted December 12, 2009 Author Share Posted December 12, 2009 Thanks Alex, when I insert that code the browser says: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /nav.php on line 13 referring to this line: echo "<li>About</li>"; Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976161 Share on other sites More sharing options...
Alex Posted December 12, 2009 Share Posted December 12, 2009 There's no error there, post your entire code. Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976184 Share on other sites More sharing options...
smordue Posted December 13, 2009 Author Share Posted December 13, 2009 <? $file = file('data.txt'); if(strpos($lines[4], 'about') !== false) { echo "<li class='<?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'active';?>'><a href='about_us.php'>About Us</a></li>"; } ?> Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976347 Share on other sites More sharing options...
smordue Posted December 13, 2009 Author Share Posted December 13, 2009 I think this is the script in the script issue, how can I rewrite this? Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976509 Share on other sites More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 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 More sharing options...
smordue Posted December 13, 2009 Author Share Posted December 13, 2009 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 More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 Forgot a closing ), sorry. $class = (strpos($_SERVER['PHP_SELF'], 'about.php')) ? 'active' : null; Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-976537 Share on other sites More sharing options...
smordue Posted December 14, 2009 Author Share Posted December 14, 2009 Awesome Alex, thanks Link to comment https://forums.phpfreaks.com/topic/184914-simple-if-then-else/#findComment-977173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.