Getting link go to one Page

I have a database with a list of Games on and i have used a hyper link on all game, my probem is at the moment each entry goes to a different page, I want to the links to go to the same page (i.e display.php) and it get the details from the database and show them on screen.

Can anybody help with this

What is your current code - that goes to different pages?

  <?php

		$sql = "SELECT
	id,
	Game_Name,
	Owned,
	Completed,
	
IF
	( Owned = 1, '✔', ' ' ) AS Owned,
	IF
		( Completed = 1, '✔', ' ' ) AS Completed
FROM
	games;";


		$dt = "SELECT Convert(VARCHAR(3, started, 101))";

		$result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
		$num_rows = mysqli_num_rows($result);?>


    <?php while ($row = mysqli_fetch_assoc($result)):
     ?>


    <tr>
	  <td style="text-align: center; font-weight: bold"><?php echo $row['id']; ?></td>
      <td><a href='details.php'><?php echo $row['Game_Name']; ?></a></td>
      <td style="text-align: center; color:green; font-weight: bold"><?php echo $row['Owned']; ?></td>
	  <td style="text-align: center; color:blue; font-weight: bold"><?php echo $row['Completed']; ?></td>
	  
    <tr>
<?php endwhile; ?>

Is this just a case of changing your a href value, or am I missing something?

<td><a href='details.php?id=<?php echo $row['id']; ?>'><?php echo $row['Game_Name']; ?></a></td>
2 Likes

Hi Thanks

That is what i wanted.

Hi I am struggle a lot with reading text and learning from text so would be glad if someone could help me with this problem i have with php. I have a list of games printed on screen with the link line as above, i can not get the game name printed on the display page. if anybody could help me or point me in the direction of a tutorial on this subject I would be greatful.

Show us the current version of the code.

<?php
include_once 'dbh.php';

date_default_timezone_set('Europe/London');
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="Images/bb.ico">
<title>My Collection</title>
<link rel="stylesheet" type="text/css" href="blue.css">
<link href="https://fonts.googleapis.com/css?family=Chakra+Petch:600" rel="stylesheet">
</head>


<body>

<!--Header-->
  <div id="header">
  <h3>MY COLLECTION</h3>  
  </div>
  <?php  include 'navbar.php';
  include 'a-z.php' ?>

  <div id="main">
		<?php
    $sql = "SELECT * FROM games;";
		$result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
		$num_rows = mysqli_num_rows($result);
		echo "<span style='color: white; font-size: 20px'/><strong><center><font color=yellow>$num_rows Found </strong></center></f
  <table>
      <thead>
  			<tr>
			<th style="text-align: center">ID</th>
      		<th style="text-align: center">Game Name</th>
			<th style="text-align: center">Owned</th>
			<th style="text-align: center">Completed</th>

  			</tr>
    </thead>


    <?php

		$sql = "SELECT
	id,
	Game_Name,
	Owned,
	Completed,
	
IF
	( Owned = 1, '✔', ' ' ) AS Owned,
	IF
		( Completed = 1, '✔', ' ' ) AS Completed
FROM
	games;";


		$dt = "SELECT Convert(VARCHAR(3, started, 101))";

		$result = mysqli_query($conn, $sql) or die("Bad Query: $sql");
		$num_rows = mysqli_num_rows($result);?>


    <?php while ($row = mysqli_fetch_assoc($result));
     ?>


    <tr>
	  <td style="text-align: center; font-weight: bold"><?php echo $row['id']; ?></td>
      <td><a href='details.php?id=<?php echo $row['id']; ?>'><?php echo $row['Game_Name']; ?></a></td>
      <td style="text-align: center; color:green; font-weight: bold"><?php echo $row['Owned']; ?></td>
	  <td style="text-align: center; color:blue; font-weight: bold"><?php echo $row['Completed']; ?></td>
	  
    <tr>
<?php endwhile; ?>
 </tbody>
</table>

If you add

var_dump($row);

inside your while() loop, what do you get in the ‘Game_Name’ field each time it loops?

do I put this after the while () loop like this.

<?php while ($row = mysqli_fetch_assoc($result)):
var_dump($row);
 ?>```

if this correct i get 'array(4) { ["id"]=> string(1) "1" and so on with all entries in database

Yes, that’s the correct place to put it.

Does that contain the correct value?

ETA - wait a minute, you said:

By the “display page”, do you mean this page which lists all the games, or the page that you link to which then displays an individual one? If it’s the latter, then obviously that’s the code we need to see to be able to offer advice.

I mean a page i link too, just want the page to be the same for every game but different as the entrys in the database

so first it should only display the name i click on (in display page)

hope this make sense?

OK, then you need to show the code for that page, details.php, if that’s the page you are having trouble with.

I have not much on that page as I cannot not get it to print the result from the last page

<?php
include_once("dbh.php");
date_default_timezone_set('Europe/London');
?>
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="Images/bb.ico">
<title>Game Collection</title>
<link rel="stylesheet" type="text/css" href="blue.css">
<link href="https://fonts.googleapis.com/css?family=Chakra+Petch:600" rel="stylesheet">
</head>


<body>
<!--Header-->
<div id="header">
  <h3>MY COLLECTION</h3>  
  </div>
  <?php  include 'navbar.php';?>

  <div id="main">
<?php

echo $row['id'];
?>

As soon as your first page was displayed, that was the end of the execution of the PHP code that drew it. All the variables are gone.

When you open the new page as a result of the user clicking on your <a> tag, it starts a new execution of, in this case, display.php. Within that, you need to run a new query to retrieve the individual game results, based on the game id parameter that you passed in as part of the URL.

Thank you for your help.

Could you point me in the right direction to learn this?

Well, your first code already shows that you know how to connect to the database, execute a query and display the results. You already have the building blocks.

Break the task into separate stages:

  1. Retrieve the game id from the URL and display it on the page.
  2. Write a query (in phpMyAdmin or whatever you use to access the database directly) to retrieve just that game.
  3. Put the query into your display.php code and get it to work with the id that you got in step 1.
  4. Finally, change the query to use prepared statements for security and make sure it still works.

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