oriental_express Posted November 7, 2007 Share Posted November 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Share Posted November 7, 2007 $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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 Not escaped correctly: print("<TD><a href='".$a['website']."'>".$a['website']."</a></TD>"); Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Michan Posted November 7, 2007 Share Posted November 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 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 Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 dont aswer that guys i think I'll try and find out myself, its the only way to learn properly. Thanks for all your help Have a nice day Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 Hint, just look under anchor tags, html, and the "target" attribute :-) Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 thank you for the tip Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 7, 2007 Share Posted November 7, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
oriental_express Posted November 7, 2007 Author Share Posted November 7, 2007 works like a dream mate, i cant thank you enough. This will get me good marks ! Thanks a bunch Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.