XML parsing -> why does this not work?

:o So I wanted to put stuff in an array and started out with the code below. The problem is, its not working, and I can’t quite grasp why.

<?php $xml = simplexml_load_file('http://ehitus.ristiku.com/temp/feed.xml'); for($i = 0; $i < 10; $i++){ $day = $xml->DAY[$i]; echo $day; } ?>

What do you want to echo?

Here’s a working demo of some parsing of that xml

[php]<?php
$xml = simplexml_load_file(‘http://ehitus.ristiku.com/temp/feed.xml’);

foreach ($xml->DAY as $day) {
$show = $day->time->show;
?>
Time: <?= $day->time->attributes()->attr ?>

Name: <?= $show->attributes()->name ?>

Sid: <?= $show->sid ?>

Network: <?= $show->network ?>

Title: <?= $show->title ?>

Ep: <?= $show->ep ?>


<?php }[/php]

Output

[code]Time: 05:30
Name: Jimmy Houston Outdoors
Sid: 39752
Network: Destination America
Title: River and Ocean Fishing
Ep: 04x03

Time: 05:30
Name: The Next Bite
Sid: 39755
Network: Destination America
Title: From the Ground Up: Western Minnesota Muskies
Ep: 11x03

Time: 09:00
Name: Live with Kelly and Michael
Sid: 4263
Network: Syndicated
Title: Kevin Bacon, Troian Bellisario
Ep: 32x101

Time: 07:30
Name: Headhunter Chronicles
Sid: 39839
Network: SPORTSMAN CHANNEL
Title: Season 4, Episode 3
Ep: 04x03
------------------------------------------------------[/code]

Thanks JimL,

This did help me =) However, why did the ‘for’ cycle not work?

P

It worked fine, but you can only echo strings and numbers, you had an array or an object

Well, the goal is to store those results in an array and from there to an mySql DB for further filtration and so on…

Let us know if you need any help with that :slight_smile:

Still struggling with this thing. :-
I must be missing something crucial, as I only get 1 result per day.

[php]

<?php include 'incl/db_connector.php'; $xml = simplexml_load_file('http://ehitus.ristiku.com/temp/feed.xml'); $sql1 = "TRUNCATE TABLE tmp" ; mysql_select_db('tmp'); mysql_query( $sql1, $connection ); // -> my guess is that theres something wrong over here foreach ($xml->DAY as $day) { $kuup = date('y-m-d'); $aeg = date('H:i:s'); $teisend_kuup = date('y-m-d', strtotime($day->attributes()->attr)); $teisend_aeg = date('H:i:s', strtotime($day->time->attributes()->attr)); $show = $day->time->show->attributes()->name; $Sid = $day->time->show->sid; $Network = $day->time->show->network; $Title = $day->time->show->title; $ep = $day->time->show->ep; $link = $day->time->show->link; $sql = "INSERT INTO `ristiku_tvRage`.`tmp` (`id`, `day`, `time`, `sid`, `Show`, `Title`, `Episode`, `Link`, `Network`) VALUES (NULL, '$teisend_kuup', '$teisend_aeg', '$Sid', '$show', '$Title', '$ep', '$link', '$Network');"; mysql_query($sql,$connection); } $sql = $sql = "SELECT * FROM `tmp` WHERE `day`='2014.01.01'"; mysql_select_db('tmp'); $retval = mysql_query( $sql, $connection ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_NUM)) { echo "
  • {$row[2]} | ". " {$row[5]} ". " on {$row[8]}
    ". "
  • "; } mysql_free_result($retval); mysql_close($connection); ?>

    [/php]

    I think I see light in the end of the tunnerl… 8)
    [php]

    <?php $xml = simplexml_load_file('http://ehitus.ristiku.com/temp/feed.xml'); $i=0; foreach ($xml->DAY as $day) { $teisend_kuup = date('y-m-d', strtotime($day->attributes()->attr)); echo ("Kuupäev: $teisend_kuup
    "); foreach ($day->time as $ajad) { $teisend_aeg = date('H:i:s', strtotime($ajad->attributes()->attr)); echo ("
  • Aeg: $teisend_aeg
  • "); } echo(""); } ?>[/php]
    Sponsor our Newsletter | Privacy Policy | Terms of Service