Jump to content

Sorting XML items


Josh
 Share

Recommended Posts

I thought maybe someone here might know the answer to this. I have three XML data sets I am loading from Steam, and I want to sort them by date and display the most recent five items. This is the code that handles the recent activity on the forum index page:

$recordCount=3;
$fileType=4;
$queryType = 1;
$webAPIKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function LoadXML($path)
{
  $content = utf8_encode(file_get_contents($path));
  if (trim($content=='')) return NULL;
  $content = str_replace(chr(11),'',$content);
  return simplexml_load_string($content);
}

function clean($string)
{
  return preg_replace('/[^A-Za-z0-9\- ().,!]/', '', $string); // Removes special chars.
}

function truncate($string, $length)
{
   if (strlen($string)>$length)
   {
       return substr($string,0,$length-3)."...";
   }
   else
   {
       return substr($string,0,$length);
   }
}

/*
filetype = 0 Workshop items
Filetype = 1 Workshop collections
Filetype = 2 returns artwork
Filetype = 3 returns videos
Filetype = 4 returns screenshots
Filetype = 5 returns info on the Leadwerks app, I think
*/

$steamcontent = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

$fileType=3;
$recordCount2=1;
$steamcontent2 = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount2.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

$fileType=0;
$recordCount3=1;
$steamcontent3 = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount2.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

if ($steamcontent)
{
   if (min($recordCount,$steamcontent->total)>0)
   {
       /*echo("<h3 class='maintitle'>Latest Screenshots</h3>");*/        
       echo("<div style='height:4px;'></div>");
       echo("<table style='table-layout: fixed; padding: 0px; border-spacing: 0px; width: 100%;'>");

       for ($i=0; $i<min($recordCount,$steamcontent->total); $i=$i+1)
       {
           if ($steamcontent->publishedfiledetails->message[$i]->result==1)
           {
               $title = clean($steamcontent->publishedfiledetails->message[$i]->title);
               if ($title=="") $title = truncate(clean($steamcontent->publishedfiledetails->message[$i]->short_description),35);
               /*if ($title=="") $title = "Screenshot";*/
               echo("<td style='width: 20%; padding: 2px; border-spacing: 0px;'><a title='".$title."' href='http://www.leadwerks.com/werkspace/page/viewitem?fileid=".$steamcontent->publishedfiledetails->message[$i]->publishedfileid."'><img class='resized_img' style='max-width:100%;' src='".$steamcontent->publishedfiledetails->message[$i]->preview_url."/256x144.resizedimage' /></a></td>");
           }
       }

       $steamcontent = $steamcontent2;
       for ($i=0; $i<min($recordCount2,$steamcontent->total); $i=$i+1)
       {
           if ($steamcontent->publishedfiledetails->message[$i]->result==1)
           {
               $title = clean($steamcontent->publishedfiledetails->message[$i]->title);
               if ($title=="") $title = truncate(clean($steamcontent->publishedfiledetails->message[$i]->short_description),35);
               /*if ($title=="") $title = "Screenshot";*/
               echo("<td style='width: 20%; padding: 2px; border-spacing: 0px;'><a title='".$title."' href='http://www.leadwerks.com/werkspace/page/viewitem?fileid=".$steamcontent->publishedfiledetails->message[$i]->publishedfileid."'><img class='resized_img' style='max-width:100%;' src='http://i4.ytimg.com/vi/". $steamcontent->publishedfiledetails->message[$i]->youtubevideoid."/mqdefault.jpg' /></a></td>");
           }
       }    

       $steamcontent = $steamcontent3;
       for ($i=0; $i<min($recordCount3,$steamcontent->total); $i=$i+1)
       {
           if ($steamcontent->publishedfiledetails->message[$i]->result==1)
           {
               $title = clean($steamcontent->publishedfiledetails->message[$i]->title);
               if ($title=="") $title = truncate(clean($steamcontent->publishedfiledetails->message[$i]->short_description),35);
               /*if ($title=="") $title = "Screenshot";*/
               echo("<td style='width: 20%; padding: 2px; border-spacing: 0px;'><a title='".$title."' href='http://www.leadwerks.com/werkspace/page/viewitem?fileid=".$steamcontent->publishedfiledetails->message[$i]->publishedfileid."'><img class='resized_img' style='max-width:100%;' src='".$steamcontent->publishedfiledetails->message[$i]->preview_url."/256x144.resizedimage' /></a></td>");
           }
       }

       echo("</table>");
       echo("<div style='height:4px;'></div>");
   }
}

 

