Page 1 of 1

Inserting a link

Posted: Mon Jan 06, 2014 9:18 am
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.

Re: Inserting a link

Posted: Mon Jan 06, 2014 9:26 am
by Celauran

Code: Select all

echo "<a href=\"view_client_info.php?clientid={$row['_rowid_']}\">{$row['firstname']} {$row['lastname']}</a>";

Re: Inserting a link

Posted: Mon Jan 06, 2014 9:37 am
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?

Re: Inserting a link

Posted: Mon Jan 06, 2014 9:50 am
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.

Re: Inserting a link

Posted: Mon Jan 06, 2014 10:13 am
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.