PHP变量无法在正确的位置呈现

时间:2013-03-13 07:13:13

标签: php wordpress syntax concatenation buddypress

我在PHP中有以下内容,使用Wordpress + Buddypress插件:

$blogusers = get_users('role=s2member_level1&number=5');
foreach ($blogusers as $user) {
  $username = str_replace('"', '',$user->user_nicename);
  $fullname = str_replace('"', '',bp_profile_field_data( array( 'user_id'=>$user->ID,'field'=>'name' ) ));
  $category = str_replace('"', '',bp_profile_field_data( array( 'user_id'=>$user->ID, 'field'=>'category' ) ));
  echo '<div style="float:left; width:100px; height:100px;">';
  echo '<h3><a href="members/' . $username . '">' . $fullname .  '</a></h3>';
  echo '<strong>Camp Type: </strong>' . $category . '<br />';
  echo '</div>';
}

$ fullname和$ category vars在float:left元素之前碰到了,但是a $ username变量保持不变,这就是所有变量应该呈现的方式。

即:

<div class="page" id="blog-latest" role="main">
<div style="width:500px;height:100px;position:relative;">
Frank GoreType 4<div style="float:left; width:100px; height:100px;"><h3><a href="members/fgore"></a></h3><strong>Camp Type: </strong><br /></div>
</div>

任何线索都是为什么这两个变量被抛出echo语句?

2 个答案:

答案 0 :(得分:0)

它与php的echo构造如何处理连接有关。它对我来说完全没有意义,但显然,当连接发生时,它会打开一个临时变量来执行组合,然后输出结果。

如果您要使用该语法,请在echo语句中用逗号替换句点。 Echo将逗号分隔值视为单独的语句,并按接收顺序回显它们。

另外,它的速度要快一点,但每百万个回声语句只有1秒。 :)

我首选的方法是创建一个html字符串,然后每个逻辑块回显一次。

$echo = '<div style="float:left; width:100px; height:100px;">'
      . '<h3><a href="members/' . $username . '">' . $fullname .  '</a></h3>'
      . '<strong>Camp Type: </strong>' . $category . '<br />'
      . '</div>';
echo $echo;

答案 1 :(得分:0)

事实证明它与bp_profile_field_data有关。它是核心用于检索数据的函数。开发人员更友好的方法get_bp_profile_field_data可以更好地使用。我正在格式化所有字符串。不过,我感谢你分享知识。谢谢!