Home > Mobile >  Inserting PHP variable inside HTML tag, which is inside PHP function
Inserting PHP variable inside HTML tag, which is inside PHP function

Time:10-08

I feel pretty stupid for asking this one, I want to paste 'id' that I retrieve from database, which is stored in $link variable into the <a href> tag after details=...>Read More.., but I can't seem to get it to work.

<p>
    <?php 

    $longString = $row['name'];
    $link = $row['id'];

    echo readMoreHelper($longString);

    function readMoreHelper($story_desc, $chars = 100) {
        $story_desc = substr($story_desc,0,$chars);  
        $story_desc = substr($story_desc,0,strrpos($story_desc,' '));  
        $story_desc = $story_desc." <a href='details.php?details=$link>Read More...</a>";  
        return $story_desc;  
    } 

    ?> 
</p>

CodePudding user response:

You need to pass $link as a function parameter.

You also left out the ' at the end of the href attribute.

echo readMoreHelper($longString, $link);

function readMoreHelper($story_desc, $link, $chars = 100) {
    $story_desc = substr($story_desc,0,$chars);  
    $story_desc = substr($story_desc,0,strrpos($story_desc,' '));  
    $story_desc .= " <a href='details.php?details=$link'>Read More...</a>";  
    return $story_desc;  
} 
  •  Tags:  
  • php
  • Related