You are here:  » Reverse the sorting from an XML feed


Reverse the sorting from an XML feed

Submitted by rcasey on Tue, 2013-08-20 00:36 in

Hello, I'm hoping someone can help me reverse the order my XML feed is parsed. There is some conditional stuff in here, but I don't believe that should affect reversing the order that things are parsed. Here's what I'm working with:

<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    // This is where you write your code to process each record, such as loading a database
    // Call records:?>
    <div class="block">
    <?php if($record["GAME-HASRESULT"] == "Y") { ?>
            <table class="boxscore">
                <tbody>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOLNAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank"><?php print $record["GAME-SCORE1"]; ?></a></td>
                    </tr>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOL2NAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank"><?php print $record["GAME-SCORE2"]; ?></a></td>
                    </tr>
                    <tr>
                      <td class="info">Final (<?php echo str_replace("/2013"""$record["GAME-DATE"]); ?>)</td>
                      <td class="school-class"><?php echo str_replace("Division """$record["GAME-CLASS"]); ?></td>
                    </tr>
                </tbody>
            </table>
    <?php } else { ?>
            <table class="boxscore">
                <tbody>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOLNAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank">-</a></td>
                    </tr>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOL2NAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank">-</a></td>
                    </tr>
                    <tr>
                      <td class="info"><?php print $record["GAME-TIME"]; ?> (<?php echo str_replace("/2013"""$record["GAME-DATE"]); ?>)</td>
                      <td class="school-class"><?php echo str_replace("Division """$record["GAME-CLASS"]); ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    <?php ?>
<?php
  }
  MagicParser_parse("{url goes here}","myRecordHandler","xml|GAMES/GAME/");
?>

Any help would be greatly appreciated. Thank you!

Submitted by support on Tue, 2013-08-20 08:29

Hi,

The trick here (although this is memory dependent - let me know if that's a problem due to teh size of your feed) is to parse the feed into an array, reverse the array, and then process the records from the reversed order array using your existing myRecordHandler function... consider the following example which should do the trick - based on your code above;

<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    // This is where you write your code to process each record, such as loading a database
    // Call records:?>
    <div class="block">
    <?php if($record["GAME-HASRESULT"] == "Y") { ?>
            <table class="boxscore">
                <tbody>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOLNAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank"><?php print $record["GAME-SCORE1"]; ?></a></td>
                    </tr>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOL2NAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank"><?php print $record["GAME-SCORE2"]; ?></a></td>
                    </tr>
                    <tr>
                      <td class="info">Final (<?php echo str_replace("/2013"""$record["GAME-DATE"]); ?>)</td>
                      <td class="school-class"><?php echo str_replace("Division """$record["GAME-CLASS"]); ?></td>
                    </tr>
                </tbody>
            </table>
    <?php } else { ?>
            <table class="boxscore">
                <tbody>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOLNAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank">-</a></td>
                    </tr>
                    <tr>
                        <td class="team" width="100%"><?php print $record["GAME-SCHOOL2NAME"]; ?></td>
                        <td class="score"><a href="<?php print $record["GAME-TARGETURL"]; ?>" target="_blank">-</a></td>
                    </tr>
                    <tr>
                      <td class="info"><?php print $record["GAME-TIME"]; ?> (<?php echo str_replace("/2013"""$record["GAME-DATE"]); ?>)</td>
                      <td class="school-class"><?php echo str_replace("Division """$record["GAME-CLASS"]); ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    <?php ?>
<?php
  }
  // parse records into $records array
  $records = array();
  function mySortRecordHandler($record)
  {
    global $records;
    $records[] = $record;
  }
  MagicParser_parse("{url goes here}","mySortRecordHandler","xml|GAMES/GAME/");
  // reverse the array
  $records = array_reverse($records);
  // and process with original myRecordHandler function
  foreach($records as $record)
  {
    myRecordHandler($record);
  }
?>

Hope this helps!
Cheers,
David.

Submitted by rcasey on Wed, 2013-08-28 21:22

Thank you, that did help. They ended up just reversing the XML feed after I figured everything out. Ah well!