在项目符号列表中显示多选框选项

时间:2013-03-31 作者:user30766

我有一个包含多选框的表单。

<form method="POST" action="">
  <select name="countries[]" size="5" multiple="true">
  <option value="bolivia">Bolivia</option>
  <option value="canada">Canada</option>
  <option value="ireland">Ireland</option>
  <option value="mexico">Mexico</option>
  <option value="united states">United States</option>
  </select>
  <input type="submit" value="Submit" size="5"></p>
</form>
代码显示为以下PHP代码:

<?php bp_member_profile_data( \'field=Countries\' ); ?>
如何将结果分为项目符号列表?

1 个回复
SO网友:Ralf912

您的评论如下:

此代码显示成员选择,但仅显示为逗号分隔的列表:<?php bp_member_profile_data( \'field=Countries\' ); ?>

// $countries_csl should contain something like \'hufflepuff, hogwarts, heaven, hell\'
$contries_csl = bp_get_member_profile_data( \'field=Countries\' );
// convert the comma seperated list (csl) into an array by using explode( [delimiter], [string] )
$countries = explode( \',\', $countries_csl );

// check if the string to array conversion was successfull
if ( is_array( $countries ) && ! empty( $countries ) ) {
    // adjust the html to your needs (add some css classes or something else)
    echo \'<ul>\';

    // walk over the array and print out a li-item for each array element
    foreach ( $countries as $country ) {
        // to be sure that every country starts with an UpperCase letter, use ucfirst()
        printf( \'<li>%s</li>\', ucfirst( $country ) );
    }

    // do not forget to close the list
    echo \'</ul>\';
}
你有一个逗号分隔的列表,你需要一个项目符号列表。只需将逗号分隔的列表转换为一个数组,并遍历它以生成项目符号列表。

结束