The creation date is accessible with:

$steamcontent->publishedfiledetails->message[$i]->time_created

 

Any ideas how to handle this?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

So here is my solution. Requires some modification depending on your PHP verson and the format of the date.

 

<?php
$recordCount=3;
$fileType=4;
$queryType = 1;
$webAPIKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function LoadXML($path)
{
$content = utf8_encode(file_get_contents($path));
if (trim($content=='')) return NULL;
$content = str_replace(chr(11),'',$content);
return simplexml_load_string($content);
}

function clean($string)
{
return preg_replace('/[^A-Za-z0-9\- ().,!]/', '', $string); // Removes special chars.
}

function truncate($string, $length)
{
if (strlen($string)>$length)
{
return substr($string,0,$length-3)."...";
}
else
{
return substr($string,0,$length);
}
}

// TODO: Look at your PHP version to see how pass by refernece is handled. Is it declared on the function or handled on the caller. One is depreciated in the latest php
function xmlArrayAppend(&$items, $xml)
{
if($xml !== false)
{
for($i = 0; $i < $xml->total; $i++)
{
$items[] = $xml->publishedfiledetails->message[$i];
}
}
}

// http://php.net/manual/en/function.usort.php $value_compare_func param
/*The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.*/
function xmlTimeSort($a, $b)
{
//compare $a->time_created to $b->time_created;
// Are these strings, timestamps, date objects?
// If I had more info I could help here
}

/*
filetype = 0 Workshop items
Filetype = 1 Workshop collections
Filetype = 2 returns artwork
Filetype = 3 returns videos
Filetype = 4 returns screenshots
Filetype = 5 returns info on the Leadwerks app, I think
*/

$steamcontent = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

$fileType=3;
$recordCount2=1;
$steamcontent2 = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount2.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

$fileType=0;
$recordCount3=1;
$steamcontent3 = LoadXML('http://api.steampowered.com/IPublishedFileService/QueryFiles/v0001?key='.$webAPIKey.'&format=xml&query_type='.$queryType.'&page=1&numperpage='.$recordCount2.'&appid=251810&filetype='.$fileType.'&return_vote_data=1&return_short_description=1');

$items = [];
// TODO: Look at your PHP version to see how pass by refernece is handled. Is it declared on the function or handled on the caller. One is depreciated in the latest php
xmlArrayAppend(&$items, $steamcontent);
xmlArrayAppend(&$items, $steamcontent2);
xmlArrayAppend(&$items, $steamcontent3);
usort($items, xmlTimeSort);
for($i = 0; $i < $recordCount; $i++)
{
$item = $items[$i];
echo "<td style='width: 20%; padding: 2px; border-spacing: 0px;'><a title='".$item->title."' href='http://www.leadwerks.com/werkspace/page/viewitem?fileid=".$item->publishedfileid."'><img class='resized_img' style='max-width:100%;' src='".$item->preview_url."/256x144.resizedimage' /></a></td>");
}

 

 

Basically you store all 9 items in an array, use usort to sort the array by the time_created, then echo the last 3 off.

 

I'd recommend using something like SimplePie as it supports XML caching and you don't want to do 3 page requests every time someone refreshes your page.

 

http://simplepie.org/

Link to comment
Share on other sites

I display the date like this:

$formatteddisplaytime = date('D, d M Y, h:m:s', intval($displaytime));

 

So $displaytime is some type of value that can be converted into an integer.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...