Jump to content

Creating hyperlinks from data input to be display in table


oriental_express

Recommended Posts

Hi there.

 

Im abit stuck with hyperlinks. Ive used search functions and have found relevent thread but the coding does not match.

 

I have a php page the displays records from a form.

id  name, address, phone, website etc

 

I would like to make the website data to turn into a hyperlink once submitted.

 

Here is my following php code that displays the table

 

=============

 

$results = mysql_query("SELECT * FROM charitylist");

 

  if ($results){

 

      while ($a = mysql_fetch_array($results)) {

 

print("<TR>");

print("<TD>$a[id]</TD>");

print("<TD>$a[charitynum]</TD>");

print("<TD>$a[name]</TD>");

print("<TD>$a[address]</TD>");

print("<TD>$a[postcode]</TD>");

print("<TD>$a[website]</TD>");        i would like this to turn whatever is in there to a hyplink

print("<TD>$a[phone]</TD>");

print("<TD>$a[fax]</TD>");

print("</TR>");

 

      };

 

====================

 

It would be great if someone got back to me :)

 

Thank you for your time

 

Michael

$results = mysql_query("SELECT * FROM charitylist");

   if ($results){

       while ($a = mysql_fetch_array($results)) {

print("<TR>");
print("<TD>$a[id]</TD>");
print("<TD>$a[charitynum]</TD>");
print("<TD>$a[name]</TD>");
print("<TD>$a[address]</TD>");
print("<TD>$a[postcode]</TD>");
print("<TD><a href='$a[website]'>$a[website]</a></TD>");
print("<TD>$a[phone]</TD>");
print("<TD>$a[fax]</TD>");
print("</TR>");

       };

 

Should be fine, as long as your website field is a url.

I don't see anything wrong... but I would do this:

 

 

<?php
$results = mysql_query("SELECT * FROM charitylist");

   if ($results){
       
       while ($a = mysql_fetch_array($results)) {

              echo'<TR>
              <TD>'.$a['id'].'</TD>
              <TD>'.$a['charitynum'].'</TD>
              <TD>'.$a['name'].'</TD>
              <TD>'.$a['address'].'</TD>
              <TD>'.$a['postcode'].'</TD>
              <TD><a href="'.$a['website'].'">'.$a['website'].'</a></TD>
              <TD>'.$a['phone'].'</TD>
              <TD>'.$a['fax'].'</TD>
              </TR>';

       }
?>

thanks for reply guys :) im using dream weaver and both of the above have not worked ?

website is in black, but the rest is red, is it not meant to be read ?

 

when using this

 

print("<TD><a href="'.$a['website'].'">'.$a['website'].'</a></TD>");

 

it give me this as an error

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/dontwork/public_html/charity/charity.php on line 79

This will check to make sure your query is valid:

<?php
$results = mysql_query("SELECT * FROM charitylist")or die(mysql_error());

   if ($results){
       
       while ($a = mysql_fetch_array($results)) {

              echo'<TR>
              <TD>'.$a['id'].'</TD>
              <TD>'.$a['charitynum'].'</TD>
              <TD>'.$a['name'].'</TD>
              <TD>'.$a['address'].'</TD>
              <TD>'.$a['postcode'].'</TD>
              <TD><a href="'.$a['website'].'">'.$a['website'].'</a></TD>
              <TD>'.$a['phone'].'</TD>
              <TD>'.$a['fax'].'</TD>
              </TR>';

       }
?>

Your a star mate ! :) wicked it worked . But now have one problem. The links is also including its directory..    eg  www.mysite.com/charity/www.bbc.com

 

any solutions just to include the real url ?

 

thanks again wooohooooooooooo

 

This will check to make sure your query is valid:

<?php
$results = mysql_query("SELECT * FROM charitylist")or die(mysql_error());

   if ($results){
       
       while ($a = mysql_fetch_array($results)) {

              echo'<TR>
              <TD>'.$a['id'].'</TD>
              <TD>'.$a['charitynum'].'</TD>
              <TD>'.$a['name'].'</TD>
              <TD>'.$a['address'].'</TD>
              <TD>'.$a['postcode'].'</TD>
              <TD><a href="'.$a['website'].'">'.$a['website'].'</a></TD>
              <TD>'.$a['phone'].'</TD>
              <TD>'.$a['fax'].'</TD>
              </TR>';

       }
?>

If there's no http://, your site thinks it's an internal link. So here;

 

<?php
$results = mysql_query("SELECT * FROM charitylist")or die(mysql_error());

   if ($results){
       
       while ($a = mysql_fetch_array($results)) {

              echo'<TR>
              <TD>'.$a['id'].'</TD>
              <TD>'.$a['charitynum'].'</TD>
              <TD>'.$a['name'].'</TD>
              <TD>'.$a['address'].'</TD>
              <TD>'.$a['postcode'].'</TD>
              <TD><a href="http://'.$a['website'].'">'.$a['website'].'</a></TD>
              <TD>'.$a['phone'].'</TD>
              <TD>'.$a['fax'].'</TD>
              </TR>';

       }
?>

 

That should work.

I really thank you for your help guys its work this time.

I have one last questions. Instead of redirecting to a new

page when the link is clicked, I would like to open the link in

a new window if possible ?

 

Thanks

What if the site has htt://?? then what? you will have a link that looks like this:

 

http://http://somesite.com

 

so you may want to do this (I didn't test it though, but it looks like it should work):

<?php
$results = mysql_query("SELECT * FROM charitylist")or die(mysql_error());

   if ($results){
       
       while ($a = mysql_fetch_array($results)) {

              echo'<TR>
              <TD>'.$a['id'].'</TD>
              <TD>'.$a['charitynum'].'</TD>
              <TD>'.$a['name'].'</TD>
              <TD>'.$a['address'].'</TD>
              <TD>'.$a['postcode'].'</TD>
              <TD><a href="http://'.preg_replace("~http://~","",$a['website']).'">'.$a['website'].'</a></TD>
              <TD>'.$a['phone'].'</TD>
              <TD>'.$a['fax'].'</TD>
              </TR>';

       }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.