通过topic获取属性->icon_id

Get the attribute by topic->icon_id

我正在尝试通过 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" />';
                        }
                    }
                }
            }
        }

我刚刚尝试 var_dump/print_r 这个对象,它似乎没有用它的名字加载根元素,所以检查它的名字(需要 php 5.1.3)

编辑:所以如果我理解正确的话,你想要 return 只有 $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
  }
}