Inserting a link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpayne
Forum Newbie
Posts: 3
Joined: Mon Jan 06, 2014 9:08 am

Inserting a link

Post by jpayne »

novice learner here am having a problem formatting a link and could use assistance. Ive tried many variations and am thinking it has something to do with quotes but am lost.

First of all. this works for just displaying a URL and a name with it.

Code: Select all

echo "<a href=\'view_client_info.php?clientid=\">" . $row['firstname'] . " " . $row['lastname'];
My goal is to have clientid=[a value from one of the rows returned from the database] so I did something like this

Code: Select all

echo '<a href=\"view_client_info.php?clientid=" . $row['_rowid_'] . "\"> . $row['firstname'] . " " . $row['lastname'] . "</a>'; 
but I always get a syntax error. As ive said, ive tried many variations of single/double quotes and "/" (escapes) but apparently its not sinking in.

Ive searched this problem and actually saw some old posts with similar issues and I tried to imitate but still ended up with Syntax errors. Really would appreciate some assistance/explanation. Thanks in advance.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Inserting a link

Post by Celauran »

Code: Select all

echo "<a href=\"view_client_info.php?clientid={$row['_rowid_']}\">{$row['firstname']} {$row['lastname']}</a>";
jpayne
Forum Newbie
Posts: 3
Joined: Mon Jan 06, 2014 9:08 am

Re: Inserting a link

Post by jpayne »

wow! that worked thanks. I guess I never tried braces lol. Nothing I read suggested that. Did I miss something somewhere in my reading?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Inserting a link

Post by Celauran »

Looks like there's some confusion over how escape characters work as both your examples have escapes where they aren't required. Single quotes can be placed inside double quotes without escaping and vice versa. Variables are parsed inside a string enclosed in double quotes and are not parsed in strings enclosed in single quotes. Also, the single quotes around your array key were closing the opening single quote of the echo statement, leading to the syntax error. Perhaps a better question to ask yourself, however, is why you're echoing HTML in the first place.
jpayne
Forum Newbie
Posts: 3
Joined: Mon Jan 06, 2014 9:08 am

Re: Inserting a link

Post by jpayne »

Celauran wrote:... Perhaps a better question to ask yourself, however, is why you're echoing HTML in the first place.
Probably because I don't know any better? I am trying to learn and I am probably not following the directions as well as I should maybe? I am using a video series by Lynda.com and he uses a similar example I am just trying to use it on my project instead of the example one. Can you give me an example of the more correct way to write this? thanks.
Post Reply