简体   繁体   中英

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. My code of 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.

BUT

Notice that all id are first printed and then someone will choose one of these links.

You're already doing it in other places. It's called concatenation - All that PHP does is output dynamically generated HTML. This means that within the quotes of your echo construct you can concatenate anywhere.

All you have to do is add it to the URL as you did with ID:

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. the $row variable gets set to a new

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 .

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).

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:

&check1=$row['topic_id']

with whatever your ID field is named, probably something along the lines of:

&check1=$row['id']

where id would be the actual id of the row in the database table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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