• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

sort xml document

I have the below code. I want to sort by the tag name Date. Can someone help me to get this done?




$xmlDoc1 = new DOMDocument();
$xmlDoc1->preserveWhiteSpace = false;
$xmlDoc1->formatOutput = true;
$xmlDoc1->loadXML($response->GetEventInfoResult->any); 


$dataset1 = $xmlDoc1->getElementsByTagName( "Table" );
foreach( $dataset1 as $row1 )
{
 
    $xmlEvents = $row1->getElementsByTagName( "Event" );
    $xmlEvent = $xmlEvents->item(0)->nodeValue;
 
    $xmlDates = $row1->getElementsByTagName( "Date" );
    $xmlDate = $xmlDates->item(0)->nodeValue;
    $xmlDate = explode("T",$xmlDate);
    $xmlDate = $xmlDate[0];
    $xmlDate = date("n/j/y", strtotime($xmlDate));

Open in new window

0
JPERKS1985
Asked:
JPERKS1985
  • 5
  • 3
1 Solution
 
hieloCommented:

$xmlDoc1 = new DOMDocument();
$xmlDoc1->preserveWhiteSpace = false;
$xmlDoc1->formatOutput = true;
$xmlDoc1->loadXML($response->GetEventInfoResult->any); 


$dataset1 = $xmlDoc1->getElementsByTagName( "Table" );

$result=array();
foreach( $dataset1 as $row1 )
{
 
    $xmlEvents = $row1->getElementsByTagName( "Event" );
    $xmlEvent = $xmlEvents->item(0)->nodeValue;
 
    $xmlDates = $row1->getElementsByTagName( "Date" );
    $xmlDate = $xmlDates->item(0)->nodeValue;
    $xmlDate = explode("T",$xmlDate);
    $xmlDate = $xmlDate[0];
    $xmlDate = date("n/j/y", strtotime($xmlDate));

   
   $result[ strtotime($xmlDate) ] = array("event"=>$xmlEvent, "date"=> $xmlDate);
}

sort( $result );
print_r($result);

Open in new window

0
 
JPERKS1985Author Commented:
This is my output,


Array ( [0] => Array ( [event] => Cats (7:00 PM) [date] => 4/13/10 ) ) Array ( [0] => Array ( [event] => Cats (7:30 PM) [date] => 4/14/10 ) ) Array ( [0] => Array ( [event] => Cats (7:30 PM) [date] => 4/15/10 ) ) Array ( [0] => Array ( [event] => Cats (8:00 PM) [date] => 4/16/10 ) ) Array ( [0] => Array ( [event] => Boston Bruins vs Carolina Hurricanes [date] => 4/10/10 ) ) Array ( [0] => Array ( [event] => Boston Celtics vs Washington Wizards [date] => 4/9/10 ) ) Array ( [0] => Array ( [event] => Boston Celtics vs Milwaukee Bucks [date] => 4/14/10 ) ) Array ( [0] => Array ( [event] => Joe Bonamassa [date] => 4/16/10 ) ) Array ( [0] => Array ( [event] => Boston Blazers vs Minnesota Swarm [date] => 4/10/10 ) ) Array ( [0] => Array ( [event] => Boston Red Sox vs Tampa Bay Rays [date] => 4/16/10 ) ) Array ( [0] => Array ( [event] => Alvin Ailey American Dance Theatre [date] => 4/9/10 ) ) Array ( [0] => Array ( [event] => Coppelia [date] => 4/9/10 ) ) Array ( [0] => Array ( [event] => Coppelia [date] => 4/10/10 ) ) Array ( [0] => Array ( [event] => Coppelia [date] => 4/11/10 ) ) Array ( [0] => Array ( [event] => Coppelia [date] => 4/15/10 ) ) Array ( [0] => Array ( [event] => Coppelia [date] => 4/16/10 ) ) Array ( [0] => Array ( [event] => Coppelia (Matinee) [date] => 4/10/10 ) ) Array ( [0] => Array ( [event] => Smuckers Stars On Ice [date] => 4/11/10 ) ) Array ( [0] => Array ( [event] => Phantom Gourmet Wine and Food Phest [date] => 4/10/10 ) ) Array ( [0] => Array ( [event] => Roberto Carlos [date] => 4/14/10 ) )

i'm not sure why it doesn't work.
0
 
JPERKS1985Author Commented:
Also multiple events have the same date if that matters.
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
hieloCommented:

$xmlDoc1 = new DOMDocument();
$xmlDoc1->preserveWhiteSpace = false;
$xmlDoc1->formatOutput = true;
$xmlDoc1->loadXML($response->GetEventInfoResult->any); 


$dataset1 = $xmlDoc1->getElementsByTagName( "Table" );

$result=array();
foreach( $dataset1 as $row1 )
{
 
    $xmlEvents = $row1->getElementsByTagName( "Event" );
    $xmlEvent = $xmlEvents->item(0)->nodeValue;
 
    $xmlDates = $row1->getElementsByTagName( "Date" );
    $xmlDate = $xmlDates->item(0)->nodeValue;
    $stamp=date('Y-m-d H:i:s strtotime($xmlDate));
    $xmlDate = explode("T",$xmlDate);
    $xmlDate = $xmlDate[0];
    $xmlDate = date("n/j/y", strtotime($xmlDate));

   
   $result[ ] = array("event"=>$xmlEvent, "date"=> $xmlDate, "sortKey"=>$stamp);
}
function cmp($a, $b)
{
    if ($a["sortKey"] == $b["sortKey"]) {
        return 0;
    }
    return ($a["sortKey"] < $b["sortKey"]) ? -1 : 1;
}

usort( $result,"cmp");
print_r($result);

Open in new window

0
 
JPERKS1985Author Commented:
It prints the first item that takes place and not the rest now.
0
 
JPERKS1985Author Commented:
Heres my full code.
try {
//Get Geo Events
$User = "";
$Password = "";
$Domain = "";


$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");

$result=array();

//Get two weeks out
$Start = date('Y-m-d');
$End = date("Y-m-d", strtotime(date("Y-m-d", strtotime($Start)) . " +1 week"));

$paramSearchEvents = array('UserName' => $User, 'Password' => $Password, 
        'DomainName' => $Domain, 'Event' => '', 'Venue' => '', 
        'StartDate' => $Start, 'EndDate' => $End, 'City' => $UserCity, 'State' => $UserState,
         'bWithTickets' => 'false', 'bVerbose' => 'true');

$searchEventsresponse = $client->SearchEvents($paramSearchEvents); 

//var_dump($searchEventsresponse);

$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->loadXML($searchEventsresponse->SearchEventsResult->any); 

/*
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
    print $item->nodeName . " = " . $item->nodeValue . "<br />";
}
*/




$dataset = $xmlDoc->getElementsByTagName( "Table" );
foreach( $dataset as $row) if ($intCount++ < 20) //Limit to 20 events.
{
    $xmlEventIDs = $row->getElementsByTagName( "EventID" );
    $xmlEventID = $xmlEventIDs->item(0)->nodeValue;
 
   


    //Second request gets event specific information.


	$paramGetEventInfo = array('UserName' => $User, 'Password' => $Password, 
        'DomainName' => $Domain, 'EventID' => $xmlEventID);

	$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");


    $response = $client->GetEventInfo($paramGetEventInfo);



$xmlDoc1 = new DOMDocument();
$xmlDoc1->preserveWhiteSpace = false;
$xmlDoc1->formatOutput = true;
$xmlDoc1->loadXML($response->GetEventInfoResult->any); 



$dataset1 = $xmlDoc1->getElementsByTagName( "Table" );
foreach( $dataset1 as $row1 )
{
 
    $xmlEvents = $row1->getElementsByTagName( "Event" );
    $xmlEvent = $xmlEvents->item(0)->nodeValue;
 
    $xmlDates = $row1->getElementsByTagName( "Date" );
    $xmlDate = $xmlDates->item(0)->nodeValue;
    $stamp=date("Y-m-d H:i:s", strtotime($xmlDate));

    $xmlDate = explode("T",$xmlDate);
    $xmlDate = $xmlDate[0];
    $xmlDate = date("n/j/y", strtotime($xmlDate));

   
    $ShortDate = date("n/j", strtotime($xmlDate));





    $xmlTimes = $row1->getElementsByTagName( "Time" );
    $xmlTime = $xmlTimes->item(0)->nodeValue;

    $xmlTime = strtotime($xmlTime);
   
    $xmlVenues = $row1->getElementsByTagName( "VenueID" );
    $xmlVenue = $xmlVenues->item(0)->nodeValue;

    	//Pull venue name
   	$paramGetVenueInfo = array('UserName' => $User, 'Password' => $Password, 
        'DomainName' => $Domain, 'VenueID' => $xmlVenue);

	$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");


    	$response = $client->GetVenueInfo($paramGetVenueInfo);

	$xmlDoc = new DOMDocument();
	$xmlDoc->preserveWhiteSpace = false;
	$xmlDoc->formatOutput = true;
	$xmlDoc->loadXML($response->GetVenueInfoResult->any); 
	$datasetvenue = $xmlDoc->getElementsByTagName( "Table" );

	foreach( $datasetvenue as $rowvenue )
	{
	$xmlVenueNames = $rowvenue->getElementsByTagName( "Venue" );
    	$xmlVenueName = $xmlVenueNames->item(0)->nodeValue;
	
		$pos = strpos($xmlVenueName, '(');
		if ($pos == true) {
		$xmlVenueNameExplode = explode("(", $xmlVenueName);
		$xmlVenueName = $xmlVenueNameExplode[0];
		}

	}




   	   $result[] = array("event"=>$xmlEvent, "date"=> $xmlDate, "sortKey"=>$stamp);




	


}


function cmp($a, $b)
{
    if ($a["sortKey"] == $b["sortKey"]) {
        return 0;
    }
    return ($a["sortKey"] < $b["sortKey"]) ? -1 : 1;
}

usort( $result,"cmp");
print_r($result);

Open in new window

0
 
hieloCommented:
try the attached code. It that doesn't work, post a sample of your xml
<?php
//Get Geo Events
$User = "";
$Password = "";
$Domain = "";


$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");

$result=array();

//Get two weeks out
$Start = date('Y-m-d');
$End = date("Y-m-d", strtotime(date("Y-m-d", strtotime($Start)) . " +1 week"));

$paramSearchEvents = array(	'UserName' => $User
						, 'Password' => $Password
						,'DomainName' => $Domain
						,'Event' => ''
						,'Venue' => ''
						,'StartDate' => $Start
						,'EndDate' => $End
						,'City' => $UserCity
						,'State' => $UserState
						,'bWithTickets' => 'false'
						, 'bVerbose' => 'true'
					);

$searchEventsresponse = $client->SearchEvents($paramSearchEvents); 

//var_dump($searchEventsresponse);

$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->loadXML($searchEventsresponse->SearchEventsResult->any); 

/*
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
    print $item->nodeName . " = " . $item->nodeValue . "<br />";
}
*/

$dataset = $xmlDoc->getElementsByTagName( "Table" );

foreach( $dataset as $row)
{
	if ($intCount++ < 20) //Limit to 20 events.
	{
		$xmlEventIDs = $row->getElementsByTagName( "EventID" );
		$xmlEventID = $xmlEventIDs->item(0)->nodeValue;

		//Second request gets event specific information.
		$paramGetEventInfo = array('UserName' => $User
								, 'Password' => $Password
								,'DomainName' => $Domain
								, 'EventID' => $xmlEventID
							);

		$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");
		$response = $client->GetEventInfo($paramGetEventInfo);
		$xmlDoc1 = new DOMDocument();
		$xmlDoc1->preserveWhiteSpace = false;
		$xmlDoc1->formatOutput = true;
		$xmlDoc1->loadXML($response->GetEventInfoResult->any); 

		$dataset1 = $xmlDoc1->getElementsByTagName( "Table" );
		foreach( $dataset1 as $row1 )
		{
			$xmlEvents = $row1->getElementsByTagName( "Event" );
			$xmlEvent = $xmlEvents->item(0)->nodeValue;
 
			$xmlDates = $row1->getElementsByTagName( "Date" );
			$xmlDate = $xmlDates->item(0)->nodeValue;
			$stamp=date("Y-m-d H:i:s", strtotime($xmlDate));

			$xmlDate = explode("T",$xmlDate);
			$xmlDate = $xmlDate[0];
			$xmlDate = date("n/j/y", strtotime($xmlDate));
			$ShortDate = date("n/j", strtotime($xmlDate));
			$xmlTimes = $row1->getElementsByTagName( "Time" );
			$xmlTime = $xmlTimes->item(0)->nodeValue;
			$xmlTime = strtotime($xmlTime);
   
			$xmlVenues = $row1->getElementsByTagName( "VenueID" );
			$xmlVenue = $xmlVenues->item(0)->nodeValue;

			//Pull venue name
			$paramGetVenueInfo = array('UserName' => $User
									, 'Password' => $Password
									,'DomainName' => $Domain
									, 'VenueID' => $xmlVenue
								);

			$client = new SoapClient("https://xmlfeed.eventprocess.com/Ticketsxml.asmx?WSDL");
			$response = $client->GetVenueInfo($paramGetVenueInfo);
			$xmlDoc = new DOMDocument();
			$xmlDoc->preserveWhiteSpace = false;
			$xmlDoc->formatOutput = true;
			$xmlDoc->loadXML($response->GetVenueInfoResult->any); 
			$datasetvenue = $xmlDoc->getElementsByTagName( "Table" );

			foreach( $datasetvenue as $rowvenue )
			{
				$xmlVenueNames = $rowvenue->getElementsByTagName( "Venue" );
				$xmlVenueName = $xmlVenueNames->item(0)->nodeValue;
        
				$pos = strpos($xmlVenueName, '(');
				if ($pos == true) 
				{
					$xmlVenueNameExplode = explode("(", $xmlVenueName);
					$xmlVenueName = $xmlVenueNameExplode[0];
				}
			}
			$result[] = array("event"=>$xmlEvent, "date"=> $xmlDate, "sortKey"=>$stamp);
		}
	}
}


function cmp($a, $b)
{
    if ($a["sortKey"] == $b["sortKey"])
    {
        return 0;
    }
    return ($a["sortKey"] < $b["sortKey"]) ? -1 : 1;
}

usort( $result,"cmp");
print_r($result);

Open in new window

0
 
JPERKS1985Author Commented:
Thanks for all your help I know it was a mess to work with!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now