Reputation: 972
I have the following code which displays a theatre show feed on my website, coming from an external ticketing company.
<!-- PULL IN SHOW FEED -->
<div class="container">
<div class="col-md-12 whats-on-feed">
<!-- PULL IN SHOW FEED -->
<div class="container">
<div class="col-md-12 whats-on-feed">
<?php
$xml = simplexml_load_file('https://tickets.leicesterymca.co.uk/feed/shows');
if(isset($wp_query->query_vars['month'])) {
$month = $wp_query->query_vars['month'];
$fromDate = date("Y"). '-'. $month;
$shows = $xml->xpath("//*[contains(ActiveFrom,'$fromDate')]");
}else if(isset($wp_query->query_vars['genre'])) {
$genre = str_replace("-", " ", $wp_query->query_vars['genre']);
if($genre != 'all'){
$shows = $xml->xpath("//*[Genres/Genre/@Name='$genre']");
}else{
$shows = $xml;
}
}else{
$shows = $xml;
}
?>
<div class="block-grid-xs-1 block-grid-sm-2 block-grid-md-3 block-grid-lg-4 show-list">
<?php
$i = 1;
foreach ($shows as $Show) {
$activeFrom=date_create($Show->ActiveFrom);
$activeTo=date_create($Show->ActiveTo);
if(strlen($Show->ShowId) > 0){
?>
<div>
<div class="box">
<div class="caption">
<h3><?php echo $Show->Name ?></h3>
<p><?php echo $activeFrom->format('D d M Y'); ?></p>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/show?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>More Info</a>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/buy?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>Buy Tickets</a>
</div>
</div>
</div>
<div class='image-holder' style='background-image: url("<?php echo $Show->SmallImageUrl ?>");' /></div>
</div>
</div>
<?php $i++; }} ?>
</div>
</div>
This is coming from an XML feed that the ticketing company outputs from their system.
Currently they are displaying in order of show ID, I think this is the default, but I need them to display in order of date - The data node in the xml feed for date is: ActiveFrom.
I have seen this solution:
<xsl:sort select="ActiveFrom" order="ascending" />
But I don't really know if A: it's the correct approach, or B: Where I would add it in relation to my code? Can anyone help?
Upvotes: 0
Views: 39
Reputation: 1020
You can try this code
$xml = 'https://tickets.leicesterymca.co.uk/feed/shows';
$data = simplexml_load_file($xml);
$encode = json_encode($data);
$decode = json_decode($encode, true);
$show = $decode['Show'];
//function to sort by array value
function date_compare($a, $b) {
$t1 = strtotime($a['ActiveFrom']);
$t2 = strtotime($b['ActiveFrom']);
return $t1 - $t2;
}
usort($show, 'date_compare');
//test active from value
foreach ($show as $time) {
echo $time['ActiveFrom'].'<br>';
}
//echo '<pre>'.print_r($show, true).'</pre>';
Edited
<?php
$xml = 'https://tickets.leicesterymca.co.uk/feed/shows';
$data = simplexml_load_file($xml);
$encode = json_encode($data);
$decode = json_decode($encode);
$results = $decode->Show;
//function to sort by object (not array)
function date_compare($a, $b) {
$t1 = strtotime($a->ActiveFrom);
$t2 = strtotime($b->ActiveFrom);
return $t2 - $t1; // reverse it to check it works or not $t2 - t1
}
usort($results , 'date_compare');
if(isset($wp_query->query_vars['month'])) {
$month = $wp_query->query_vars['month'];
$fromDate = date("Y"). '-'. $month;
$shows = $xml->xpath("//*[contains(ActiveFrom,'$fromDate')]");
}
else if(isset($wp_query->query_vars['genre'])) {
$genre = str_replace("-", " ", $wp_query->query_vars['genre']);
if($genre != 'all'){
$shows = $xml->xpath("//*[Genres/Genre/@Name='$genre']");
}else{
$shows = $results;
}
}
else {
$shows = $results;
}
?>
<!--next-->
<?php
$i = 1;
foreach ($results as $Show) {
$activeFrom = date_create($Show->ActiveFrom);
$activeTo = date_create($Show->ActiveTo);
if(strlen($Show->ShowId) > 0){
?>
<div>
<div class="box">
<div class="caption">
<h3><?php echo $Show->Name ?></h3>
<p><?php echo $activeFrom->format('D d M Y'); ?></p>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/show?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>More Info</a>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<a href='/buy?showid=<?php echo $Show->ShowId ?>' class='btn btn-success btn-sm btn-clear-white'>Buy Tickets</a>
</div>
</div>
</div>
<div class='image-holder' style='background-image: url("<?php echo $Show->SmallImageUrl ?>");' /></div>
</div>
</div>
<?php $i++; }} ?>
</div>
</div>
Upvotes: 2