简体   繁体   中英

PHP undefined variable $page_id - Includes

I have the following code: this is index.php initialize.php init the database connection

<?php require_once('../private/initialize.php'); ?>

<?php 
    
    if(isset($_GET['id'])){
        $page_id = $_GET['id'];
        $page = find_page_by_id($page_id);
        if(!$page){
            redirect_to(url_for('/index.php'));
        }
    }
?>


<?php include(SHARED_PATH.'/public_header.php'); ?>

<div id="main">

    <?php include(SHARED_PATH.'/public_navigation.php');  ?>
    
    <div id="page">
    
        <?php
        if(isset($page)){
            echo $page['content'];
        } else { 
            include(SHARED_PATH.'/static_homepage.php'); 
        }
        ?>
    </div>
    
</div>


<?php include(SHARED_PATH.'/public_footer.php'); ?>

here is the public_navigation.php bellow:

<navigation>
    <?php 
      echo $page_id;
      exit;
    ?>
    <?php $nav_subjects = find_all_subjects(); ?>
    
    <ul class="subject">
        <?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)) { ?>
            <li>
                <a href="<?php echo url_for('/index.php'); ?>">
                    <?php echo h($nav_subject['menu_name']); ?>
                </a>
                    <?php $nav_pages = find_pages_by_subject_id($nav_subject['id']); ?>
    
                    <ul class="pages">
                        <?php while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
                            <li class="<?php if($nav_page['id'] == $page_id){ echo 'selected'; } ?>">
                                <a href="<?php echo url_for('/index.php?id='.h(u($nav_page['id']))); ?>">
                                    <?php echo h($nav_page['menu_name']); ?>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                    <?php mysqli_free_result($nav_pages); ?>
            </li>
        <?php } ?>
    </ul>
    
    <?php mysqli_free_result($nav_subjects); ?>
    
</navigation>

I am not able to get $page_id from index.php -> Undefined variable in public_navigation.php!

When calling index.html, echo $page_id; returns back undefined..

This is I want to set in public_navigation.php

<li class="<?php if($nav_page['id'] == $page_id){ echo 'selected'; } ?>">

when calling index.html, $page_is is not set in navigation, and navigation runs..

if(!isset($page_id)) { $page_id = 1; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM