0

Basically I have one page titled allListings.php (displays all listings for an item) and another page titled viewListing.php (where you can view the details of one listing). In this allListings php page I created a table using a database I made from SQL. In one of the table columns, I have a button titled "View Listing", where if you click on it it should send you to the view listing page (which works). In the all listings page, each row is assigned a listing ID but I don't know how to get it so that when you click on each individual view listing page it sends you to the adequate page, not just the first listing from the database. I dropped some of my code below for reference.

$sql = "SELECT * FROM listing";
$result = $mydb->query($sql);
while ($row = mysqli_fetch_array($result)) {  
    echo "<tr>
              <td class='orange'>" . $row["Listing_ID"] . "</td>
              <td class='light'>" . $row["Book_Title"] . "</td>
              <td class='light'>" . $row["Price"] . "</td>
              <td class='light'>" . $row["Description"] . "</td>
              <td><button type='submit'>View Listing</button></td>
          </tr>";
    }

The view listing page currently has a HTML form set up. I want it so that each view listing button gets sent to the appropriate view listing page and not just the same first database entry page, so I guess one way to go about it is to assign each table row a variable? I'm not totally sure. (I have the classes in so I'm able to color code things with CSS)

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
unknown
  • 1
  • 2
  • What is your general HTML setup? Judging by that button, you have a single form that encompasses the entire table and then each row has its own button to view the listing? – El_Vanja Apr 27 '21 at 21:53
  • @El_Vanja yes thats what i want to have happen! i have a button in each row but it keeps linking to the same listing no matter which button i click. – unknown Apr 27 '21 at 22:09
  • 2
    It would make a little more sense to have links, where you could directly include the id in the URL. – El_Vanja Apr 27 '21 at 22:10
  • You don't actually need to `fetch()` the rows as you iterate, a `foreach()` can do that for you: https://stackoverflow.com/a/66775416/2943403. When you are wanting to pass data to your server-side for a "reading" operation (`SELECT`), then you should use `$_GET`. Just pass the id in the url's querystring as El_V is saying. This probably **Needs More Focus** because we have loads of duplicates for what you need, but what you need would require multiple duplicate pages from Stack Overflow. – mickmackusa Apr 27 '21 at 22:12

0 Answers0