按主题获取属性 - > icon_id

时间:2016-05-05 23:38:21

标签: php xml-simple

我正在尝试通过xml显示主题图标。

如何从右侧图标ID中获取图标。现在他总是加载id 0。

感谢您的帮助。

我尝试并搜索了建议的例子,但没有运气。

的xml:

<icons type="user" width="48" height="48">
    <icon id="0" name="default" published="1" b2="file" b3="file" fa="file"  src="user/default.png" />
    <icon id="1" name="exclamation" published="1" b2="notification-circle" b3="exclamation-sign" fa="exclamation-circle"  src="user/exclamation.png" />
    <icon id="2" name="question" published="1" b2="question-sign" b3="question-sign" fa="question-circle" src="user/question.png" />
    <icon id="3" name="idea" published="1" b2="lamp" b3="lamp" fa="lightbulb-o"  src="user/idea.png" />
    <icon id="4" name="love" published="1" b2="heart" b3="heart" fa="heart"  src="user/love.png" />
</icons>

PHP:

        $topicicon = $topic->icon_id;
        $xmlfile = topicicons.xml';

        if (is_file($xmlfile))
        {
            $xml = simplexml_load_file($xmlfile);

            if (isset($xml->icons))
            {
                foreach ($xml->icons as $icons)
                {

                    foreach ($icons->icon as $icon)
                    {
                        $attributes = $icon->attributes();
                        $icon       = new stdClass();
                        $icon->id   = (int) $attributes->id;
                        $icon->b2   = (string) $attributes->b2;
                        $icon->b3   = (string) $attributes->b3;
                        $icon->fa   = (string) $attributes->fa;
                        $icon->src  = (string) $attributes->src;

                        if ($topicicontype == 'B2')
                        {
                            return '<span class="icon icon-' . $icon->b2. '"></span>';
                        }
                        elseif ($topicicontype == 'B3')
                        {
                            return '<span class="glyphicon glyphicon-' . $icon->b3 . '"></span>';
                        }
                        elseif ($topicicontype == 'fa')
                        {
                            return '<i class="fa fa-' . $icon->fa . '"></i>';
                        }
                        else
                        {
                            return '<img src="' . $icon->src . '" alt="topicicon" />';
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我只是尝试了var_dump / print_r对象,它似乎没有加载根元素及其名称,因此检查其名称(需要php 5.1.3)

编辑:所以如果我理解正确,你只想返回$topicicon = $topic->icon_id的图标,所以这里是更新后的代码

if (is_file($xmlfile))
{
  $xml = simplexml_load_file($xmlfile);

  if (isset($xml) && $xml->getName()=="icons")
  {
    $icon = $xml->xpath('/icons/icon[@id='.$topicicon.']');
    $attributes = $icon[0]->attributes();
    $icon       = new stdClass();
    .. your conditions here
  }
}