Andurit
Andurit

Reputation: 5762

Order output of elements

i write this RSS reader for me and it actualy work really good, but unfortunately i don't understand how PHP order output. Can someone help me with that?

My PHP code:

$reader = new XMLReader();

$reader->open("articles.xml");

while ($reader->read())
{
  if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title")
    echo "<h1>Pøehled aktuálních zpráv ze serveru
      <a href='" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "'>" .
      $title . "</a></h1>";

  // obsluha názvu kanálu
  if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link")
    $title = htmlspecialchars($reader->readString());

  if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "article")
  {
    echo "<dl>";
    while ($reader->read())
    {

      if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link")
                echo "<dt><a href='" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "'>" .
                    $title . "</a></dt>";

      if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title")
                $title = htmlspecialchars($reader->readString());

      if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "description")
                echo "<dd>" .  htmlspecialchars($reader->readString()) . "</dd>";

    }

    echo "</dl>";
  }
}

Output of this is 1. it display that part with desc and just after that it display title which works as a link. But it should be in oposit order.

This is probably something really stupid but i look to this code for too long and can't find the problem.

I will be really happy if someone can help me with that. Thanks

Upvotes: 0

Views: 31

Answers (1)

oradwell
oradwell

Reputation: 402

The echo statements for anchor tags <a> were wrong. I swapped $title with htmlspecialchars($reader->readString(), ENT_QUOTES).

This should be what you wanted:

$reader = new XMLReader();
$reader->open("http://blog.omer.london/feed/");

$title = '';
while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title") {
        echo "<h1>Pøehled aktuálních zpráv ze serveru
      <a href='" . $title . "'>" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "</a></h1>";
    }

    // obsluha názvu kanálu
    if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link") {
        $title = htmlspecialchars($reader->readString());
    } elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "article") {
        echo "<dl>";
        while ($reader->read()) {
            if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "link") {
                echo "<dt><a href='" . $title . "'>" . htmlspecialchars($reader->readString(), ENT_QUOTES) . "</a></dt>";
            } elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "title") {
                $title = htmlspecialchars($reader->readString());
            } elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == "description") {
                echo "<dd>" . htmlspecialchars($reader->readString()) . "</dd>";
            }
        }
        echo "</dl>";
    }
}

Here's the HTML output: http://jsfiddle.net/0qt773eb/

Upvotes: 1

Related Questions