Mohamed Hassan
Mohamed Hassan

Reputation: 1619

Getting the attribute of the main element of the xml using php

Here is the code first

rates.xml

<?xml version="1.0" encoding="UTF-8"?>
<main>
     <unittype id="24944">
          <date value="2020-10-10">
               <baserate>45.50</baserate>
               <closed>0</closed>
               <unitsavailable>2</unitsavailable>
          </date>
     </unittype>
     <unittype id="24944">
          <date value="2020-10-11" value2="2020-10-13">
               <baserate>99</baserate>
               <minimumstay>2</minimumstay>
               <unitsavailable>9</unitsavailable>
          </date>
          <date from="2020-10-15" to="2020-10-20">
                <closed>1</closed>
          </date>
     </unittype>
     <unittype id="24946">
          <date value="2020-10-10">
               <unitsavailable>0</unitsavailable>
               <closed>1</closed>
          </date>
     </unittype>
</main>

and here is the index.php

<?php
$xml = simplexml_load_file("xml_files/rates.xml");
//$xml = new SimpleXMLElement($xml);
$room_id = 24944;
$date = "2020101";
$hotel = $xml -> xpath('/main/unittype[@id = "'.$room_id.'"]/date[number(translate(@value,\'-\',\'\')) >"' .$date.'" ]');
var_dump($hotel);

foreach($hotel as $room) {
    //var_dump($room);
    echo "<br /><br />";
    echo "The room with the ID: {$room->unittype} <br />";
    echo "Date: {$room[0]->attributes()['value']}<br />";
    echo "Room Price: {$room->baserate}<br />
    Room min stay: {$room->unitsavailable}<br />";
}
?>

now I am trying as you see to get to the id in the unittype with the room that was selected, as $room->unittype but of course it's not working so does anyone know how to solve this?

the output of the var_dump($hotel) is

array (size=2)
  0 => 
    object(SimpleXMLElement)[2]
      public '@attributes' => 
        array (size=1)
          'value' => string '2020-10-10' (length=10)
      public 'baserate' => string '45.50' (length=5)
      public 'closed' => string '0' (length=1)
      public 'unitsavailable' => string '2' (length=1)
  1 => 
    object(SimpleXMLElement)[3]
      public '@attributes' => 
        array (size=2)
          'value' => string '2020-10-11' (length=10)
          'value2' => string '2020-10-13' (length=10)
      public 'baserate' => string '99' (length=2)
      public 'minimumstay' => string '2' (length=1)
      public 'unitsavailable' => string '9' (length=1)

Upvotes: 0

Views: 131

Answers (2)

Mohamed Hassan
Mohamed Hassan

Reputation: 1619

I got it guys. Thanks alot and sorry if i bothered anyone, here is the solution just in case somebody else needed it

<?php
$xml = simplexml_load_file("xml_files/rates.xml");
//$xml = new SimpleXMLElement($xml);
$room_id = 24944;
$date = "2020101";
$hotel = $xml -> xpath('/main/unittype[@id = "'.$room_id.'"]/date[number(translate(@value,\'-\',\'\')) >"' .$date.'" ]');
var_dump($hotel);

foreach($hotel as $room) {
    //var_dump($room);
    echo "<br /><br />";
    $node_name = $room->getName();
    //echo $node_name."<br/><br/>";
    $parent = $room->xpath("parent::*");
    //print_r($parent);
    echo "The room with the ID: {$parent[0]->attributes()['id']} <br />";
    echo "Date: {$room[0]->attributes()['value']}<br />";
    echo "Room Price: {$room->baserate}<br />
    Room min stay: {$room->unitsavailable}<br />";
}
?>

Upvotes: 0

Branden S. Smith
Branden S. Smith

Reputation: 1161

$room->unittype->attributes()->id;

Reference

Upvotes: 1

Related Questions