doulu4316 2015-01-15 09:29
浏览 85

将html插入PHP返回

My PHP is failing me. I'm trying to put a div around an element in a function. Here is the original code:

if ($withCurrency) {
    $currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
    return $currency . ' ' . $lowestPrice;
}

I want to put a div around the $currency.

I've tried changing this line:

return $currency . ' ' . $lowestPrice;

to this:

return "<div>" . $currency . "</div> " . $lowestPrice;

However that actually outputs the div tags as text on the page. Can someone tell me how to add the divs so they render as html?

UPDATE:

Here is the entire function:

function getTourPrice($tourId, $withCurrency = true) {

    $lowestPrice = false;
    $offers = getTourOffers($tourId);
    // first get price from special offers
    foreach ($offers as $offer) {
        if ((isset($offer->options['special'])) && ((!$lowestPrice) || floatval($offer->options['price']) < $lowestPrice)) {
            $lowestPrice = floatval($offer->options['price']);
        }
    }
    // if special price isn't available then get price from normal offers
    if (!$lowestPrice) {
        foreach ($offers as $offer) {
            if ((!$lowestPrice) || (floatval($offer->options['price']) < $lowestPrice)) {
                $lowestPrice = floatval($offer->options['price']);
            }
        }
    }
    if (!$lowestPrice) return false;

    if ($withCurrency) {
        $currency = (isset($GLOBALS['aitThemeOptions']->directory->currency)) ? $GLOBALS['aitThemeOptions']->directory->currency : '';
        return $currency . ' ' . $lowestPrice;
    } else {
        return $lowestPrice;
    }

}

The result of that function is called with the following code:

{var $lp = getTourPrice($post->id)}
                {if $lp}<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>{/if}

If I use echo instead of return like this:

echo "<div>" . $currency . "</div> " . $lowestPrice;

I lose all the html above. I.E. What was the following:

<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>

Becomes this:

<div>R</div> 56200

(where R is $currency and 56200 is $lowestPrice)

Hope that's not painfully convoluted.

  • 写回答

3条回答 默认 最新

  • doukeng1922 2015-01-15 09:32
    关注

    This will have something to do with what you are doing with the return

    I imagine if you did this:

        if ($withCurrency) {
            $currency = 
            (isset($GLOBALS['aitThemeOptions']->directory->currency)) ?
            $GLOBALS['aitThemeOptions']->directory->currency : 
            '';
    
            echo "<div>" . $currency . "</div> ";
        }
    

    It would work.

    Is your code the end of a function?

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP