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.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XML parsing with PHP

    Hello everybody! I'm new here and also new with PHP, XML and parsing.

    I'm reading and learning from here, but I haven't had any success with what I want to do.

    I want to parse this XML with PHP to output something like this:
    ('game id->date and time', 'description->match', 'description->league', 'alternative odds->where 1', 'alternative odds->where X', 'alternative odds->where 2', 'double chance->alternative odds->where 1X', 'double chance->alternative odds->where X2', 'Over/under->alternative odds->where Under 2.5 goals', 'Over/under->alternative odds->where Over 2.5 goals')

    Is this even possible? I had no luck, but I'm total beginner here, so help me please!

  • #2
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I made this: www.infotipovi.com/xml.php
    with this code:
    Code:
    <?php
    // load SimpleXML
    $games = new SimpleXMLElement('expekt.xml', null, true);
    
    echo <<<EOF
    <table>
            <tr>
                    <th>Time</th>
                    <th>Game</th>
                    <th>League</th>
                    <th>Tip 1</th>
                    <th>Tip X</th>
                    <th>Tip 2</th>
                    <th>Tip 1X</th>
                    <th>Tip X2</th>
                    <th>Tip U2.5</th>
                    <th>Tip 02.5</th>
            </tr>
    
    EOF;
    foreach($games as $game) // loop through our games
    {
            echo <<<EOF
            <tr>
                    <td>('{$game['date']}{$game['time']}00',</td>
                    <td>'{$game->description}',</td>
                    <td>'{$game->description->category}',</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']},</td>
                    <td>{$game->alternatives->alternative[2]['odds']},</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']},</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']}),</td>
            </tr>
    
    EOF;
    }
    echo '</table>';
    ?>
    I'd like it to be like that:
    DateTime, League, Game, alternative[0] (where normal game), alternative[1] (where normal game), alternative[3] (where normal game), alternative[0] (where normal game: double chance), alternative[1] (where normal game: double chance), alternative[0] (where normal game: Over/under), alternative[1] (where normal game: Over/under)
    I can't isolate this... I don't know how to use something like WHERE in MySQL query or whatever should here be used... I don't know if this is even possible.
    Any ideas?
    I want for example the game FK Jablonec - Banik Ostrava only to go once and with this information in line:
    ('20090817173000',' FK Jablonec - Banik Ostrava', 'Cze. Gambrinus Liga', 2.30, 2.95, 3.25, 1.30, 1.55, 1.65, 2.15,),

  • #3
    New to the CF scene
    Join Date
    Aug 2009
    Location
    Central PA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Not sure if this really answers your question, but I'll take a shot at it.

    It sounds like you want to loop through your XML structure and only display the first category of a certain type. For instance, you want to display games with the description "FK Jablonec - Banik Ostrava" but not with any of the suffixes in later rows (like ": time of first goal", ": first half goals", etc.)

    If that's indeed what you're trying to do, the code updates (in red) below might help. If not, I'm sorry for the confusion.

    Code:
    <?php
    // load SimpleXML
    $games = new SimpleXMLElement('expekt.xml', null, true);
    
    echo <<<EOF
    <table>
            <tr>
                    <th>Time</th>
                    <th>Game</th>
                    <th>League</th>
                    <th>Tip 1</th>
                    <th>Tip X</th>
                    <th>Tip 2</th>
                    <th>Tip 1X</th>
                    <th>Tip X2</th>
                    <th>Tip U2.5</th>
                    <th>Tip 02.5</th>
            </tr>
    
    EOF;
    
    $str_CurrentGame = '';
    
    foreach($games as $game) // loop through our games
    {
            if(!is_numeric(strpos($game->description, $str_CurrentGame)))
            {
                    $str_CurrentGame = $game->description;
            }
            else
            {
                    continue;
            }
    
            echo <<<EOF
            <tr>
                    <td>('{$game['date']}{$game['time']}00',</td>
                    <td>'{$game->description}',</td>
                    <td>'{$game->description->category}',</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']},</td>
                    <td>{$game->alternatives->alternative[2]['odds']},</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']},</td>
                    <td>{$game->alternatives->alternative[0]['odds']},</td>
                    <td>{$game->alternatives->alternative[1]['odds']}),</td>
            </tr>
    
    EOF;
    }
    echo '</table>';
    ?>

  • #4
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for replying!

    Yes that's partly what I want to do.
    I also want that 4th row of alternatives {$game->alternatives->alternative[0]['odds']} and 5th row of alternatives {$game->alternatives->alternative[1]['odds']} come from where description has a suffix ": double chance" and 6th row of alternatives {$game->alternatives->alternative[0]['odds']} and 7th row of alternatives {$game->alternatives->alternative[1]['odds']} come from where description has a suffix ": Over/under"

    Unfortunately, the code you wrote doesn't do the job, the output is the same, with all suffixes.

  • #5
    New to the CF scene
    Join Date
    Aug 2009
    Location
    Central PA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I may have figured out what's going on with the first problem (different variations of the same game still printing out). The issue here is with the way the XML is defined. It includes text data (e.g. "Dagdizel Kaspiysk - Angusht Nazran") with subentities of the main entity (e.g. "<category id="SOCMENEURRUSDV2SOU" order="14400">Rus. Division 2 South</category>"). Apparently, the document is still well-formed, but (IMHO) makes it a good deal more difficult to work with. My recommendation would be to define your XML as follows (change in red):

    Code:
    <game id="5832840" date="20090818" time="1300">
            <description>
                    <category id="SOCMENEURRUSDV2SOU" order="14400">Rus. Division 2 South</category>
                    <subcategory>Dagdizel Kaspiysk - Angusht Nazran</subcategory>
            </description>
            <type id="0"/>
            <alternatives>
                    <alternative odds="1.40" team="223067">1</alternative>
                    <alternative odds="3.90" team="223067">X</alternative>
                    <alternative odds="6.25" team="223062">2</alternative>
            </alternatives>
    </game>
    As far as a fix is concerned, I ended up exporting the content inside the <description> tags as XML, parsing out the <category> tag, and again using the comparison I presented in my previous post. This is certainly no elegant solution (read: a hack), but it gets the job done. Code follows (changes at lines 26 and 28):

    PHP Code:
    <?php
    // load SimpleXML
    $games = new SimpleXMLElement('expekt.xml'nulltrue);

    echo <<<EOF
    <table>
            <tr>
                    <th>Time</th>
                    <th>Game</th>
                    <th>League</th>
                    <th>Tip 1</th>
                    <th>Tip X</th>
                    <th>Tip 2</th>
                    <th>Tip 1X</th>
                    <th>Tip X2</th>
                    <th>Tip U2.5</th>
                    <th>Tip 02.5</th>
            </tr>

    EOF;

    $str_CurrentGame '';

    foreach(
    $games as $game// loop through our games
    {
            
    $str_Description substr($game->description->asXML(), strpos($game->description->asXML(), '</category>')+11, -14);

            if(! 
    strlen($str_CurrentGame) || !is_numeric(strpos($str_Description$str_CurrentGame)))
            {
                    
    $str_CurrentGame $str_Description;
            }
            else
            {
                    continue;
            }

            echo <<<EOF
            <tr>
                    <td>('
    {$game['date']}{$game['time']}00',</td>
                    <td>'
    {$game->description}',</td>
                    <td>'
    {$game->description->category}',</td>
                    <td>
    {$game->alternatives->alternative[0]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[1]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[2]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[0]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[1]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[0]['odds']},</td>
                    <td>
    {$game->alternatives->alternative[1]['odds']}),</td>
            </tr>

    EOF;
    }
    echo 
    '</table>';
    ?>
    I'll get back to you on your second question later, hopefully.

  • #6
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx a lot for your effort, but I don't know why, this isn't working either.


  •  

    Posting Permissions

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