-1

I have a string of text that is part of a URL on a PHP page that looks like this:.

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

I need to replace the {{EMP_NO}} with a session variable I have created $SESSION['EmpNo']

The href is populated by a PHP function that queries the database and writes the href links depending on the userid.

function get_user_icons($user_id)
{
    $icons = array();
    $user_id = mysql_real_escape_string($user_id);

    $sql = "SELECT ic.* "
        . "FROM gbl_empicons AS em "
        . "LEFT JOIN gbl_icons AS ic ON em.IconId = ic.id "
        . "WHERE em.EmpNo = '$user_id' ORDER BY em.IconId";
    $res = mysql_query($sql) or die(mysql_error());

    while ($row = mysql_fetch_assoc($res))
    {
        $icons[] = $row;
    }

    return $icons;
}

Later the actual links are written out like this:

$icons = get_user_icons($row_WAATKgblqemplisting['EmpNo']); 

    foreach ($icons as $ic)
    {       
echo $ic['url'] . ' ';
    }

I've tried to update this function with str_replace with no luck in both locations.

str_replace('{{EMP_NO}}', $_SESSION['EmpNo']);

This is not working. I have also tried to use the em.EmpNo to no avail. What is the best way to achieve this result?

9
  • How are you getting that string into $linkString? Commented Nov 30, 2017 at 18:47
  • I'm not, I updated it. I basically want to replace any instance of {{EMP_NO}} found on the page with the session. Commented Nov 30, 2017 at 18:48
  • Dont just hide variables lol, now the str_replace wont work, and will cause people to think that's the issue. Commented Nov 30, 2017 at 18:48
  • @RoccoTheTaco: str_replace doesn't search back through all raw output of the page. Nothing does. Unless you're using a templating engine of some kind (as can be implied by the {{placeholder}} syntax), what you'd need to do is output the value where it's needed. Like in an answer posted below: stackoverflow.com/a/47580611/328193 Commented Nov 30, 2017 at 18:51
  • 1
    Seems like an XY Problem, you must be outputting the html from somewhere. Be it from the database or right at the end before an echo or a view, you will always have somewhere where you can str_replace or worse use buffering. 3v4l.org/LJR81 Commented Nov 30, 2017 at 19:02

3 Answers 3

1

change the foreach loop:

foreach ($icons as $ic) { 
    echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code

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

6 Comments

That won't work as the href is written out from a database. I need to manipulate it after it's written
<?php echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo']); ?> maybe this should help. use it when you are writing the href from the database
@RoccoTheTaco: Then use str_replace() on the value you're getting from the database. Before you output it to the page.
@David I updated the question as that make sense but still need some help. Please review.
change this int the foreach loop: foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; }
|
0

That's not how you use str_replace(). You forgot the third function argument, the overall string in which you want to search for something and replace it with something else.

So where is that string? Looks like it's right here:

echo $ic['url'] . ' ';

That's what you'd replace:

echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' ';

Wherever you output or otherwise can manipulate the string value that contains this link, that's where you would replace its contents.

1 Comment

This solved it: foreach loop: foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; }

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.