dynamic named anchor syntax

5 posts by 2 authors in: Forums > CMS Builder
Last Post: November 30, 2009   (RSS)

By rez - November 30, 2009

Please show me how to make a dynamic anchor, already within the php tags. [crazy]

<?php
echo "<a name=\"$record['num']\" id=\"$record['num']\"></a>";
?>

Re: [rez] dynamic named anchor syntax

By Chris - November 30, 2009

Hi rez,

I'm a little confused, it seems like you answered your own question! Did that not work for you?

P.S. The id="" isn't necessary; the name="" is what's required for an anchor.
All the best,
Chris

Re: [rez] dynamic named anchor syntax

By rez - November 30, 2009 - edited: December 1, 2009

Still getting the error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The code I tried is:
<?php

echo "<a name=\"$record['num']\"></a>";
?>

Re: [rez] dynamic named anchor syntax

By Chris - November 30, 2009 - edited: November 30, 2009

Hi rez,

Sorry, if you want to have an array-lookup inside a string, you need to enclose it in curly braces:

<?php
echo "<a name=\"{$record['num']}\" id=\"{$record['num']}\"></a>";
?>


Or, alternately, take it out of the string entirely:

<?php
echo '<a name="' . $record['num'] . '" id="' . $record['num'] . '"></a>';
?>


I hope this helps. Please let me know if you have any questions.

P.S. Yet another approach would be the following, but this sometimes gets confusing:

<?php
?><a name="<?php echo $record['num'] ?>" id="<?php echo $record['num'] ?>"></a><?php
?>

All the best,
Chris