0

I contact another website by get and get the result of a xml to interprete.

I contact like this:

   $v= file_get_contents(  
   "https://www.v.com/fechasexcursion.php? 
   agent=M&password=s&fecha1=2018-10-05&fecha2=2018-12- 
   31&idmodelo=$cifraexcursion");

Then I parse the information to get the dates from the xml to disable these dates in datepicker:

     $cifraexcursion = 9;
     $xml = new SimpleXMLElement($v);
     foreach ($xml->excursion as $tour) {   
        if ($nameoftour->idmodelo == $cifraexcursion) {
          echo "<br>Date ".$nameoftour->date;

Then I need to add these dates into a javascript var to use in datepicker. Below works perfectly, but I am not able to do with the array $nameoftour->date

     <script>
     var array = ["2018-10-14","2018-10-15","2018-10-16"];

     $('input').datepicker({
         beforeShowDay: function(date){
         var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
         return [ array.indexOf(string) == -1 ]
         }
      });
      (function() {
        $.datepicker.setDefaults({
          regional: 'en',
          buttonImage: "/images/calendar_blue.png",
          buttonImageOnly: true,
          firstDay: 1,
        }) 
      })();
     </script> 

Updated: This prints out 4 dates:

    echo "<br>Date ".$nameoftour->date;

However if I do this trying to save as javascript array it works but I only get the last date of the four dates.

    var array=["<?php echo ($nameoftour->date);?>",];

Updated: I got it working:

    $xml = new SimpleXMLElement($viajes);
    $tmpArrayFechas = [];
      foreach ($xml->excursion as $excursion) {
        if ($excursion->idmodelo == $cifraexcursion) {
        $tmpArrayFechas[] = $excursion->fecha;
        }
      }
     $tmpArrayFechas = implode(',', $tmpArrayFechas);
    ?>
    <input type="hidden" id="plazasDisponibles" 
     name="plazasDisponibles" 
     value="<?php echo $tmpArrayFechas; ?>">
    <script>

    var arrayDiasDisponibles = 
      document.querySelector("#plazasDisponibles").value.split(',');
    $('input').datepicker({
      beforeShowDay: function(date){
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      return [ arrayDiasDisponibles.indexOf(string) !== -1 ]
      }
    });

    (function() {
       $.datepicker.setDefaults({
         regional: 'en',
         buttonImage: "/images/calendar_blue.png",
         buttonImageOnly: true,
         firstDay: 1,
       })
     })();
    </script>
Helenp
  • 143
  • 2
  • 15
  • I use the informaton of the xml like this: $tour->date – Helenp Oct 05 '18 at 19:03
  • In order to get more help for this: edit your post and add the `php` tag so that PHP gurus see your question; also, I think it would be useful if you provide the PHP code that leads to your `$tour` variable. Finally, if you can, give insights on how you want to pass data from server's PHP to client's JS: do you write data directly into output HTML/JS file loaded with the page, do you have an endpoint to be reached with AJAX, do you have a socket stream or even a carrier pigeon you want to use, etc. – Stock Overflaw Oct 06 '18 at 08:25

2 Answers2

0

Since you seem to be using jQuery, you can use $.parseXML then you can wrap it in jQuery and .find('date'). Something like this should do:

var output = Array.from( // if you need an array (and not just iterable object)
  $( // wrap the XMLDocument in jQuery to parse it with .find
    $.parseXML(rawXmlData) // parse the XML into an XMLDocument
  )
  .find('date') // find the nodes we need
  .map(function (index, element) { // map the tags/nodes
    return $(element).text(); // retrieve only the content
  })
);
Stock Overflaw
  • 3,203
  • 1
  • 13
  • 14
0

Here is a raw js example of some code parsing xml into an array...

var myXml = "<root><tour><date>2018-10-14</date></tour><tour><date>2018-10-15</date></tour><tour><date>2018-10-18</date></tour></root>";
    
if (window.DOMParser)
{
    parser = new DOMParser();
    console.log(parser);
    xmlDoc = parser.parseFromString(myXml, "text/xml");    
}
else // Internet Explorer
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(myXml);
}

var elementsCollection = xmlDoc.getElementsByTagName("date");
var myArray = [];

for(var i = 0; i < elementsCollection.length; i++) {
  myArray.push(elementsCollection[i].innerHTML);  
};

for(i = 0; i < myArray.length; i++)
{
  console.log(myArray[i]);
  document.write(myArray[i] + "<br/>");
}

I found the following post very helpful: Parse XML using JavaScript

Also note that the dom parser will not do a very good job parsing your xml if you do not have a single root element thus you will notice in my example I added a element.

peter.edwards
  • 335
  • 1
  • 3