named anchors in dynamic content
Moderator: General Moderators
named anchors in dynamic content
In a project of mine I produce a dynamic page (product_list.php) with a list of products taken from a database. When I click on one of these products I get to another page which has info for this specific product (product_info.php) with a "Return" link on the bottom of the page. When I click on this "Return" link, I get the previous product list page but the browser gets me to the top of it. I want to add code so the "Return" link gets me to the same spot of the product list page where the clicked product is. With static html pages I know that you can do that with named anchors (using "id=..." for an element) and with "#". How can I do the same with php pages and dynamically created links?
Thanks for any help
Thanks for any help
I set up a quick example of how this can be done. Hope it does the trick.
Code: Select all
<?php
if (!isset($_GET['use_id'])) { print('<a href="redirect.php?use_id=test">Start Test</a>'); }
else { print('<a href="redirect.php#' . $_GET['use_id'] . '">Return</a>'); }
?>Can't you do something like:
and then, from the product_info.php page, on your return link have:
Code: Select all
<a name="$id_product"></a><a href="product_info.php?id_product=$id_product">Go to Product Detail</a>Code: Select all
<a href="product_list.php#$id_product">Return</a>In fact... looking again at my code, I'm thinking that it'd would work just as well with:
and then in product_info.php:
It is the "NAME" attribute on the anchor tag that allows you to go to that point in the document.
Note - I haven't tested any of this, just going on gut feeling.
Code: Select all
<a name="$id_product" href="product_info.php?id_product=$id_product">Go to Product Info</a>Code: Select all
<a href="product_list.php#$id_product">Return</a>Note - I haven't tested any of this, just going on gut feeling.
I have tried the above code but nothing works. When I use the '#', e.g. ,
then when I get in the 'product_list.php' script in debugging mode (been using the Zend Development Environment), I get null values in the variables of the query string, e.g. $var1, $var2 are null therefore my code doesnt work correctly, something that doesnt happen when I erase the '#$product_id' part (but then I return to my initial code which redirects my users to the top of the page...), so I think that the '#<spot in the page>' patent doesnt work with php files. I hope I am wrong....
Code: Select all
<a href=product_list.php#$product_id?var1=$var1&var2=$var2>Return</a>then when I get in the 'product_list.php' script in debugging mode (been using the Zend Development Environment), I get null values in the variables of the query string, e.g. $var1, $var2 are null therefore my code doesnt work correctly, something that doesnt happen when I erase the '#$product_id' part (but then I return to my initial code which redirects my users to the top of the page...), so I think that the '#<spot in the page>' patent doesnt work with php files. I hope I am wrong....
You need to put the # as the last thing in the URL, so
Code: Select all
<a href="product_list.php?var1=$var1&var2=$var2#$product_id">Return</a>