Welcome Guest, Not a member yet? Register   Sign In
nav dont working properly
#1
Question 
(This post was last modified: 01-30-2024, 05:22 PM by derose.)

Hello, i already know the problem but i dont know how to fix
in the app/Views i have the layouts/blog/partials which contains the header and footer outside the partials i have the main file which load the layout, in the header i am trying to set my navbar like this:


Code:
<nav class="tm-nav" id="tm-nav">
            <ul>
                <li class="tm-nav-item <?= (uri_string() === '') ? 'active' : ''; ?>">
                    <a href="<?= base_url('') ?>" class="tm-nav-link">
                        <i class="fas fa-home"></i>
                        Inicio
                    </a>
                </li>
                <?php if (isset($post)) : ?>
                    <?php
                        $ultimoPost = end($post);
                        $ultimoPostId = isset($ultimoPost['post_id']) ? (string) $ultimoPost['post_id'] : '';
                        $ultimoPostURI = 'post/' . $ultimoPostId;
                        $currentURI = uri_string();
                    ?>
                    <li class="tm-nav-item <?= (strpos($currentURI, $ultimoPostURI) !== false) ? 'active' : ''; ?>">
                        <a href="<?= base_url($ultimoPostURI); ?>" class="tm-nav-link">
                            <i class="fas fa-pen"></i>
                            Último Post
                        </a>
                    </li>   
                <?php endif; ?>     
                <li class="tm-nav-item <?= (uri_string() === 'sobre') ? 'active' : ''; ?>">
                    <a href="<?= base_url('sobre') ?>" class="tm-nav-link">
                        <i class="fas fa-user"></i>
                        Quem Sou
                    </a>
                </li>
                <li class="tm-nav-item <?= (uri_string() === 'contato') ? 'active' : ''; ?>">
                    <a href="<?= base_url('contato') ?>" class="tm-nav-link">
                        <i class="fas fa-comments"></i>
                        Contato
                    </a>
                </li>
            </ul>
        </nav>


but the problem is, in my controller if i dont set in every function like this:

PHP Code:
$postModel = new PostModel();
$data = [
    "title"=> "Post",
    "post"=> $postModel->find($id),
]; 


the navbar doesnt work like is suppost to work, the thing i am thinking why this is happening is because i am passing the $post variavel inside the header.php who goes inside of every file of my views, but i dont know how to avoid that and fix to work like i want is in this:


Code:
<li class="tm-nav-item <?= (uri_string() === 'post/'.id) ? 'active' : ''; ?>">
    <a href="<?= base_url('post/'.id); ?>" class="tm-nav-link">
       <i class="fas fa-pen"></i>
         Último Post
     </a>                 
</li> 

the id is just an example, in the base url i want to go to the last id of my table post
pls everyone can correct me and give me some solution ?
Reply
#2

When usiing layouts I take and use the first view called dataview and it just passes all the

data first.
PHP Code:
$data = [
    'posts'      => $posts->getLivePosts()->paginate(3'group1'),
    'pager'      => $posts->pager,
    'currentPage' => $posts->pager->getCurrentPage('group1'),
    'totalPages'  => $posts->pager->getPageCount('group1'),
    'categories'  => $categories->getTopCategories(),
    'title'      => 'Blog Home',
    'pageHeading' => 'Blog',
    'subHeading'  => 'Home',
    'typography'  => Services::typography(),
];

/**
 * Make all variables global to all views. (Empty PHP File)
 * view_data is and empty file just for the data, works for
 * me.
 */
echo view('Insitefx\Blog\Views\view_data'$data);   // <-- Just the data nothing else!
echo view('Insitefx\Blog\Views\posts\index');        // <--layout view 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(01-31-2024, 05:38 AM)InsiteFX Wrote: When usiing layouts I take and use the first view called dataview and it just passes all the

