doufu6504 2016-02-09 16:52
浏览 39
已采纳

$ mobile = table_phone(); 即使不拨打$ mobile也会立即显示

can anyone explain me why is my function value shown on a page immediately even without putting <?php echo $mobile; ?>

I just type this part:

<?php $mobile = table_phone(); ?>

and phone number gets displayed, but not where I want it. Not where I call

<?php echo $mobile; ?> 

I tried to use other function like osc_user_phone_land() which is part of the osclass core and that one is working fine, but calling for mobile_phone, which is actually part of the plugin is not working properly.

Any ideas? My coding knowledge is quite limited so it might be a super simple fix..

Here is the more of the code:

<div class="description-right">
     <?php 
        $mobile = '';      
        if($mobile == '') { $mobile = table_phone(); }      
        if($mobile == '') { $mobile = __('No phone number', 'OSCLASS'); }      
     ?> 
   <div class="mob-wrap">

     <div class="mob-top">
       <span><i class="fa fa-phone"></i><?php _e("Contact", 'OSCLASS'); ?>     </span>
     </div>

     <div class="mobile-show" rel="<?php echo $mobile; ?>">
      <span>
          <?php 
            if(strlen($mobile) > 3 and $mobile <> __('No phone number', 'OSCLASS')) {
              echo substr($mobile, 0, strlen($mobile) - 3) . 'XXX'; 
            } else {
              echo $mobile;
            }
          ?>
      </span>
     </div>     

   </div>

function from helper.php

<?php
function get_realestate_attributes(){
    $locale = osc_current_user_locale();
    $return = array('attributes','other_attributes','special','lang','tel_num');
    $detail = ModelRealEstate::newInstance()->getAttributes( osc_item_id() );
    $keys = array_keys($detail) ;
    if(count($keys) == 1 && $keys[0] == 'locale' && is_null($detail[0]['locale']) ){
        // nothing to do
        return false;
    } 

    if(@@$detail['locale'][$locale]['s_number'] != "") {
        $return['tel_num']['year'] = array(
                 'label' =>__('Telephone number', 'realestate_attributes')
                ,'value' => @@$detail['locale'][$locale]['s_number']
            );
    }
    return $return;
}


function table_phone(){ 
    $detail = get_realestate_attributes();
    if($detail['tel_num']){
    ?>     
        <span>
        <?php
            foreach($detail['tel_num'] as $item){
                echo ''.$item['value'].'';
            } 
        ?>
        </span>

    <?php
    }
}
  • 写回答

1条回答 默认 最新

  • dpv50040 2016-02-09 17:05
    关注

    Change your table_phone() method like this :

    function table_phone(){ 
        $detail = get_realestate_attributes();
        $result = '';
    
        if($detail['tel_num']) {
           foreach($detail['tel_num'] as $item){
                $result .= $item['value'];
            } 
        }
    
        return $result;
    }  
    

    Now you can do $mobile = tab_phone();, you'll not have any output.
    And when you want display it, use echo $mobile.

    Because I've removed the <span> in your method, you can juste add it manually like :

    <span><?php echo $mobile; ?></span>
    

    If you cannot change the method, use a closure like the following :

    if($mobile == '') { 
        $mobile = function() { 
            table_phone(); 
        };
    }
    

    And everywhere you call $mobile, use :

    <?php $mobile(); ?> // Result is displayed
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线