tags:

views:

62

answers:

3

I'm trying to see if the user has entered a website URL into the database with the following code. If the user did not enter their website's URL, do not display anything. If the user did enter their website, display the website.

I think I'm doing it wrong.

 <?php
 if (!empty($url))
 {
 echo'';
 } else { 
 echo'p>Website: <a href="<?php echo "http://","$url"; ?>" title="<?php echo "$url"; ?>"><?php echo "http://","$url"; ?></a></p>';
 } 
 ?>
+1  A: 

what you've got there is that you're checking if it's NOT empty (that is, there is some data), you're echoing an empty string. If it IS empty, then you're echoing it out.

Remove the ! in the first line and you should be right.

nickf
you where right but now the url link won't display.
22
I only get this now Website: " title="">.
22
in that case, the error isn't in that block of code, is earlier.
yoda
@yoda: no, it's later, just look at the echo statement...
jeroen
didn't saw it, thanks
yoda
+1  A: 

You're simple mismatching the meaning of "!" :

<?php
if (!empty($url))
{
echo'p>Website: <a href="<?php echo "http://","$url"; ?>" title="<?php echo "$url"; ?>"><?php echo "http://","$url"; ?></a></p>';
} else { 
echo'';
} 
?>
yoda
+2  A: 

Apart from the wrong order (you need to remove ! before the empty()), you also have some errors in the echo part: the < is missing for the < p > tag and you are including php tags in your string.

It should read something like:

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