0

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" />';
                        }
                    }
                }
            }
        }
810
  • 15
  • 1
  • 5
  • where does $topicicontype come from ? why is it in uppercase for B2/B3 while FontAwesome is in lowercase ? Why are you overwriting $icon just after receiving attributes ? Just extract that info to simple variables or use directly. – moped May 06 '16 at 01:08
  • $topic->icon_id comes from a database table. Where the topic icon id is saved, after saving a topic. There is no reason for the uppercase, I added a uppercase for nicer look. could you give me an example, what you think it can be better. – 810 May 06 '16 at 01:37
  • It will work I just see no reason in creating object (and overwriting existing one) just for printing out some variables, so instead of `` I would use just `'` ..check my answer, maybe it'll help – moped May 06 '16 at 01:48

1 Answers1

0

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
  }
}
moped
  • 2,197
  • 2
  • 24
  • 30
  • what exactly doesn't work for you ? I tried it on my localhost and it printed 5 icons. Or you want to return just single icon ? – moped May 06 '16 at 03:15
  • updated the answer, so now it only returns icon with specific id thanks to xpath filter (it always returns array, so $icon[0] – moped May 06 '16 at 03:42
  • well you're making it even worse from what I see. here's your code modified, created a function as it's logical to me.. http://codepad.org/cZ0rvKb4 – moped May 06 '16 at 13:36