-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?

Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79
  • How are you getting that string into `$linkString`? – Lawrence Cherone Nov 30 '17 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. – Rocco The Taco Nov 30 '17 at 18:48
  • Dont just hide variables lol, now the str_replace wont work, and will cause people to think that's the issue. – Lawrence Cherone Nov 30 '17 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: https://stackoverflow.com/a/47580611/328193 – David Nov 30 '17 at 18:51
  • How do you output that link? – AbraCadaver Nov 30 '17 at 18:52
  • Updated the question to better reflect what is going on. Hope this helps. – Rocco The Taco Nov 30 '17 at 19:02
  • 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. https://3v4l.org/LJR81 – Lawrence Cherone Nov 30 '17 at 19:02
  • @LawrenceCherone yes, I updated the question. thanks – Rocco The Taco Nov 30 '17 at 19:05
  • I love it when people say it can't be done...this worked in the foreach loop: foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; } – Rocco The Taco Dec 01 '17 at 14:08

3 Answers3

1

change the foreach loop:

foreach ($icons as $ic) { 
    echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; 
}
Alam Darji
  • 275
  • 3
  • 13
0

Try this code

<a href="http://www.adomain.com/links.php?EmpNo=<?= $_SESSION['EmpNo']; ?>">LINK</a>
Alam Darji
  • 275
  • 3
  • 13
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.

David
  • 208,112
  • 36
  • 198
  • 279
  • This solved it: foreach loop: foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; } – Rocco The Taco Dec 01 '17 at 14:08