Skip to main content
Known Participant
June 11, 2009
Question

Adding Html link to every line of database

  • June 11, 2009
  • 3 replies
  • 1594 views

I know the heading sounded a bit strange so I will try to explain it here:

I have created a recordset of a database which lists the top sites of the day now the database has a field site_names in which I add the top ten site names every day.

I m already using the nl2br code to add a line break after every line.

Now I want the lines to link to their respective sites.

See if we try to create a click here link that links to http://www.adobe.com then the code generated will be like this:

                         <a href="http://www.adobe.com">Click Here</a>

Now lets say the sites that I have added are

     http://www.blogator.blogspot.com

     http://www.tensports.com

     http://www.fifa.com

so I need a code such that these values get converted into not in the database but only in the browser.

     <a href="http://www.blogator.blogspot.com">http://www.blogator.blogspot.com</a>

     <a href=" http://www.tensports.com"> http://www.tensports.com</a>

     <a href=" http://www.fifa.com">http://www.fifa.com</a>

plz help the code that I m using now is this

     <?php echo nl2br ($row_rsSites['site_names']); ?>

this code allows to add a line break to all the lines in a database.

Plz help if any one of you have not understood what m trying to explain plz let me know I will try to explain again.

This topic has been closed for replies.

3 replies

David_Powers
Inspiring
June 13, 2009

Marking this thread as "assumed answered".

Known Participant
June 12, 2009

Ok let me simplify it when I type a website address in adobe like I m now it comes out to be like this

     http://www.wwe.com

But when I add this same line to a post it comes out with out any links in my site

     http://www.wwe.com
David_Powers
Inspiring
June 12, 2009

As the others have said, maybe time to reconsider your database structure. However, since each URL is separated by a newline character, what you want to do is fairly easy.

$sites = explode("\n", $row_rsSites['site_names']);

foreach ($sites as $site) {

  echo "<a href='$site'>$site</a><br />";

}

The first line creates an array of the sites, breaking them at the new line character. The rest of the code uses a foreach loop to go through each URL in the array, and uses echo to create the link.

Known Participant
June 12, 2009

Thanks Man it works great Thanks a lot.

DwFAQ
Participating Frequently
June 11, 2009

<a href="<?php echo ($row_rsSites['site_names']); ?>"><?php echo ($row_rsSites['site_names']); ?></a><br>

Known Participant
June 11, 2009

Ya I tried that man the problem is that all the 10 links are presents in one databse feild like this

    http://www.blogator.blogspot.com

    http://www.tensports.com

    http://www.fifa.com

and applying the code that you gave the result comes out to be this

    http://www.blogater.blogspot.comhttp://www.tensports.comhttp://www.fifa.com

try to hover on the line and see the address it links too.

DwFAQ
Participating Frequently
June 11, 2009

Looks like it's time to change your database field.