Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
    function sortByTheTimeASC($a$b)
    {
        
    $iResult 0;
        if (
    is_object($a) && is_object($b) && isset($a->thetime$b->thetime))
        {
            
    // Eventually these could be a problem mathematically speaking.  Better to make them times.
            
    $aParts sscanf($a->thetime'%4s%2s%2s%2s%2s');
            
    $bParts sscanf($b->thetime'%4s%2s%2s%2s%2s');
            
    $adt = new DateTime(vsprintf('%s%s%st%s%s00'$aParts));
            
    $bdt = new DateTime(vsprintf('%s%s%st%s%s00'$bParts));
            
    $iResult = ($adt $bdt : ($adt $bdt ? -0));
        }
        return 
    $iResult;
    }

    usort($aCopy'sortByTheTimeASC'); 
    Edit:
    Oh yeah that assumes thetime is YYYYMMDDHHII. The 0802 could be either MMDD or DDMM, and I can't tell which, so I just took general convention :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  2. Users who have thanked Fou-Lu for this post:

    denhamd2 (09-02-2013)

  3. #17
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Perfect

    Thanks so much for your help!
    Last edited by denhamd2; 09-02-2013 at 10:28 PM.

  4. #18
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry again to bother. Below is how I'm looping through the matches and links - working great

    The problem is I have added an attribute kbps to <link> in my XML file (<link kbps="425">link4.html</link>. Do you know how I can access this attribute within my loop?

    Code:
    foreach ($aCopy AS $thematches)
    {
    
    foreach ($thematches->linkset->link AS $link)
            {
                $singlematchlinks .= '<div>'.$link.'</div>';
            }
    }

  5. #19
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You can find the attributes in the $link->attributes(). Extract it to an array variable and dereference the name you are looking for.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  6. #20
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Perfect, I was able to access that now

    Final final question... it it possible to sort the <links> by the kbps attribute?

    Thanks again, this is the final question I promise.

  7. #21
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, pretty much the same as above:
    PHP Code:
    function sortByKBPSASC($a$b)
    {
        
    $iResult 0;
        if (
    is_object($a) && is_object($b) && ($a instanceof SimpleXMLElement) && ($b instanceof SimpleXMLElement))
        {
            
    $aAttr $a->attributes();
            
    $bAttr $b->attributes();
            if (isset(
    $aAttr['kbps'], $bAttr['kbps']))
            {
                
    $iResult $aAttr['kbps'] - $bAttr['kbps'];
            }
        }
        return 
    $iResult;
    }

    //. . .
    usort($thematches->linkset->link'sortByKBPSASC'); 
    Prior to the foreach. Works okay in my head, not sure how it'll turn out though :P
    With these types of changes though, you may want to consider making more use of the xpath functionality. The downside of that is that the libxml libraries will dictate what will work with it, so you may not be able to use xpath 2.0 functionality if you only have a 1.0 library for example.

    Edit:
    Hindsight is 20/20, you could probably typehint this method instead of using the object and instanceof checks.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  8. #22
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You can find the attributes in the $link->attributes(). Extract it to an array variable and dereference the name you are looking for.
    Hi again,

    Sorry to drag up this thread again.

    Something very strange is happening. The links are combining under the same event names ok, but the issue is the attributes for the 2nd linkset isnt showing up - it outputs something like this:

    Code:
    Link 1 en 300 Watch
    Link 2 en 350 Watch
                       Watch
                       Watch
    The XML looks like this:
    Code:
    <match><sportname>Football</sportname><tournamentname>Red League</tournamentname><thetime>201311021245</thetime><matchname dateandtime="1383396300">Town Rovers vs Chanel</matchname><linkset><link lang="en" kbps="300" channelname="Link 1">link1.html</link><link lang="en" kbps="350" channelname="Link 2">link2.html</link></linkset></match>
    
    <match><sportname>Football</sportname><tournamentname>Red League</tournamentname><thetime>201311021245</thetime><matchname dateandtime="1383396300">Town Rovers vs Chanel</matchname><linkset><link lang="en" kbps="320" channelname="Link 3">link3c.html</link><link lang="en" kbps="320" channelname="Link 3">link4.html</link></linkset></match>
    Any ideas?

    Thanks again in advance for your help.

  9. #23
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Not with that block of data.
    If you have empty entries, it indicates that there is an empty link in the linkset indicating that the data provides a result without a linkset.
    You'll have to track down that exact block of xml causing that to see what the cause is.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  10. #24
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I had a look in my XML file and all looks good. Here is what the array prints out with the data I've given above.

    The other strange thing is link4 seems to go totally missing...


    Code:
    Array
    (
        [Town Rovers vs Chanel] => SimpleXMLElement Object
            (
                [sportname] => Football
                [tournamentname] => Red League
                [thetime] => 201311021245
                [matchname] => Town Rovers vs Chanel
                [linkset] => SimpleXMLElement Object
                    (
                        [link] => Array
                            (
                                [0] => link1.html
                                [1] => link2.html
                                [2] => link3.html
                            )
    
                    )
    
            )
    
    )


    Here is how I'm outputting the HTML (getting the attributes):

    PHP Code:
    foreach ($aCopy AS $thematches)


    {

      
    // BUILD THE LINK NAMES FOR THE BASIC STANDALONE PAGES  
      
    $singlematchname strtolower($thematches->matchname); // FORMAT 

      
    $singlematchlinks '';


    // LOOP THROUGH ALL LINKS



    foreach ($thematches->linkset->link AS $link)
            {
                
                
    $singlematchlinks .= '<div class="matchrow"><div class="channelname">'.$link["channelname"].'</div><div class="language">'.$link["lang"].'</div><div class="kbps">'.$link["kbps"].'</div><div class="link"><a href="'.$link.'" target="_blank">Watch</a></div><div class="clearb"></div></div>';
                
                
          
    }

    echo 
    '<h5>'.$singlematchname.'</h5>'.$singlematchlinks;
      } 
    Any idea what I might be doing wrong?


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •