Php 在$page\u内容中使用时无法读取GET变量

Php 在$page\u内容中使用时无法读取GET变量,php,get,Php,Get,我有一个名为productFocus.php的文件,其中我使用GET变量ID:$ID=$\u GET[“ID”] 但是,此页面在product.php文件中的使用方式如下: product.php <?php $page_content = 'productFocus.php'; // $id = $_GET["id"]; include('master.php'); ?> <?php include "db/db.php"; $id = $_

我有一个名为
productFocus.php
的文件,其中我使用GET变量ID:
$ID=$\u GET[“ID”]
但是,此页面在
product.php
文件中的使用方式如下:

product.php

<?php
    $page_content = 'productFocus.php'; // $id = $_GET["id"];
    include('master.php');
?>
<?php
    include "db/db.php";
    $id = $_GET["id"];
    $product = get_product_by_id($id);
?>

<div class="product-focus">
    <h3><?php echo $product->name ?></h3>
    <img src="/images/products/<?php echo $product->image ?>">
    <div id="description">
        <h4>Productinformatie</h4>
        <p><?php echo $product->description ?></p>
        <h4>Partners</h4>
        <table>
            <?php
                foreach($product->partners_obj as $partner) {
            ?>
            <tr>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank">
                        <img id="partner" src="/images/partners/<?php echo $partner->image ?>">
                    </a>
                </td>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank"><?php $partner->$product_price ?></a>
                </td>
            </tr>
            <?php
                }
            ?>
        </table>
    </div>
</div>
//HTML code
...
 <?php include($page_content);?>
...
//HTML code

productFocus.php

<?php
    $page_content = 'productFocus.php'; // $id = $_GET["id"];
    include('master.php');
?>
<?php
    include "db/db.php";
    $id = $_GET["id"];
    $product = get_product_by_id($id);
?>

<div class="product-focus">
    <h3><?php echo $product->name ?></h3>
    <img src="/images/products/<?php echo $product->image ?>">
    <div id="description">
        <h4>Productinformatie</h4>
        <p><?php echo $product->description ?></p>
        <h4>Partners</h4>
        <table>
            <?php
                foreach($product->partners_obj as $partner) {
            ?>
            <tr>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank">
                        <img id="partner" src="/images/partners/<?php echo $partner->image ?>">
                    </a>
                </td>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank"><?php $partner->$product_price ?></a>
                </td>
            </tr>
            <?php
                }
            ?>
        </table>
    </div>
</div>
//HTML code
...
 <?php include($page_content);?>
...
//HTML code

图像?>“>
产品信息

合作伙伴
master.php

<?php
    $page_content = 'productFocus.php'; // $id = $_GET["id"];
    include('master.php');
?>
<?php
    include "db/db.php";
    $id = $_GET["id"];
    $product = get_product_by_id($id);
?>

<div class="product-focus">
    <h3><?php echo $product->name ?></h3>
    <img src="/images/products/<?php echo $product->image ?>">
    <div id="description">
        <h4>Productinformatie</h4>
        <p><?php echo $product->description ?></p>
        <h4>Partners</h4>
        <table>
            <?php
                foreach($product->partners_obj as $partner) {
            ?>
            <tr>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank">
                        <img id="partner" src="/images/partners/<?php echo $partner->image ?>">
                    </a>
                </td>
                <td>
                    <a href=<?php echo $partner->$product_url ?> target="_blank"><?php $partner->$product_price ?></a>
                </td>
            </tr>
            <?php
                }
            ?>
        </table>
    </div>
</div>
//HTML code
...
 <?php include($page_content);?>
...
//HTML code
//HTML代码
...
...
//HTML代码
当我浏览到
productFocus.php?id=324324
时,我可以读取GET变量,但当我浏览到
product.php?id=324324
时,我没有访问GET变量id的权限


有没有一种优雅的方法可以解决这个问题?

在常规页面加载之前,在prodcts.php文件中调用产品详细信息之前,需要检查GET变量

<?php

if (isset($_GET['id'])) {

    // GET PRODUCT DATA

}else{

    // LOAD PRODUCTS PAGE

}

?>

你能更好地解释一下,并展示一些代码吗?你的意思是你想把
id
productFocus.php?id=…
传递到
product.php
?所有相关的代码都在那里…这取决于
$page\u content
的实际使用方式。你也许可以用
$page\u content='productFocus>解决这个问题。php.。(array_key_exists('id',$\u GET)“?id={$\u GET['id']}”:“)
,但由于缺乏其他信息,因此无法保证。您可以发布每个文件的代码吗?我看不到您在product.php中包含productFocus.php文件。现在可以很好地传递数据,但是master.php不会加载:(您的master.php文件必须以php标记开始,即使它们是空的,并且在html标记之前没有代码。否则,服务器尝试加载php文件,然后运行到html标记,认为这就是php结束的地方:)我在index.php中使用了相同的代码,在调用master.php之前是否有$page\u content变量?看起来,它没有在master.php文件中声明,只是被调用了。如果调用master.php文件的页面没有$page\u内容(或者在调用master之前没有设置),它不会显示任何内容,您可以通过master.php文件中的if语句检查是否有任何内容,以确保。