# How to open href link by array one by one

**URL:** <https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659>\
**Category:** Uncategorized\
**Created:** [April 9, 2022, 1:53am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659 "2022-04-09T01:53:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 9, 2022, 1:53am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/1 "2022-04-09T01:53:22Z")

</div>

please tell how can we open href link by array one by one … please see source code file and correct the code.

[source code file](https://drive.google.com/file/d/1Vnuh0zvULE826aTJkESGVwWUnepKVmKX/view?usp=sharing)

and check where is problem .  
…  
when i click the button , one element load and one link will open through array , and when i click again on countiue button, then 2nd element of array will load and 2nd link will open.

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 9, 2022, 5:22am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/2 "2022-04-09T05:22:48Z")

</div>

any one reply please.

---

<div class="post-metadata">

**Author:** ![katsry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@katsry](https://www.phphelp.com/u/katsry)\
**Post date:** [April 10, 2022, 3:24pm UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/3 "2022-04-10T15:24:53Z")

</div>

```auto
<?php

$nextpage = array("https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20
", "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20
", "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20
");

foreach ($nextpage as $x) {

?>
<a href="<?php print $x; ?>"><button>Continue</button></a>
    
<?php

}

?>

```

Try this - I have tested the code and tree buttons will display, when clicked they work fine.  
Good luck

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 10, 2022, 5:19pm UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/4 "2022-04-10T17:19:14Z")

</div>

please note: what i want?

I want to link wiil be open as when i click “1st time” on button then 1st element of array will load through “continue” button… similarly when again i click on “continue” button then 2nd element of the array will load in “continue” button… similarly when again 3rd time i click on “continue” button then 3rd element of the array will be load in “continue” button and link will open.

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [April 10, 2022, 8:14pm UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/5 "2022-04-10T20:14:41Z")

</div>

You have an example… update it to meet your needs.

You then increment through the array each button click.

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 10, 2022, 11:41pm UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/6 "2022-04-10T23:41:56Z")

</div>

I’m beginner in coding, so that please make it yourself, i"ve try many time, but incorrect code by me… so please help from your side.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [April 10, 2022, 11:46pm UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/7 "2022-04-10T23:46:58Z")

</div>

Are there are any sites you know of that do what you want? I would like to see an example of what you are asking for.

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 11, 2022, 12:00am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/8 "2022-04-11T00:00:52Z")

</div>

i’ve stated my question above in this post with full explain , kindly see my question with source file which i share here and more explanation also i shared on my later replies.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [April 11, 2022, 12:30am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/9 "2022-04-11T00:30:49Z")

</div>

This should get you started.

```auto
<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>

<script>
let link = new Array()
link[0] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20"
link[1] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20"
link[2] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20"

let intlinkIndex = 0;

function writeLink() {
    if ( intlinkIndex == link.length ) intlinkIndex = 0;
    document.getElementById('mylink').innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';
    intlinkIndex++;
}
</script>
</head>

<body>

<button onclick="writeLink();">Click Me</button>
<div id="mylink"></div>

</body>
</html>

```

Slightly different function but does the same thing without the _if_.

```auto
function writeLink() {
let print = document.getElementById('mylink');
    print.innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';;
    intlinkIndex = (intlinkIndex + 1) % (link.length);
}

```

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 12, 2022, 12:06am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/10 "2022-04-12T00:06:49Z")

</div>

@benanamen YOUR above code is nice and work but small issue is still coming,… the array has three element… and it should be work only for three element and after that it should be stop… but when we click four time on button , array will start again from start… i want just one thing, array should not start again from start when press it four time…  
in my mind it will be better, button should be hide or block when we want to press it four time so that array will not repeat again from start again and again…  
I hope you understand this issue and again will help me in this regards,  
your thankful…

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [April 12, 2022, 2:33am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/11 "2022-04-12T02:33:32Z")

</div>

Give this a try.

```auto
<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>

<script>
let link = new Array()
link[0] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL3+8QE&distance=20"
link[1] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NG&distance=20"
link[2] = "https://www.clc-uk.org.uk/cms/cms.jsp?menu_id=26131&postcode=AL5+3NS&distance=20"

let intlinkIndex = 0;

function writeLink() {
  if (intlinkIndex >= link.length) {
    let btn = document.getElementById('btn');
    btn.style.display = 'none';
    mylink.style.display = 'none';
  }
    document.getElementById('mylink').innerHTML = '<a href="' + link[intlinkIndex] + '">' + link[intlinkIndex] + '</a>';
    intlinkIndex++;
}
</script>
</head>

<body>

<button id="btn" onclick="writeLink();">Click Me</button>
<div id="mylink"></div>

</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![moazam](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e0b2c6/32.png) [@moazam](https://www.phphelp.com/u/moazam)\
**Post date:** [April 12, 2022, 5:26am UTC](https://www.phphelp.com/t/how-to-open-href-link-by-array-one-by-one/34659/12 "2022-04-12T05:26:41Z")

</div>

@benanamen , wao, yes… it works great… you are really a great coder… unbelievable result.  
thanks a lot.
