Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making A List Of Links With PHP

I would like to know how to make a list of links appear on my page displaying a name but when you click it, it navigates to the link.

I currently know how to make a list and display the items of it using the foreach command and arrays but is there a way I can make it so I have an array, containing an array, containing the name of the link and the link itself, like so:

$links = array(array("Google","//google.co.uk"),array("Bing","//bing.co.uk"))
foreach ($links as $myurl){
foreach ($myurl as $url){
echo "<a href='".$url."'>".$myurl."</a>";
}};

I know the above doesn't work but if anyone can help with this problem, it is much appreciated.

like image 913
celliott1997 Avatar asked Dec 26 '22 22:12

celliott1997


2 Answers

$links = array('Google' => 'www.google.com', 'Yahoo' => 'www.yahoo.com');

foreach($links as $k => $v) {
  echo '<a href="//' . $k . '">' . $v . '</a>'; 
}

As you can see I don't specify http or https, just // works on both! See: http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml

You can add links to $links with:

$links['stackoverflow'] = 'www.stackoverflow.com';
like image 180
Wouter Dorgelo Avatar answered Jan 05 '23 09:01

Wouter Dorgelo


$links = array(
array("Google","//google.co.uk"),
array("Bing","//bing.co.uk")
);

foreach ($links as $urlitem){ 
echo "<a href='".$urlitem[1]."'>".$urlitem[0]."</a>";
}
like image 21
Roger Wayne Avatar answered Jan 05 '23 09:01

Roger Wayne