0

I have a series of href links saved in a MySQL database that are used to dynamically populate a PHP page like this:

<a href="http://www.adomain.com/links.php">LINK</a>

This is done by using a session variable to query the database and fetch only the links for that person by their EmpNo.

I need to append one of the links stored in the database with a session variable at the time we populate the page.

Currently the link looks like this in the database:

<a href="http://www.adomain.com/links.php?EmpNo=<?php echo $_SESSION['EmpNo'] ?>">LINK</a>

The problem is that when we populate the php page with this href it writes exactly what is stored in the record and does not actually insert in this case the EmpNo session variable.

I've tried to use ."". and it cuts off after the ".

Is there a better way to do this?

1 Answer 1

1

PHP code like this in the string isn't going to get executed when you display it:

<a href="http://www.adomain.com/links.php?EmpNo=<?php echo $_SESSION['EmpNo'] ?>">LINK</a>

And that's good, you don't want that. I would recommend updating the string that's stored in the database with a placeholder tag. Perhaps something like this:

<a href="http://www.adomain.com/links.php?EmpNo=__EMP_NO__">LINK</a>

Or this:

<a href="http://www.adomain.com/links.php?EmpNo={{EMP_NO}}">LINK</a>

Then you can dynamically change the string at display time by changing the tag to whatever value you want:

$linkString = // some query to get the string from the db
$linkString = str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $linkString);
echo $linkString;
Sign up to request clarification or add additional context in comments.

4 Comments

This looks really good, any chance you can get a fiddle together for me?
I'm not sure I understand what you're asking for, please elaborate.
Can I just replace the {{EMP_NO}} with the $SESSION['EmpNo'] using Javascript?
Not unless you've already got the EmpNo available somewhere in the environment on the client side. $SESSION['EmpNo'] is a PHP variable and JS can't see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.