-1

我一生都无法弄清楚如何让这段代码处理多条记录。代码工作正常,它只是插入第一条记录然后停止。

提要: http: //online.compiticket.com/web/events/search.rss

PHP:

<?php 

$con=mysqli_connect("localhost","vadevco_reticket","3wer5top","vadevco_reticket");

if( ! $xml = simplexml_load_file('feed.xml') ) { 
    echo 'unable to load XML file'; 
} else { 
    foreach( $xml as $event ) { 
$idrandom = rand(0,99);
        $title = $event->item->title; 
        $description = $event->item->description;
        $link = $event->item->link;
        $guid = $event->item->guid;
        $pubDate = $event->item->pubDate;
        $image_t = $event->item->image_thumb;
        $image_l = $event->item->image_large;
        $displayDates = $event->item->displayDates;
$sql = "INSERT INTO `vadevco_reticket`.`events` (`title`, `description`, `pubDate`, `image_t`, `image_l`, `displayDates`, `id`, `event_id`) VALUES ('$title', '$description', '$pubDate', '$image_t', '$image_l', '$displayDates','','$idrandom')";
mysqli_query($con,$sql);

        foreach( $event->item->showDates->showDate as $eventDate ) {        
            $date = $eventDate->date;
            $venue = $eventDate->venue;
            $complex = $eventDate->complex;
            $region = $eventDate->region;
            $costs = $eventDate->costs;
$sql2 = "INSERT INTO `vadevco_reticket`.`instances` (`id`, `event_id`, `date`, `venue`, `complex`, `city`, `region`, `costs`) VALUES ('', '$idrandom', '$date', '$venue', '$complex', '$city','$region','$costs')";
mysqli_query($con,$sql2);
          }


 } 

    } 

?> 

任何有关如何使其循环通过提要中 $item 的所有实例并将它们全部插入的建议将不胜感激。

提前致谢!

4

1 回答 1

2

您需要遍历项目而不是 xml 本身:

foreach( $xml->item as $event ) { 
  $idrandom = rand(0,99);
  $title = $event->title; 
  $description = $event->description;
  ...
}
于 2013-05-19T14:23:22.547 回答