IF current record from list then change class - need a little php help please

8 posts by 3 authors in: Forums > CMS Builder
Last Post: March 12, 2013   (RSS)

By SubD - March 9, 2013

Hi guys, need a little help with some php.

I can't seem to write this correctly.

I'm trying to get the current record number or title and if it is the current one that we are on (detail page from list) then echo a class or id.

I've done this before by setting further up in the head

<?php $page = " yadda yadda " ; ?>

and looking for it in the navigation to change classes before  -but that way is more manual.I would really like to just change classes or ID's automatically by seeing which record we are on. I'm sure matching on 'num' would work or even 'title' or even if you could tease it from the URI after the '.php?'

Something like this.....

<?php foreach ($listRecords as $record): ?> 
<?php if ($pageName==($record['num'])) echo 'class="active"' ; ?>
           <?php else: ?>
            <?php echo 'class="inactive"' ; ?>
          <?php endif ?>
...
...
...
<?php endforeach ?>

Not working obviously...

Thanks in advance.

By Toledoh - March 9, 2013

does this help at all:

<?php 
function curPageURL() { 
 $pageURL = 'http'; 
 if (@$_SERVER["HTTPS"] == "on") {$pageURL .= "s";} 
 $pageURL .= "://"; 
 if ($_SERVER["SERVER_PORT"] != "80") { 
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
 } else { 
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
 } 
 return $pageURL; 
}
?>
<div id="preload">
   <img src="assets/images/mainBeersOver.png" alt="Beers" />
   <img src="assets/images/mainBreweryOver.png" alt="Beers" />
   <img src="assets/images/mainManlyOver.png" alt="Beers" />
   <img src="assets/images/mainPSWOver.png" alt="Beers" />
   <img src="assets/images/mainShopOver.png" alt="Beers" />
   <img src="assets/images/mainBeers.png" alt="Beers" />
   <img src="assets/images/mainBrewery.png" alt="Beers" />
   <img src="assets/images/mainManly.png" alt="Beers" />
   <img src="assets/images/mainPSW.png" alt="Beers" />
   <img src="assets/images/mainShop.png" alt="Beers" />
   <img src="assets/images/member_on.png" alt="Beers" />
   <img src="assets/images/member_off.png" alt="Beers" />
</div>

<?php $currentFile = strtolower(basename(curPageUrl())); ?> 
 


<div id="sitenavcontainer">
            <ul id="navlist">
                <li><a href="/index.php" class="home"></a></li>
                
<?php $pattern = '/^our_beers/'; ?> 
    <?php if ( preg_match ($pattern, $currentFile) ) : ?>  
        <li><a href="/our_beers/our_beers.php" class="beersActive">our beers</a></li>  
    <?php else:?>  
        <li><a href="/our_beers/our_beers.php" class="beers">our beers</a></li> 
    <?php endif ?>

                
<?php $pattern = '/^brewery/'; ?> 
    <?php if ( preg_match ($pattern, $currentFile) ) : ?>  
        <li><a href="/brewery/brewery.php" class="breweryActive">the brewery</a></li>  
    <?php else:?>  
        <li><a href="/brewery/brewery.php" class="brewery">the brewery</a></li>
    <?php endif ?>


<?php $pattern = '/^manly/'; ?> 
    <?php if ( preg_match ($pattern, $currentFile) ) : ?>  
        <li><a href="/manly/manlyWelcome.php" class="manlyActive">murray's @ manly</a></li>  
    <?php else:?>  
        <li><a href="/manly/manlyWelcome.php" class="manly">murray's @ manly</a></li> 
    <?php endif ?>
              
<?php $pattern = '/^port_stephens/'; ?> 
    <?php if ( preg_match ($pattern, $currentFile) ) : ?>  
        <li><a href="/port_stephens/port_stephens.php" class="pswActive">port stephens</a></li>  
    <?php else:?>  
        <li><a href="/port_stephens/port_stephens.php" class="psw">port stephens</a></li>
    <?php endif ?>

              
<?php $pattern = '/^sc-cart/'; ?> 
    <?php if ( preg_match ($pattern, $currentFile) ) : ?>  
        <li><a href="/shop/sc-cart-add.php?featured=1" class="shopActive">shop online</a></li>  
    <?php else:?>  
        <li><a href="/shop/sc-cart-add.php?featured=1" class="shop">shop online</a></li> 
    <?php endif ?>
            </ul>
        </div>

Cheers,

Tim (toledoh.com.au)

By SubD - March 10, 2013

Thanks for the follow up. I see what you are doing I think. This is matching the filename and changing the line item based on if a match is found in the regex pattern.

I can save this for later for sure but its not quite working in my case. I tried adding this to the function to pull the query string value and match the pageURL on that.

.$_SERVER['QUERY_STRING']

Didn't work. 
My match needs to happen on the query string since these are list records and not single files. If we continue with this solution, can you show me how to match the query string to the $currentFile

By Toledoh - March 10, 2013

Sorry SubD - I'll have to leave that the the iTools team.

Cheers,

Tim (toledoh.com.au)

By SubD - March 11, 2013

Well thanks. PHP guys, could you lend a hand?

THanks

Hi,

Does the details page have the num value in the URL string? If so, this is probably the easiest way to add a class of active:

<?php
  //Get the number value from the url
  $numValue = getLastNumberInUrl();

?>
<!-- list records, if record is currently being viewed, add a class of active. -->
<ul>
<?php foreach($blogs as $blog): ?>
  <li <?php if($blog['num'] == $numValue): ?>class="active"<?php endif; ?>><a href="<?php echo $blog['_link']; ?>" ><?php echo $blog['title']; ?></a></li>
<?php endforeach; ?>
</ul>

So the getLastNumberInUrl function will get set the last number in the URL to the variable numValue. Then in the foreach loop, if the num value equals the records value, a class of active is added.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By SubD - March 12, 2013 - edited: March 12, 2013

ah ha!

Thanks again.

This is within my foreach record code inside the image that needs to have the class changed when on the current record:

<?php if($record['num'] == $numValue): ?>class="active" <?php else:?> class="inactive"<?php endif; ?>

Simple an I can totally see where to use this in other applications for list detail pages.

:-)