Forum Moderators: coopster

Message Too Old, No Replies

PHP/MySQL and hyperlinks

         

ballistix

8:38 am on Jul 2, 2004 (gmt 0)

10+ Year Member



Hi there

I am stuck on a problem, that probaly has an insanely simple solution, but I just can't get it. I have built a mysql db that people can upload their details into, ie name, website, job title etc. Then I have created a php page that queries the database and displays all the results. What I want to know, is how do I format the query to display the result as a hyperlink? Here is an example of the code to display the website field in from the db:

<?php echo $row_rsContent['url'];

How do I modify the code so that it displays 'url' as a hyperlink?

Any help would be greatly appreciated!
Regards,
Ballistix

johnerazo

8:52 am on Jul 2, 2004 (gmt 0)

10+ Year Member



echo "<a href=".$row_rsContent['url'].">".$row_rsContent['url']."</a>"; 

ballistix

9:29 am on Jul 2, 2004 (gmt 0)

10+ Year Member



Hi

That sort of worked, except for one thing. Let's say the URL is www.webmasterworld.com. The link it's creating is not to www.webmasterworld.com, but instead to [localhost...]

Any input would really help.

Ballistix

timster

1:02 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The PHP looks OK. Check your data & see if "localhost" is in the URL field.

dcrombie

4:25 pm on Jul 2, 2004 (gmt 0)



The URL has to start with "http://". I would store them in that format in the database but you can also add it to the HTML output.

ergophobe

5:12 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure the form presented to the user when entering the data looks roughly like this

<p><strong>Your website:</strong> h*tp://<input type="text" name="url"></p>

Your website: h*tp://[_______]

So that the user can see that the http:// is not needed. Then when you get the data, before putting it in the DB, check it.

$url = $_POST['url']

if (substr($url, 0, 7) == 'http://')
{
$url = substr($url, 6);
}

Now put the url in the DB knowing that it does not have the http:// at the beginning. Then, for output,

$url = <url you got from the DB>

echo '<a href="http://' . $url . '">My Website</a>';

That's what I would do rather than storing the http:// in the DB.

dcrombie

5:22 pm on Jul 2, 2004 (gmt 0)



What do you do in that case if a link has to start with 'https://', or even 'ftp://'?

ergophobe

6:31 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Good question. I guess I was assuming that links to members' personal sites would not use any protocol but http.

https shouldn't really be a problem, because the connection should still work, it just won't be secure, perhaps resulting in a redirect.

If you wanted to allow other protocols, you could do the reverse of what I just offered and test for multiple protocols. If no protocol were given, I would add http:// by default.

Still, in this situation where it is meant to be part of a personal profile, along with name, email and such, I would probably just enforce the http just like on WebmasterWorld and let them put a link to a page that gives ftp info if that's what they want.

Tom