-2

How can I make a dynamic anchor?

I have this table in my database;

----------------------------------------------
:    **id**    :    **Names**                :
----------------------------------------------
:     1        : Name1, Name2, Name3         :
----------------------------------------------

I return it to my page and what I want to do is when I click in one of the name I want to change the link id.

For example when I clicked Name1 my anchor should be

<a href="http://name.com?id=name1">

and when I click Name2

<a href="http://name.com?id=name2">

and so on.

How can I possibly do this?

Thanks!

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
jules
  • 19
  • 3
  • 8

2 Answers2

1
<?php 
    Foreach ($name as $n){
        echo '<a href ="http://name.com?id='.$n.'">'.$n.' </a>';
?>

Guessing this is what you are looking for. Question is a little vague. Could use more information.

kgarrigan
  • 133
  • 1
  • 9
  • Honestly, make your code clear: array is a massive of data, so write properly `$names as $name`, it is so simple. – sybear Jun 23 '13 at 07:48
  • Honestly, since $n is local to the foreach loop, I think it really comes down to a matter of style preference. – kgarrigan Jun 23 '13 at 07:56
0

You can script code to the HTML page which does this change without reloading the page. This modifies the HTML document at runtime, for example see here: How to change href of <a> tag on button click through javascript

The object you click on now needs to have an onclick handler like in the example.

Community
  • 1
  • 1
clearwater
  • 514
  • 2
  • 7