Reputation: 59
I'm working on page which consists from database of products.
On the front page you can see every product but with only limited information.
I would like to create secondary page by clicking on Learn more of a product with all information.
Really something widely used in eshops where there is no need to create new page for every product but rather one template which will get information from PHP.
The problem is that I don't know how is this principle working.
I don't even know how it is called so I can search for some tutorial.
Maybe fetching array through products' ID?
For showing products I use:
`
<?php
$db = new PDO("mysql:host=localhost;dbname=cars", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Query
$cars = $db->prepare("SELECT car_id, name, price, availability, description FROM cars
");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
?>
`
And then:
<?php foreach($cars as $car): ?>
<td>
<div><img src="<?php echo $car['image'];?>"
</div>
</td>
<td>
<a href="#">
<div id="textTable"><strong><?php echo $car['name'];?></strong></a>
<a href="**what to send here?**" id="learnMore">Learn More..</a>
</div>
</td>
<td>
<?php echo $car['availability'];?>
</td>
<td>
<strong><?php echo $car['price'];?>€</strong>
</td>
</tr>
<?php endforeach;?>
Upvotes: 0
Views: 2683
Reputation: 339
You can send you send your product id like this
<a href="product_detail.php?product_id=<?=$cars['car_id']?>">View</a>
And fetch each record from product id in database with only one php page
This code is for mfetch record in you second page.
$id = $_GET['product_id'];
$cars = $db->prepare("SELECT car_id, name, price, availability, description FROM cars WHERE cae_id='$id'");
$cars->execute();
$cars = $cars->fetchAll(PDO::FETCH_ASSOC);
Upvotes: 1
Reputation: 1497
One possible solution :
<a href="/more?id=<?php echo $car['id']; ?>">Learn More..</a>
(I removed the id="learnMore" because an id must appear only once in an HTML page)
Upvotes: 1