My php code to read my xml is there anyway to sort by date or event_id?

<?php

  $doc = new DOMDocument();
  $doc->load( '../events.xml' );
  $date = array(); // initialize data array 
 
  $books = $doc->getElementsByTagName( "event" );
  foreach( $books as $book )
  {
  $evts = $book->getElementsByTagName( "event_id" );
  $evt = $evts->item(0)->nodeValue;
  
  $titles = $book->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  
  $images = $book->getElementsByTagName( "image" );
  $image = $images->item(0)->nodeValue;
  
  $dates = $book->getElementsByTagName( "date" );
  $date = $dates->item(0)->nodeValue;
  
  
  $descriptions = $book->getElementsByTagName( "description" );
  $description = $descriptions->item(0)->nodeValue;
  
  echo "
  What - $title <br />
  When - $date <br />
  $description <br/ >
  <img src ='../$image' width='75 height='75'>
  <br /><br />
  ";
}


?>

Recommended Answers

All 3 Replies

I got this to work but it sorts it by the 1st digit of the event ID so it lists correctly for the 100's but then goes to 20's 30's and so on any idea how to get usort fixed so I get 100,99,98,22,21,20,10,8,7,6,1

100
101
102
103
104
22
81
82
83
90
91

<?php

// sort by EventID
function sortEvent_id($a1, $a2){
    return strcmp($a1->event_id, $a2->event_id) ;
}
// sort the Event
function sortEvent($t1, $t2) {
    return strcmp($t1['event'], $t2['event']);
}


$prop1s = simplexml_load_file('../events.xml');
foreach ($prop1s->events as $prop1) {
    $sortedEvents = array();
    foreach($prop1->event as $events) {
        $sortedEvents[] = $events;
        }
    usort($sortedEvents, "sortEvent"); // finish the sortEvents
    /* --- */
    echo '<pre>'."\n";
    print_r($sortedEvents);
    echo '</pre>'."\n"; 
    /* --- */
	
    foreach ($prop1s->children() as $eventattr) { // this doesn't do it
    //foreach($sortedStates as $hotel => @attributes){ // blargh!
        if(isset($eventattr->event)) {
            $statearr = $eventattr->attributes();
            echo '<optgroup label="'.$statearr['title'].'">'."\n";
            $options = array();
            foreach($eventattr->event as $info1) {
                $options[] = $info1;                            
            }
            usort($options, "sortEvent_id"); // finish by EventID  
            foreach($options as $eventattr => $info){
                echo '<option value="'.$info->event_id.'">'.$info->event_id.'</option>'."\n";
				 echo '<option value="'.$info->date.'">'.$info->date.'</option>'."\n";
				  echo '<option value="'.$info->date.'">'.$info->title.'</option>'."\n";
            }
            echo '</optgroup>'."\n";
            } else {
                //empty nodes don't do squat
            }
    }
}  
?>

Use intval, and compare then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.