0

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?

Shaun Taylor
  • 932
  • 2
  • 16
  • 43
  • You can try this method. http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date – rheeantz May 20 '17 at 16:47
  • I'm so sorry Ryan, I don think I know enough about PHP to click these two together and work out how they relate... So I'm struggling to apply the logic to the come I have. So, is there nothing in my current code that is deciding the order that I can alter? – Shaun Taylor May 24 '17 at 12:33
  • If you cant touch / don't have an access to modify the xml feed you should sort based on attribute you want, in this case is active date – rheeantz May 24 '17 at 14:00
  • Thanks Ryan, that's exactly what I'm trying to do - But Im struggling to understand how to do it... ActiveFrom being the attribute I wanted to sort by... as this is the start date for all shows... – Shaun Taylor May 24 '17 at 14:02
  • ps - I do not have access to modify the xml feed - I have to work with what's there. – Shaun Taylor May 24 '17 at 14:03

1 Answers1

2

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>
rheeantz
  • 960
  • 8
  • 12
  • Thanks a lot Ryan! I assume I integrate the somehow with my current code - Currently, on its own, it displays a list of the dates (in order!) I have it at the bottom of the page here: https://www.leicesterymca.co.uk/show-test/ – Shaun Taylor May 24 '17 at 14:34
  • I see that feed it self was sorted by active from date. You can parsing that feed as it is , or adding additional function within your code just to make sure all shows are sorted based on active form date. – rheeantz May 24 '17 at 14:40
  • Ok - so If add the: //function to sort by array value block to my code... but where? – Shaun Taylor May 24 '17 at 14:42
  • Thanks so much! Works perfectly - I'll be sure to study it and work out what you have changed :) – Shaun Taylor May 24 '17 at 15:57