data first.
PHP Code:
$data = [
    'posts'      => $posts->getLivePosts()->paginate(3'group1'),
    'pager'      => $posts->pager,
    'currentPage' => $posts->pager->getCurrentPage('group1'),
    'totalPages'  => $posts->pager->getPageCount('group1'),
    'categories'  => $categories->getTopCategories(),
    'title'      => 'Blog Home',
    'pageHeading' => 'Blog',
    'subHeading'  => 'Home',
    'typography'  => Services::typography(),
];

/**
 * Make all variables global to all views. (Empty PHP File)
 * view_data is and empty file just for the data, works for
 * me.
 */
echo view('Insitefx\Blog\Views\view_data'$data);   // <-- Just the data nothing else!
echo view('Insitefx\Blog\Views\posts\index');        // <--layout view 

I may have misunderstood, but I also use a navbar with variables without any problems. We have different objectives but the function is similar.

The site is local only, it is a catalog of albums, songs, movies, artists, etc. I have a Controller for each item, album, artist, etc., and each of them has specific views and css files, there is also a generic css file to avoid redundancy. There is a generic header and footer for all views.
CONTROLLER (partial view):
PHP Code:
  foreach ($this->allmediaModel->getName($id) as $row);

        $header = [
'appcss' => 'name/name',
'title' => "$row->name Songs, Albums, Reviews... | DataHome"
];

        echo view('templates/header'$header);
        echo view('content/name/name'$data);
        echo view('templates/footer');
    

Where:
'appcss' = location/css file
HEADER
PHP Code:
<!DOCTYPE html>
<
html lang="en">

<
head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/assets/css/bootstrap.css">
    <link rel="stylesheet" href="/assets/css/datahome.css">
    <link rel="stylesheet" href="/assets/css/<?= $appcss; ?>.css">
    <!-- Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <title><?= $title?></title>
</head>

<body>

    <nav class="navbar bg-dark navbar-dark py-3 navbar-expand-sm sticky-top">
        <div class="container">
            <a href="/" class="navbar-brand d-flex align-items-center">DataHome</a>
            <div class="collapse navbar-collapse">
                <div class="navbar-nav">
                    <a href="#" class="nav-link active">Home</a>
                    <a href="/blog" class="nav-link">Blog</a>
                    <a href="http://dhdata" class="nav-link">DHdata</a>
                    <a href="http://dhtest" class="nav-link">DHtest</a>
                    <a href="https://www.codeigniter.com" target="_blank" class="nav-link">CodeIgniter</a>
                    <!-- navbar-nav END -->
                </div>
                <!-- collapse END -->
            </div>
            <div class="search-container">
                <form action="/search" class="site-search" method="post" name="site-search">
                    <input class="site-search-button" name="wdeda" type="submit" />
                    <input class="site-search-input" name="search" placeholder="Search" tabindex="1" type="search" />
                </form>
                <!-- search-container END -->
            </div>
            <!-- container END -->
        </div>
        <!-- nav END -->
    </nav> 
and finally...
FOOTER
PHP Code:
<footer class="bg-dark navbar-dark">
    <div class="container">
        <div class="footer">
            <?php $year date("Y"); ?>
            DataHome: 2009 - <?= $year?> | &copy; no rights reserved.<br>
            Powered by: <a href="https://www.codeigniter.com" target="_blank">Codeigniter</a> / HAL9000
            <!-- footer END -->
        </div>
        <!-- container END -->
    </div>
</footer>
<!-- scripts -->
<script src="/assets/js/jquery-3.7.0.min.js"></script>
<!-- Bootstrap -->
<script src="/assets/js/popper.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script>
<!-- Font Awesome -->
<script src="https://kit.fontawesome.com/a2784e5f6c.js" crossorigin="anonymous"></script>
<!-- Progress Bar -->
<script src="/assets/js/progressbar.min.js"></script>
<!-- Parallax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/parallax/3.1.0/parallax.min.js"></script>

</body>

</html> 
I believe, if I understood correctly, your problem should be resolved.
Good luck!
If plan A fails, relax... the alphabet is 26 letters
Reply
#4

Move your $data to your header, it should be the first one loaded.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB