Jump to content

[SOLVED] echo a link


markvaughn2006

Recommended Posts

How do you echo a link in a variable form while? Thanks guys!!

 

<?php

$result = mysql_query("SELECT * FROM locations

WHERE owned='$_SESSION[myusername]'")

  or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

  echo "<a href="$row['link']">$row['title']</a>";

  echo "<br/>";

 

  }

?>

 

ERROR is...

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/properties.php on line 175

 

Link to comment
Share on other sites

jon23d almost had it;

 

echo "<a href=\"{$row['link']}\">{$row['title']}</a>";

You should also wrap myusername in single quotes (it's not a constant, am I right?), and then wrap the whole session variable in curly brackets.

 

mysql_query("SELECT * FROM locations
WHERE owned = '{$_SESSION['myusername']}'")

Link to comment
Share on other sites

because you are supposed to surround associative array strings with single quotes. a string that isn't surrounded by quotes is assumed to be a constant. For example

DEFINE("MY_CONST", "SOME VALUE");

//if i want to use this constant

echo MY_CONST;

 

PHP is smart enough to realize that if a constant doesn't exist, you probably meant a string, but if you have a case where you want to say the string MY_CONST, instead of his value, when you do this

echo MY_CONST

 

when you meant

echo "MY_CONST";

you  will run into problems

 

in your case, saying

$myARR[mykey];

 

is technically OK as long as you dont have a constant named mykey, but its best practice to avoid instances when this could be a problem by surrounding your array keys with single quotes.

 

oh and you surround it with curly brackets, because thats how you output associative arrays without concatenating strings

Link to comment
Share on other sites

mikesta707 beat me to it, but here's my (pretty identical) explanation.

 

Whats the reason for..

"You should also wrap myusername in single quotes (it's not a constant, am I right?), and then wrap the whole session variable in curly brackets."??

 

and no it is not a constant

 

myusername in your case is a key in an associative array (meaning the keys are string, not integers), and therefore it should be in quotes. Just like you would write

 

$key = 'myusername';
echo $_SESSION[$key];

and not

 

$key = myusername;
echo $_SESSION[$key];

Without quotes PHP thinks it's a constant, and looks for one with that particular name. If it doesn't exist, PHP assumes it's a string (while issuing a notice), and that's why your syntax works.

 

Note: When testing it, it seems no notices are issued when the "$array[key]" syntax is used though.

 

The curly brackets are just the correct syntax when using array values of associative arrays inside double quoted strings.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.