简体   繁体   English

获取ID,然后将其放入href

[英]Get an id then put it into a href

I'm a bit new to this so sorry if this has been covered already but i'm going around in circles searching. 我对此有些陌生,所以很抱歉,如果这个问题已经被涵盖,但是我正在圈子里搜索。

I've had a look around learn t how to edit htaccess and use the get function, I then even found a plugin called redirection that did similar. 我环顾四周,学习如何编辑htaccess并使用get函数,然后我甚至找到了一个名为redirection的插件,该插件的功能与此相似。

What I would like to do is if I have a URL http://example.com/file.php?id=blue 我想做的是,如果我有一个URL http://example.com/file.php?id=blue

is to grab the id which is "blue" 是要获取“蓝色”的ID

then in a href link dynamically add it to the end of another url 然后在href链接中将其动态添加到另一个URL的末尾

 <a href="http://www.domain.com/file.php?id=blue">Link Example</a>

If someone could help show me or point me in the right direction on how to get the id blue and add it into a href that would be great. 如果有人可以帮助向我展示或指导我如何将ID设置为蓝色并将其添加到href中,那将是不错的选择。

Many Thanks 非常感谢

You have to use $_GET. 您必须使用$ _GET。 People might be dicks about it here - but I had a hard time when I was first learning to program too. 人们可能对此不满意-但是当我第一次学习编程时,我也很难过。 You'll get it, don't worry. 您会明白的,不用担心。

This is how get works (at least, all you need to know about how it works): 这是get的工作方式(至少,您需要了解它的工作方式):

if you have the file index.php if you add a query string to the end of it like index.php?id=1 You can access id=1 by doing the following in your code: 如果您有文件index.php,并且在其末尾添加了一个查询字符串,例如index.php?id = 1,则可以通过在代码中执行以下操作来访问id = 1:

$id = $_GET['id'];

Similarly if the query string contains the following index.php?id=1&page=5&par=3&club=putter&upnext=tigerwoods 类似地,如果查询字符串包含以下index.php?id = 1&page = 5&par = 3&club = putter&upnext = tigerwoods

On the left hand of the equal sign is the Key(id, page, par, club, upnext) and on the right side their value(1,5,3,putter,tigerwoods) 等号的左边是键(id,page,par,club,upnext),右边的值是(1,5,3,putter,tigerwoods)

One thing to remember is that when retrieving numbers from the query string they will always be of the string type, so you cant do something like 要记住的一件事是,当从查询字符串中检索数字时,它们将始终为字符串类型,因此您不能执行以下操作

if ( $_GET['page'] === 5 )

you'll have to do 你必须要做

if ( $_GET['page'] == 5 )

and to echo it into a link: 并将其回显为链接:

$club = $_GET['club'];



 if ( $club == 'NRA' ) {
         echo "<a href='file.php?page=$club' title="lets buy some guns!">Gun Show</a>";
         echo 'Buy tickets to my gunshow ^^';
   }

Hope this helps! 希望这可以帮助!

You can also do things like set your website up so that it has one template and use the $_GET parameter to determine which files to include into the content sections of the site via a switch command. 您还可以执行一些操作,例如设置网站,使其具有一个模板,并使用$ _GET参数来确定要通过switch命令将哪些文件包括到网站的内容部分中。 I do this, but not across my whole site. 我这样做,但是不遍及整个网站。 For my user control panel, I do this to simply include only the file necessary (change email, update password, delete account, update profile, etc) 对于我的用户控制面板,我这样做是仅包含必要的文件(更改电子邮件,更新密码,删除帐户,更新配置文件等)

Cah'piche? 卡皮切?

Use the $_GET parameter. 使用$_GET参数。

YAY!! 好极了!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM