ricky spires Posted September 11, 2011 Share Posted September 11, 2011 hello. im having trouble putting an if statment in my loop this is my loop. <div id="ddtabs3" class="solidblockmenu"> <ul> <?PHP foreach($topNav as $topNavs): ?> <li><a href="<?PHP echo $topNavs->title; ?>.php" id="<?PHP echo $topNavs->title; ?>"><?PHP echo $topNavs->title; ?></a></li> <?PHP endforeach; ?> </ul> </div><!-- #ddtabs3--> i what to add an if $pageName = '$topNavs->title'; then <li class = selected> any ideas thanks rick Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/ Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 <li><a <?php echo ($pageName == $topNavs->title ? 'class="selected"' : '')?> href="<?php echo $topNavs->title; ?>.php" id="<?php echo $topNavs->title; ?>"><?php echo $topNavs->title; ?></a></li> Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1267995 Share on other sites More sharing options...
ricky spires Posted September 11, 2011 Author Share Posted September 11, 2011 thanks. i came up with this but i got an error <?PHP $topNav = navL1::find_all(); ?> <div id="ddtabs3" class="solidblockmenu"> <ul> <?PHP foreach($topNav as $topNavs): if ($pageName == '.$topNavs->title.';{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; } endforeach; ?> </ul> </div><!-- #ddtabs3--> [quote]Parse error: syntax error, unexpected ';' in /Applications/MAMP/htdocs/djsonrotation/admin/layouts/admin_topNav.php on line 13[/quote] can you see the error in my code ??? Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1267996 Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 <?php foreach($topNav as $topNavs){ if ($pageName == $topNavs->title){ echo '<li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; }else{ echo '<li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1267999 Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 the error was in the foreach declaration and in the conditional Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268000 Share on other sites More sharing options...
ricky spires Posted September 11, 2011 Author Share Posted September 11, 2011 cool. thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268013 Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 You´re welcome, don´t forget to mark the post as solved. Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268015 Share on other sites More sharing options...
ricky spires Posted September 11, 2011 Author Share Posted September 11, 2011 sorry. 1 more thing this code is for navigation . the code makes all the tabs selected if $pageName == '.$topNavs->title.' i just want 1 tab selected. "home" for example <?PHP foreach($topNav as $topNavs): if ($pageName == '.$topNavs->title.'){ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; } endforeach; ?> how do i get around that. ? Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268016 Share on other sites More sharing options...
marcelobm Posted September 11, 2011 Share Posted September 11, 2011 this is you script <?php foreach($topNav as $topNavs): if ($pageName == '.$topNavs->title.'){ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; } endforeach; ?> change it to this <?php foreach($topNav as $topNavs): if ($pageName == $topNavs->title){ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; } endforeach; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268018 Share on other sites More sharing options...
ricky spires Posted September 11, 2011 Author Share Posted September 11, 2011 Hello . Sorry if my replies are a long time apart i'm away from the computer a lot. i tried your code but it didn't seem to do the trick. it still make all navigation tabs selected if the page is = to the $topNavs->title. on home.php i have this line of code $pageName = 'Home'; and your header code is like this <?PHP foreach($topNav as $topNavs): if ($pageName == '.$topNavs->title.'){ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" >'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; } endforeach; ?> this is giving every <li> the class of selected. how can i give just the $pageName = 'Home'; selected and make the rest of the <li> unselected ??? or if im on one of the other tabs how would i make that tab selected and the rest not. do i need more if statements or something ???? thanks for all your help rick Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268103 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 <?PHP foreach($topNav as $topNavs): if ($pageName == '.$topNavs->title.'){ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'" class="selected">'.$topNavs->title.'</a></li>'; }else{ echo' <li><a href="'.$topNavs->title.'" id="'.$topNavs->title.'">'.$topNavs->title.'</a></li>'; } endforeach; ?> Add this as marcelobm stated. The code you provided is yours, not what marcelobm suggested. Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268106 Share on other sites More sharing options...
ricky spires Posted September 12, 2011 Author Share Posted September 12, 2011 your right. i should have just used marcelobm's code to start. that works great <li><a <?php echo ($pageName == $topNavs->title ? 'class="selected"' : '')?> href="<?php echo $topNavs->title; ?>.php" id="<?php echo $topNavs->title; ?>"><?php echo $topNavs->title; ?></a></li> thanks all rick Quote Link to comment https://forums.phpfreaks.com/topic/246906-need-help-putting-an-if-statment-in-my-loop/#findComment-1268253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.