Yura
Yura

Reputation: 13

Pass links as variable in php and echo it on html

i must pass multiple link as variables on php ex: www.mysite.com/dl.php?link=www.google.com&link2=yahoo.com&link3=youtube.com and so on, there is a variable number of links, and then i want to put them on and html page generated dynamically based on the number of links i inputed, in the example i did, the links was 3, so it must be:

<html><center>
<a href="<?php echo $_link ?>">Click to download part 1</a>
<a href="<?php echo $_link1 ?>">Click to download part 2</a>
<a href="<?php echo $_link2 ?>">Click to download part 3</a>
</center></html>

can someone help me with this problem?

Upvotes: 1

Views: 437

Answers (4)

N20084753
N20084753

Reputation: 2170

If URL = www.mysite.com/dl.php?link1=www.google.com&link2=yahoo.com&link3=youtube.com

<?php
for($i = 0; $i < count($_GET); $i++)
{
?>
<a href="<?php echo $_GET["link".($i+1)]; ?>">Click to download part <?php echo ($i+1);?></a>
<?php
}
?>

Upvotes: 0

Guillaume Poussel
Guillaume Poussel

Reputation: 9822

Instead of using different parameter names for all urls, use an array:

www.mysite.com/dl.php?link[]=www.google.com&link[]=yahoo.com&link[]=youtube.com

Then, in dl.php, $_GET['link'] is an array. You can iterate like this:

for ($i = 0; $i < count($_GET['link']); ++$i) {
  echo '<a href="' . $_GET['link'][$i] . '">Click to download part ' . ($i + 1) . '</a>';
}

Upvotes: 1

Gavin
Gavin

Reputation: 2143

If the only get value that is the URLs then just loop through...

foreach ($_GET as $url)
    {
    echo $url
    }

Upvotes: 0

puelo
puelo

Reputation: 5987

Parameters you append on your URL can be accessed through $_GET in php. Take a look at this page: http://php.net/manual/en/reserved.variables.get.php

Update: If you have a variable number of get parameters and you want to get them all just use a foreach loop:

foreach($_GET as $key => $url) {
 echo $url;
}

Upvotes: 2

Related Questions