-1

I cant for the life of me figure out how to get this code to process more than one record. The code works fine, it just inserts the first record and then stops.

The feed: http://online.computicket.com/web/events/search.rss

The 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);
          }


 } 

    } 

?> 

Any advice on how to make it loop through all instances of $item in the feed and insert them all would be much appreciated.

Thanks in advance!

Chris
  • 438
  • 4
  • 17
  • http://php.net/simplexml.examples-basic - In your case: You made a mistake somethwere there in, double-check each variable you loop over it contains what you're looking for and then fix it. It's that easy and we can't do anything about it. – hakre May 19 '13 at 14:38
  • Another hint: Separate your code into subroutines. Let a routine do a specific thing. Test each routine is doing well, then bring the routines together. This often then works automatically otherwise easy to debug. Compare with: [PHP file cannot enter some part of code](http://stackoverflow.com/q/11575531/367456) – hakre May 19 '13 at 14:41
  • 1
    Please, stop concatenating the queries and learn how to use prepared statements. – tereško May 19 '13 at 14:42
  • @tereško thanks, could you expand on that so I know what to research? – Chris May 19 '13 at 17:20
  • @Chris'Horris'Edington , look into prepared statements. [This](https://www.youtube.com/watch?v=nLinqtCfhKY) and [this](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) should give you some directions. – tereško May 19 '13 at 18:49

1 Answers1

2

You need to loop through the items rather than the xml itself:

foreach( $xml->item as $event ) { 
  $idrandom = rand(0,99);
  $title = $event->title; 
  $description = $event->description;
  ...
}
Rob Johnstone
  • 1,704
  • 9
  • 14