0

I am trying to pull json data into a relevant php page. I have product data in json and a php page that loops through it to display the products in a list, which works fine. Clicking any of these products in the list takes me to its product detail page (by adding ?product=<? $product['name'] ?> to the url, but I cannot pull the right data into this specific page. Here is my setup:

JSON file

{"products": [
 {"name": "Banana","price": "10ドル"},
 {"name": "Vodka","price": "1ドル"}
]}

PHP for product list (works fine)

<?php
$products_json = file_get_contents('assets/json/products.json');
$decoded_json = json_decode($products_json, true);
$products = $decoded_json['products'];
foreach($products as $product) {
?>
<p><?= $product['name']; ?></p>
<a href="product-detail.php?product=<?= $product['name']; ?>'">See Details</a>
<?php }?>

PHP for product detail page, where it all goes wrong

$products_json = file_get_contents('assets/json/products.json');
$decoded_json = json_decode($products_json, true);
$product = $decoded_json['products'];
<h1>Product Name: <?= $product['name'] ?></h1>
<h1>Product Name: <?= $_GET['name'] ?></h1> //also tried this, but no luck

p.s. don't be fooled by my reputation score - that was all from years ago and from one good answer explaining bootstrap 3!

asked Apr 29, 2022 at 21:01

2 Answers 2

1

$product is an array of all the products, not a single product. You need to loop over it to find the information for the product passed as the parameter.

$products_json = file_get_contents('assets/json/products.json');
$decoded_json = json_decode($products_json, true);
$products = $decoded_json['products'];
foreach ($products as $product) {
 if ($product['name'] == $_GET['product']) { ?>
 <h1>Product Name: <?= $product['name'] ?></h1>
 Price: <?= $product['price'] ?>
 <?php
 break;
 }
}

If you have lots of products, it would probably be a good idea to make the JSON an associative array where the product name is the key. Or use a database instead of a JSON file.

answered Apr 29, 2022 at 21:12
Sign up to request clarification or add additional context in comments.

4 Comments

this did it TY so much. I don't have a ton of products in here, 20 max. It's for a TV/kiosk running a browser not a public website so won't be an evergrowing list either.
regarding the associative array, this would require me to rewrite this foreach and my list foreach as well, right?
That's the whole point, you don't need foreach() when you have an associative array. Just use $products[$_GET['product']]
oh, well I will look into that! (unless you can point me to the best example)
1

When you are in the product_detail page, you can get the variable using:

$_GET["product"]

since in the previous page, you are sending it using this:

?product=<?= $product['name']; ?>

This is a key-value parameter, the right side of the = sign is the name of the variable and the left side is the value.

If you want to find the complete product based on the json file, you could filter the $products variable like this:

$products_json = file_get_contents('assets/json/products.json');
$decoded_json = json_decode($products_json, true);
$products = $decoded_json['products'];
$product = array_filter($products, function($p) {
 return $p['name'] == $_GET["name"];
});

With that being said, I would suggest adding an id key to the products array, so it is easier to find it and you don't have to deal with spaces in the url.

answered Apr 29, 2022 at 21:08

1 Comment

thanks, I ended up using the Barmar's answer above, combined with your suggestion for adding an id key. rather than rewrite my loops etc, I just added another key:value pair that which is the product name with underscores instead of spaced

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.