在 php 中使用上一页数组值到另一个页面中


Using previous page array values in php into another page

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

我的产品摘要页面代码,

        <?php
        include 'productData.php';
        if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
            $selected = $productArr[$_GET['cat']];
        }
        foreach ($selected as $productName => $productDescriptionArr):
            ?>
            <div>
                <div>
                    <a class="thumbnail" href="productDetailPage.php?cat=<?= $selected; ?>&code=<?= $productName; ?>">
                        <img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
                        <div><h3><?php echo $productName; ?></h3></div>
                    </a>
                </div>
            </div>
        <?php endforeach; ?> 
        
    </body>
</html>

我的商品详情页面,

<html>
    
    <body> 
        <?php
        include 'productData.php';
        
       if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
            $selected = $productArr[$_GET['cat']];
            if (isset($_GET['code']) && isset($productName[$_GET['code']])){
                $name = $productName[$_GET['code']];
            }
        }
        foreach ($selected as $name => $productDescriptionArr):
            foreach ($productDescriptionArr as $key => $value) :
            
            ?>
            <div>
                <div>
                        <img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
                        <div class="title"><h3><?php echo $name; ?></h3></div>
                    </a>
                </div>
            </div>
        <?php endforeach; ?> 
        <?php endforeach; ?> 
    </body>
</html>

在排序中,选择"PT",移至包含所有"PT"商品

的页面,并在商品详情页面中特定选定的"PT"商品处结束。

我已经编辑了产品详细信息页面的代码。由于您尚未在页面中的任何位置定义$productName变量。我已经自定义了您的代码,希望对您有好处。

include 'productData.php';

       <?php
         if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
            $productNames = $productArr[$_GET['cat']];   //Added $productNames here to asign your current product category
        }
          foreach($productNames as $name=>$details): 
            if (isset($_GET['code']) && $name==$_GET['code']){
            //$name is product name.
            //print_r($details);
            // now you can use $details['images'], $details['dimension'], $details['price'] as you want...
          ?>
            <div class="category">
                <div class="col-md-3">
                    <a class="thumbnail">
                        <img src="<?php echo "img/" . $details['image'] ?>" class="img-rounded" width="250" height="220">
                        <div class="title"><h3><?php echo $name; ?></h3></div>
                    </a>
                </div>
            </div>
        <?php } ?> 
        <?php endforeach; ?>