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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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>';

       }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

       }
?>

Link to comment
Share on other sites

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>';

       }
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';

       }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.