named anchors in dynamic content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

named anchors in dynamic content

Post by vasilis »

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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You can easily redirect the users to previous.php#bottom too...

But the problem is that (afaik) you can't store the location the user was surfing at... Eg: it's impossible to determine if a user is at #top or at #bottom when he follows a link..
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

but, that's exactly what my problem is... How do I redirect my users to the previous.php and at the specific link which they clicked? It seems that I can't use any named anchors in dynamically produced pages.
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post by neogeek »

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>'); }

?>
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Can't you do something like:

Code: Select all

<a name="$id_product"></a><a href="product_info.php?id_product=$id_product">Go to Product Detail</a>
and then, from the product_info.php page, on your return link have:

Code: Select all

<a href="product_list.php#$id_product">Return</a>
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post by neogeek »

@GM: Exactly, my reason for coding my example the way I did was to allow for a one file testing script. When implementing the script, they way you set it up was perfect.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

In fact... looking again at my code, I'm thinking that it'd would work just as well with:

Code: Select all

<a name="$id_product" href="product_info.php?id_product=$id_product">Go to Product Info</a>
and then in product_info.php:

Code: Select all

<a href="product_list.php#$id_product">Return</a>
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.
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

I have tried the above code but nothing works. When I use the '#', e.g.

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....
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

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>
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

I did it the way you say and it works fine! Thanks
Post Reply