生成每个项目的不同网站。

huangapple go评论52阅读模式
英文:

Generating different website for every item

问题

在index.php中,您可以使用以下方式为变量$id赋值,以在点击项目时将产品的ID传递给info.php:

$id = $_GET['product_id'];

在这里,我们使用$_GET超级全局数组来获取传递给info.php的product_id参数。确保在点击项目时将product_id参数附加到URL中,例如:

<a href="info.php?product_id=123">Click to view product</a>

然后,在info.php中,您可以通过以下方式引入index.php并使用$id变量:

include 'index.php';
echo $id;

这种方法将允许您在index.php和info.php之间传递产品ID,并且更加有效和通用。请确保在数据库查询中使用$id来检索相应的产品信息。

英文:

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=&quot;&quot;

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

In info.php:

include &#39;index.php&#39;;
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!

答案1

得分: -1

根据项目数量的不同,一种方法是在 index.php 中进行如下操作:

&lt;?php
..
?&gt;
&lt;a href=&quot;/example.com/info?item=1&quot;&gt;项目 1&lt;/a&gt;
&lt;a href=&quot;/example.com/info?item=2&quot;&gt;项目 2&lt;/a&gt;

每个链接都指向不同的项目。

在 info.php 中:

&lt;?php
$id = filter_var ($_GET [&#39;item&#39;] ?? &#39;&#39;, FILTER_SANITIZE_STRING);
?&gt;

因为从 index 传递了参数,所以需要使用 $_GET。但应对其进行清理以确保没有人传递可能损害您网站的参数。另外,?? &#39;&#39; 部分确保如果参数不存在,您将在 $id 中获得一个空格而不是空值。您可以将此默认值更改为任何您想要的值。

英文:

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

&lt;?php
..
?&gt;
&lt;a href=&quot;/example.com/info?item=1&quot;&gt;Item 1&lt;/a&gt;
&lt;a href=&quot;/example.com/info?item=2&quot;&gt;Item 2&lt;/a&gt;

Each link is a link to a differrent item.

in info.php

&lt;?php
$id = filter_var ($_GET [&#39;item&#39;] ?? &#39;&#39;, FILTER_SANITIZE_STRING);
?&gt;

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 ?? &#39;&#39; 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.

huangapple
  • 本文由 发表于 2023年2月26日 20:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定