Call id in link and create unique page

I created a site that lists recipes by category. Each category is a page that populates its own set of recipes on it (which is coming from a database).
What I want is to link each result to a page that will show the actual recipe. I’m assuming I need to address the id for each one, but I can’t figure out how to call the id for each one since it depends on the results. Below is what one of the category pages looks like:

<!-- PAGE TITLE-->
  <div class="row">
    <div class="col-lg-12">
      <h1 class="page-header">Appetizers & Beverages</h1>
    </div>
  </div>

  
<!-- DATABASE RESULTS-->
  <?php
	$sql = "SELECT id, category, bilde, title FROM oppskrift_table WHERE category = 'Appetizers & Beverages' ORDER BY title ";
	$result = mysqli_query($con, $sql);
	if(mysqli_num_rows($result) > 0 ){
		while($row = mysqli_fetch_assoc($result)){
?>
<div align="center" style="vertical-align:bottom;" class="col-md-4"> <a href="#"> <img class="img-responsive img-cat" src="bilder/rBilder/<?=$row['bilde']?>" width="250" alt=""> </a>
    <h4 style="max-width:250px;"><a href="#" class="dishes"><?=$row['title']?></a></h4>
</div>

  <?php
			}
		}
		else {
    echo "<div class='container-fluid' style='align=center;'><h4>There are no results for this category.</h4></div>";
}
	?>

I can’t figure out if I need to call the id within the row results, or create a new if statement to handle this and then place the variable for it within the a href tag… something like:

<a href="<?=$row['id']?>" class="dishes">
1 Like

I’d think your link as you showed it would work, assuming you add the remaining parts of the URL to actually call whatever code you have to display the recipe by id number. Call it within the row results as you already do for the title:

    <h4 style="max-width:250px;"><a href="/mysite/showrecipe.php?id=<?=$row['id']?>" class="dishes"><?=$row['title']?></a></h4>

Then in showrecipe.php access $_GET['id'], sanitise it, and use it to build your query.

Thanks for that. I think I have it, but I wound up displaying all of the recipes in the results page. It appears to be because I have $row in the code. Don’t I just need a single id instance to be called? This is what I have as the link:

<a href="01T.php?id=<?=$row['id']?>">IMAGE</a>

This seems to work, but I think the resulting page is asking for all the recipes instead of just one:

  <?php
	$sql = "SELECT id, category, bilde, title, duration, servings, ingredients, directions FROM oppskrift_table";
	$result = mysqli_query($con, $sql);
	if($result) {
		while($row = mysqli_fetch_assoc($result)){
	?>

Then in the HTML section I put:

<div class="container" style="margin-top:3%;">
  <div class="row product">
    <div class="col-md-5 col-md-offset-0"><img class="img-responsive img-cat" src="bilder/rBilder/<?=$row['bilde']?>" width="400" alt=""></div>
    <div class="col-md-7">
      <h2><?=$row['title']?></h2>
      <p>Duration: <?=$row['duration']?></p>
      <p>Servings: <?=$row['servings']?></p>
    </div>
  </div>
  <div class="page-header">
    <h3>Ingredients:</h3>
  </div>
  <h5 class="media-heading"><?=$row['ingredients']?></h5>
  <div class="page-header">
    <h3>Directions:</h3>
  </div>
  <div class="media">
    <div class="media-body">
      <p><?=$row['directions']?></p>
    </div>
  </div>
  <div class="media">
    <div class="media-body"></div>
  </div>
</div>

  <?php
			}
		}
?>

I just need one id to show in the page, obviously.

Yes, your query doesn’t pay any attention to the recipe id that you pass into the php code:

  // do some checks to the $_GET['id'] before you use it, don't just leave it like this once you've seen it work.
  $id = $_GET['id'];
  $sql = "SELECT id, category, bilde, title, duration, servings, ingredients, directions FROM oppskrift_table where id=$id";

You should also look at using bound parameters instead of the way I put the id into the query above - I don’t use mysqli so I don’t know what the syntax is. The note about sanitising the id is so that if people call your code directly, not via your first page, you can deal with any junk they put in the URL for the id value, such as “" or "; drop table recipes” if that even works.

Ah! So I had to pull the id from a created variable to call it. I tried that before, but didn’t add it to the $sql variable definition. Got it. Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.