简体   繁体   English

将href的ID传递到href链接中

[英]Pass the id of href into the href link

So here is my problem.I want the id of the href which is taken dynamically to be printed in the url. 所以这是我的问题。我希望动态获取的href的ID打印在url中。 My code of category.php: 我的category.php代码:

CATEGORY.PHP CATEGORY.PHP

while($row = mysql_fetch_assoc($result))
{   
    echo '<a id="' . $row['topic_id'] . '"  href="category.php?id=' . 
         $_SESSION['id'] . '&check1=//what should i put here?">;
}

all i want is for check1 to have the id of each link seperately so if i have 3 links(depends on the database) the 2nd link will have id=2 and i want to print id=2 the next time category.php is loaded. 我只想让check1分别拥有每个链接的id,所以如果我有3个链接(取决于数据库),第二个链接将具有id = 2,并且我想在下次加载category.php时打印id = 2 。

BUT

Notice that all id are first printed and then someone will choose one of these links. 请注意,所有ID都会先打印出来,然后有人会选择这些链接之一。

You're already doing it in other places. 您已经在其他地方这样做了。 It's called concatenation - All that PHP does is output dynamically generated HTML. 这称为串联 -PHP所做的只是输出动态生成的HTML。 This means that within the quotes of your echo construct you can concatenate anywhere. 这意味着您可以在echo构造的引号内连接任何位置。

All you have to do is add it to the URL as you did with ID: 您所需要做的就是像使用ID一样将其添加到URL:

while ($row = mysql_fetch_assoc($result)) {
    echo '<a id="'. $row['topic_id'] .'" href="category.php?id='. $_SESSION['id'] .'&check1='. $row['topic_id'] .'"></a>'
}

your $row['topic_id'] should be different with every iteration. 您的$row['topic_id']每次迭代都应该有所不同。 the $row variable gets set to a new $row变量被设置为新的

This will output the same $row['topic_id'] as the id attribute of the link but it will also append it as a $_GET parameter in your category.php . 这将输出与链接的id属性相同的$row['topic_id'] ,但还将其作为$_GET参数附加到您的category.php中

If you do not know what gets passed on in the $_GET array you could always do a print_r($_GET) anywhere in the script since $_GET always gets set (even without query params, it'll just be empty). 如果您不知道$_GET数组中传递了什么,则可以始终在脚本中的任何位置执行print_r($_GET) ,因为$_GET总是被设置(即使没有查询参数,它也将是空的)。

It could be that it is unclear to me what you're asking, it could also be that you want the actual primary key id field in which case you'll just have to swap out the last bit: 可能是我不清楚您要问什么,也可能是您想要实际的primary key id字段,在这种情况下,您只需要换掉最后一位即可:

&check1=$row['topic_id']

with whatever your ID field is named, probably something along the lines of: 使用您的ID字段命名的任何内容,可能类似于:

&check1=$row['id']

where id would be the actual id of the row in the database table. 其中id是数据库表中该行的实际id

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

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