1

I want to build a marketplace(index.php) website where a user can click on a item and then a separate website will be open where there will be information about this item. This website will be the same for all items, but only some text and image will be different. My idea was to use php. So when I click an item, a info.php file will be called, and the corresponding html will be generated. The problem I am facing is how to transfer that product's id from the main site to info.php by clicking this item.

In index.php:

$id=""

How to give a value to var once this item is clicked(and info.php is called)?

In info.php:

include 'index.php';
echo $id;

And now I use var to retrieve information from the database. If there is a more efficient way to make this transition please let me know!

TylerH
  • 20,799
  • 66
  • 75
  • 101
NFT_king
  • 31
  • 4
  • Do you mean a different web page per item (not site)? – Nigel Ren Feb 26 '23 at 12:32
  • Typically you transfer data between pages using either a querystring parameter or form post or by setting and retrieving cookies among other methods – Stu Feb 26 '23 at 12:34
  • @NigelRen I want the link to be https://mywebsite/item1 and then the index changes but info.php is always called. – NFT_king Feb 26 '23 at 12:36
  • Perhaps https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php – Nigel Ren Feb 26 '23 at 12:41

1 Answers1

-1

Depending on how many items there are, one way to do it is in index.php you have

<?php
..
?>
<a href="/example.com/info?item=1">Item 1</a>
<a href="/example.com/info?item=2">Item 2</a>

Each link is a link to a differrent item.

in info.php

<?php
$id = filter_var ($_GET ['item'] ?? '', FILTER_SANITIZE_STRING);
?>

Because a param is passed from index, you need to use $_GET. But it should be sanitised to ensure that someone does not pass a param thats going to compromise your site. Additionally, the ?? '' bit ensures that if the param is not present, you get a space in $id instead of a null. You can change this default value to whatever you want.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Apr 21 '23 at 08:19