thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Main Navigation - .active not showing #178

Closed dbashby closed 8 years ago

dbashby commented 8 years ago

Prior to video 13 having static links the active state worked. With the help of Creptor on some previous code due to trying to convert all the MySQLi to PDO I have managed to get the navigation up on the site, on click does what is expected and fetches the results however the active class is not working anymore. I can only put this down to my PDO

HTML & PHP Code - navigation.php `

`



When I look at the inspect I see that class is not in the code yet I have styled it in the css as I say it was working when I had static links.  What have I missed?  Thanks

![screenshotnav1](https://cloud.githubusercontent.com/assets/6895380/13901166/aa5bcf2e-ee12-11e5-89df-23a85cf81ddd.png)
creptor commented 8 years ago

maybe the $padeid is null, try to echo out that value to check that it works.

dbashby commented 8 years ago

I tried <?php echo $nav['id'] . ' ' . $pageid . ' ';?> and in return I got `

dbashby commented 8 years ago

So I have now got the active class working with 1 change

`#Main nav function function nav_main($pdo, $pageid, $nav){ $stmt = $pdo->query("SELECT * FROM posts"); $stmt->bindParam(1,$pageid,PDO::PARAM_INT); $stmt->execute(); foreach($stmt as $nav){ ?>

        <?php //echo $nav['id'] . ' ' . $pageid; ?>

        <li<?php if($pageid == $nav['id']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['id']; ?>"><?php echo $nav['label']; ?></a></li>
 <?php }
    }`

In the

  • I have had to change the <?php echo $nav['slug']; to <?php echo $nav['id'];

    And everything works however this now means that I no longer have the slug coming out in the address bar but I do get the following on view source

    `

  • Home
  •         <li class="active"><a href="?page=2">About</a></li>
    
            <li><a href="?page=3">FAQ</a></li>
    
            <li><a href="?page=4">Contact</a></li>

    `

    So now I need to find out why I cannot use the slug rather than the id.

    dbashby commented 8 years ago

    When trying to echo

    <?php echo 'nav id: ' . $nav['id'] . '<br>' . 'pageid: ' . $pageid . '<br>' . '$nav slug: ' . $nav['slug']; ?>

    I get

    nav id: 1 $pageid: home $nav slug: home

    so the pageid is picking up the label rather than the id, however the $pageid is always the selected page and none of the other options show.

    dbashby commented 8 years ago

    Working

    <li<?php if($pageid == $nav['slug']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['slug']; ?>"><?php echo $nav['label']; ?></a></li>

    As page id is taking the label, on selection will now equal the slug.