The code below loops through a database table (accommodation) using mysqli_query and for each row creates a summary card to display an image and row info (name, ID, location, and price). 14 summary cards are displayed.
A submit button links to a 'more info' page.
<?php
$query = "SELECT * FROM accommodation";
$result = mysqli_query($conn, $query);
if ($result){
if(mysqli_num_rows($result) > 0){
while($fetch = mysqli_fetch_assoc($result)){
?>
<div class="summary-card">
<img src="<?php echo $fetch['main_image'] ?>" >
<p> <?php echo $fetch['name'], $fetch['ID'] ?> </p>
<p> <?php echo $fetch['location'] ?> </p>
<p>from <?php echo $fetch['price'] ?> </p>
<form action="info.php" method="get">
<input type="submit" name="<?php echo $fetch['ID'] ?>" value="More info" >
</form>
</div>
<?php
}
}
}
?>
Can accommodationID be sent when submitting?
This would facilitate having just one 'more info' page and then being able to populate that page based on the table row ID that was passed when clicking on the related submit.
I've seen examples showing how to send a row ID to a linked page (1, 2, 3) but not when the initial content was created using a loop.
So far I am able to append an ID to the target URL after clicking using the "get" method. However, if I try to print the ID using $_SESSION['accommodationID'] I get the ID relating to the last loop (14). If I use $_GET['accommodationID']I get an undefined index error.
New to PHP (procedural) and have honestly done my best trying for a few days before posting.