Search code examples
phpmultidimensional-arrayforeachisset

Using previous page array values in php into another page


I am creating a online webstore. But i am stuck in calling out the selected value to be used on the next page.

My Product summary page code,

        <?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>

My Product Detail Page,

<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>

in sort, select "PT", moves to page with all "PT" product, and end at the specific selected "PT" product in the product detail page.


Solution

  • I have edited your code for product detail page.. As you've not defined $productName variable anywhere in page. I've customized your code and hope it'll be good for you..

    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; ?>