简体   繁体   English

在 HTML 标签内插入 PHP 变量,即在 PHP function 内

[英]Inserting PHP variable inside HTML tag, which is inside PHP function

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.问这个问题我觉得很愚蠢,我想将从数据库中检索到的“id”粘贴到details=...>Read More.之后的<a href>标签中,该数据库存储在 $link 变量中。 ., 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>

You need to pass $link as a function parameter.您需要将$link作为 function 参数传递。

You also left out the ' at the end of the href attribute.您还遗漏了href属性末尾的'

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;  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM