Jump to content

need help putting an if statment in my loop


ricky spires

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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 ???


Link to comment
Share on other sites


<?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>';
}
}?>

Link to comment
Share on other sites

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. ?

Link to comment
Share on other sites

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; ?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

<?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.

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.