从一个文件到另一个文件的值


Value from a file to another file?

我正在使用php创建一个在线网上商店。但是我被困在调用要在下一个 php 页面上使用的选定值。

这是我的数组文件。

<?php
$categoryArr = [
    "PT" => ["title" => "Plush toy", "image" => "img/111.PNG"],
    "KC" => ["title" => "key Chain", "image" => "img/222.PNG"],
    "PC" => ["title" => "Phone Cover", "image" => "img/333.PNG"]
];
$productArr = [
    "PT" => [
        "Giraffe" => ["dimension" => "tall: 13 wide: 4.5", "price" => "28.00", "image" => "Giraffe.jpg"],
        "Koala"   => ["dimension" => "tall:  9 wide:  10", "price" => "28.00", "image" => "Koala.jpg"],
        "Octopus" => ["dimension" => "tall:  8 wide:   9", "price" => "30.00", "image" => "Octopus.jpg"],
    ],
    "KC" => [
        "IceCream" => ["dimension" => "tall:  3 wide:   2", "price" => " 8.00", "image" =>   "ice.jpg"],
        "Toast"    => ["dimension" => "tall:2.5 wide:   2", "price" => " 8.00", "image" => "toast.jpg"],
        "Cupcake"  => ["dimension" => "tall:2.5 wide:   2", "price" => " 8.00", "image" =>   "cup.jpg"],
    ],
    "PC" => [
        "iPhoneGiraffe" => ["dimension" => "tall:4.5 wide: 2.3", "price" => " 20.00", "image" => "iPhoneGiraffe.jpg"],
        "iPhoneKoala"   => ["dimension" => "tall:4.5 wide: 2.3", "price" => " 20.00", "image" =>   "iPhoneKoala.jpg"],
        "iPhoneOctopus" => ["dimension" => "tall:4.5 wide: 2.3", "price" => " 20.00", "image" => "iPhoneOctopus.jpg"],
    ],
];

我的类别页面代码,

<html>
    <head>
        <title>OnlineStore</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href ="css/bootstrap.min.css" rel="stylesheet">
        <link href ="css/styles.css" rel="stylesheet">
        <script>
            function newPage() {
                window.location.assign('productSummaryPage.php?cat=');
            }
        </script>
    </head>
    <body>
        <?php include 'mainNavBar.php'; ?>

        <?php
        include 'productData.php';
        foreach ($categoryArr as $category => $SectionArr):
            foreach ($SectionArr as $field => $value):
            endforeach;
            ?>
            <div class="category">
                <div class="col-md-3">
                    <input type="submit" value="<?= $category; ?>"  onclick="newPage()" name="cat" method="post" action="productSummaryPage.php">
                    <div class="header">
                        <h3><?= $category; ?></h3>
                    </div>
                    <?php
                    echo "<img src='" . $value . "' >";
                    ?>
                </div>
            </div>
        <?php endforeach; ?> */

    </body>
</html>

我的产品页面代码,

<html>
    <head>
        <title>OnlineStore</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href ="css/bootstrap.min.css" rel="stylesheet">
        <link href ="css/styles.css" rel="stylesheet">
    </head>
    <body>
        <?php include 'mainNavBar.php'; ?>
        <?php
        include 'productData.php';
        if (isset($_POST['cat'])) {
            $productCategory = $_POST['cat'];
        }
            /* if (isset($_GET['viewProduct'])){
              $product = $_GET['viewProduct'];
              } */
            foreach ($productArr as $productCategory => $ProductNameArr):
                foreach ($ProductNameArr as $pfield => $pvalue):
                ?>
                <div class="category">
                    <div class="col-md-3">
                        <a herf="#" class="btn btn-default">
                            <div class="header">
                                <h3><?= $pfield; ?></h3>
                            </div>
                        </a>
                        
                    </div>
                </div>
            <?php endforeach; ?>
            <?php endforeach; ?>
    </body>
</html>

我的最终目标是拥有一个类别页面,其中包含指向产品页面中特定位置的按钮/链接。例如,如果一个人点击了"PT",它将移动到另一个页面,其中包含所有"PT"产品。

你可以替换这个

<input type="submit" value="<?= $category; ?>"  onclick="newPage()" name="cat" method="post" action="productSummaryPage.php">

通过这个

<a href="productSummaryPage.php?act=<?php echo $category; ?>"><?php echo $category; ?></a>

而这个

if (isset($_POST['cat'])) {
            $productCategory = $_POST['cat'];
        }
            /* if (isset($_GET['viewProduct'])){
              $product = $_GET['viewProduct'];
              } */
            foreach ($productArr as $productCategory => $ProductNameArr):
                foreach ($ProductNameArr as $pfield => $pvalue):
                ?>
                <div class="category">
                    <div class="col-md-3">
                        <a herf="#" class="btn btn-default">
                            <div class="header">
                                <h3><?= $pfield; ?></h3>
                            </div>
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>
            <?php endforeach; ?>

通过这个

if(isset($_GET['cat']) && isset($productArr[$_GET['cat']] ))
            foreach ($productArr[$_GET['cat']]  as $pfield => $pvalue):
                ?>
                <div class="category">
                    <div class="col-md-3">
                        <a herf="#" class="btn btn-default">
                            <div class="header">
                                <h3><?php echo $pfield; ?></h3>
                            </div>
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>