Search code examples
phpxml-simple

Get the attribute by topic->icon_id


I'm trying to show the topic icon by xml.

How can I get the icon from the right icon id. Now he is always loading id 0.

Thank you for the help.

I tried and searched suggested examples, but no luck.


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" />';
                        }
                    }
                }
            }
        }

Solution

  • I just tried to var_dump/print_r the object and it seems that it doesn't load root element with its name, so checking for its name (requires php 5.1.3)

    EDIT: so if I understand it right, you want to return only icon that is $topicicon = $topic->icon_id, so here is the updated code

    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
      }
    }