Forum Moderators: coopster

Message Too Old, No Replies

dynamic links in php with mysql field

         

mapgeek

8:38 pm on May 14, 2003 (gmt 0)

10+ Year Member



I am in the process of creating my first dynamic page with PHP and MySQL.
I am trying to create a page from a MySQL table in which field is either empty, a url or an email address. I want to be able to create a hyperlink from this field. My theory is that I can create an If Else statement within the hyperlink tag to distinguish between the url and an email address, then use the hyperlink format accordingly (i.e, mailto: for email and http:\\ for url.) However, every time I try it I get an error. Before I post the code that I have written, I wanted to make sure that my theory was on the right track or see if anybody has any ideas about how else I can do this. I don't know if I'm getting the errors because my theory is incorrect of because I'm not coding correctly.
Thanks.

jatar_k

8:40 pm on May 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sounds like the theory makes sense. What is the exact error you are getting?

dmorison

8:55 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

A couple of pointers...

In most web apps that input a URL into a database, unless you're carefull upfront you normally end up with either "http://www.example.com" or just "www.example.com". It depends on what validation has been done when the field value was input.

Secondly, it is perfectly valid for a URL to contain the @ character - so you have to be careful when trying to distinquish between a URL and an email address.

Anyway, having said that; I would use a "standard" email verification regexp. If it passes, render the field as an email address, if it fails, render the value as a URL.

So, your input from the db is:

$something

and you want to create

$field

which is the HTML to output to the client, then somethign like:


<?php

$something = "someone@example.com";

$field = "";

if ($something)
{
if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $something))
{
$something = htmlentities($something,"ENT_QUOTES");

$field = "<a href='mailto:".$something."'>".$something."</a>";
}
else
{
$something = htmlentities($something,"ENT_QUOTES");

if (substr($something,0,4)<>"http")
{
$something = "http://".$something;
}
$field = "<a href='".$something."'>".$something."</a>";
}
}

echo $field;

?>

(tested very briefly, but should be ok!)

mapgeek

8:56 pm on May 14, 2003 (gmt 0)

10+ Year Member



I'm getting a "Parse error: parse error, unexpected T_STRING"

Here's my latest stab at the line it's on:

<p>CustodianMail: <a href="<?php if ($row_rsTest['custodian'] LIKE '%@%'){echo mailto:$row_rsTest['custodian']);} else {echo $row_rsTest['custodian'];}?>"><?php echo $row_rsTest['custodian'];?></a></p>

Could it be that I'm not testing for an empty string as well?

jatar_k

9:20 pm on May 14, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It looks like the error is because you used LIKE in your if statement

maybe try
$emailtest = strstr [php.net]($email, '@');
if ($emailtest!= false) {
//echo your email stuff
} else {
//echo your domain stuff
}