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

 

 

 

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



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

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

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

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

 

 

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

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

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.