Day: January 24, 2018

How to read child elements of XML using php?

If you want to get child elements from xml using php then you can use this code which help you to get child elements and some other options. This is simple demo code which help you. In this code first get child elements data then also child of child data. Just parse data using php simplexml_load_string function.

<?php
$string = <<<XML
<ORDERSTATUS>
    <DATE>23/01/18</DATE>
    <INVOICE>Test Data</INVOICE>
    <INVTOTAL Tax="25.00" Freight="28" SubTotal="80.00">100.0</INVTOTAL>
</ORDERSTATUS>
XML;

$xml = simplexml_load_string($string);

echo "DATE:".$xml->DATE."<br>";
echo "INVOICE:".$xml->INVOICE ."<br>";
echo "INVTOTAL:".$xml->INVTOTAL ."<br>";
echo  "Tax:".$xml->INVTOTAL->attributes()->Tax."<br>";
echo  "Freight:".$xml->INVTOTAL->attributes()->Freight."<br>";
echo  "SubTotal:".$xml->INVTOTAL->attributes()->SubTotal."<br>";

this code give you first child elements then also child of child elements . This is output this code

DATE:23/01/18
INVOICE:Test Data
INVTOTAL:100.0
Tax:25.00
Freight:28
SubTotal:80.00

If you face any problem then please inform me. I will try to help you.

Comment

how to set class to selected “li” dynamically using jQuery?

If you want to add class in click li and remove all other class from all li then you can use simple jQuery code. Sometime we need to set active class for menu or other in this case this code is very helpful. This is simple code which set new class and remove all class from li.

$(document).ready(function(){
        $('.list li').click(function() {
             $( '.list li' ).removeClass( "active" ); // remove active class from all li 
            $(this).addClass('active'); // add active class for click li
        });
    });

then this is html code

active {
color:red;
}
   li {
   cursor: pointer;
  }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list">
            <li>Test 1</li>
            <li>Test 2</li>
            <li>Test 3</li>
            <li>Test 4</li>
            <li>Test 5</li>
            <li>Test 6 </li>
            <li>Test 7</li>
        </ul>

When you click any li then remove all class and add active class , i also add a css which color is red then easily can understand it.
If you face any problem then please inform me. I will try to help you.

Comment