Echo a variable in PHP which displays as a link

15 pts.
hi, I have a number of variables that i pull from a mysql database. I want 1 of the variables to show as a link to a web page. i have tried the following code, which displays everything fine, except for the $url bit

echo "<p>$djname $genre $biography <a href='/$url'> Profile</a>";  [/code]

any help would be great

Answer Wiki

Thanks. We'll let you know when a new response is added.

I am not 100% sure you know what you are doing but if one of my developers was using php and had this I would point them to out coding standard and tell them to rewrite it as

>>>start snippit<<<
<p><?=$djname?> <?=$genre?> <?=$biography?> <a href’/<?=$url?>’> Profile</a>
>>>>end <<<<

Now you will still not see the URL but if you mouse over it the URL will be shown in the status bar of your browser and if you click it it will go to that URL.
PHP allows a lot of adddition of logic and data access but for layout and display it really works a lot better if you use tags. It lets people who are good at layout do the layout using the tools they know while allowing people who are good at logic do their stuff and then using the concept of Model View Controller for your web interface.

Hope this helps.

Discuss This Question: 3  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • YuvalShavit
    What does it output now?
    905 pointsBadges:
  • Whiteyoh
    it outputs $djname $genre and $biography fine, but i want a link to be displayed that takes you to their profile. the url ($url)is stored in the table
    15 pointsBadges:
  • YuvalShavit
    The code seems okay to me. Are you sure $url has a value? PHP doesn't require you to pre-declare all values, so if you mistyped $url when you assigned it a value, it could be uninitialized (and thus default to an empty string) here. It could also be a scoping issue; if $url is a global value, remember to declare "global $url" in the function. And if you're getting the value from a database query, make sure the query is correctly returning a value; if it doesn't, $row['url'] (or whatever it is) will not be set and will thus default to an empty string. You can test this by doing something like:
    $url=isset($row['url'])? '['.$row['url'].']' : "[UNDEFINED_URL]"; 
    Does it output something like this?
    <p>Name Genre Bio <a href=''> Profile</a>
    If so, I'd check to make sure you're correctly assigning $url a value. Otherwise, I've run out of ideas, sorry. HTH!
    905 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: