0

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Gospel77
  • 133
  • 1
  • 7
  • 1
    You could add accomodationID to the form within the loop using a hidden input, as the question does in your [second example](https://stackoverflow.com/questions/50314340/sending-rowid-through-a-form-to-another-php-using-a-button). This should not be changed by the fact it's in a loop. Using this method should allow you to access it from `$_GET['accommodationID']` where you couldn't before. [`$_SESSION`](https://www.php.net/manual/en/reserved.variables.session) is entirely seperate and not to be confused with [`$_GET`](https://www.php.net/manual/en/reserved.variables.get) and forms. – Jess Sep 02 '22 at 23:17

1 Answers1

0

Here are two ways of doing it

Using a param

<form  action="info.php?<?php echo $fetch['ID'];?>" method="get">
  <input type="submit" name="<?php echo $fetch['ID'];?>" value="More info" >
</form>

And you use a $_GET to obtain the value.

Using a hidden input

<form  action="info.php" method="get">
  <input class="hidden" type="text" name="ID" value="<?php echo $fetch['ID'];?>">
  <input type="submit" name="<?php echo $fetch['ID'];?>" value="More info" >
</form>

And look at the hidden value of 'ID'. Note that this name does not have to be unique as only one will be posted. And use a style in css to hide the 'hidden'

input.hidden {display:none}
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41