Making a link using PHP coding and HTML

Hi,

I have used the select query to get an array of 2 variables. 1 is Name and 2 is a Link.

If I do this:

while($row = mysql_fetch_array($b))
  {
  echo $row['name'];
  echo $row['link'];
  }

The output will be wrong just saying
James www.sitea.com Ben www.siteb.com etc.

How do I code it in HTML and PHP so that the name James is linked to be clicked to go to sitea and the word Ben is linked to go to siteb.com? They also need to be vertically listed.

Is this easy?

Matt.

Just change the echo so it echoes what you want it to echo :wink:

Hi Matt,

Looks like what you want is a HTML list, that way they’ll be displayed as a vertical list by default. To show the links correctly, they need to be [URL=“http://reference.sitepoint.com/html/a”]anchor elements, which follow this format:

<a href="www.example.com">My link</a>

To do this in PHP would look something like this:


echo "<ul>";

while($row = mysql_fetch_array($b))
{
    echo "<li><a href=\\"$row[link]\\">$row[name]</a></li>";
}

echo "</ul>";

which will output HTML that looks like this:


<ul>
    <li><a href="www.sitea.com">James</a></li>
    <li><a href="www.siteb.com">Ben</a></li>
</ul>

Thanks Fret Burner - that’s great. One more thing though…do you know how to make a list spread across the page?

By this I mean… rather than a continuous long list. The list goes down vertically then it starts again to the right? Like this:

A I
B J
C K
D L
E M
F N
G
H

You could use CSS to do that.


<ul class="link_list">
    <li><a href="www.sitea.com">James</a></li>
    <li><a href="www.siteb.com">Ben</a></li>
    <li><a href="www.sitec.com">Freddy</a></li>
    <li><a href="www.sited.com">Brian</a></li>
    <li><a href="www.sitee.com">Rodger</a></li>
    <li><a href="www.sitef.com">John</a></li>
</ul>

link_list {
  columns: 2;
  -webkit-columns: 2;
  -moz-columns: 2;
}

Note that this isn’t compatible with older browsers (anything less than IE10, for example). See this compatibility chart for more information.