Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Simple calculation script using XML data

    Thank you so much for stopping in on my first thread here. If you know javascript I imagine this will be very simple for you and may only require a line or two of code(I certainly hope). I have spent hours researching how to code this myself and feel confident javascript is the way to go but after several tutorials and videos I still do not understand how to pull XML data from an external site for use in a calculation on my own site. I would gladly buy you a beer($5 via paypal) if you are the first person that provides a solution.

    LET ME EXPLAIN

    Quite simply, I am attempting to use XML data from http://www.xmlcharts.com/cache/industrial-metals.php to calculate a value on my website.

    All I want to display is the scrap value of one aluminum can. I need to multiply the XML data for the latest spot of aluminum/lb against .029982.

    Thanks again for any help

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    As much as I love Beer
    I must say to you that
    browser security, (same
    origin policy) will prevent
    you from doing this in a
    simple manner.
    You could do this using
    your server to get the data
    then appending that to
    the page.

    http://en.wikipedia.org/wiki/Same-origin_policy
    Last edited by DaveyErwin; 12-31-2013 at 12:46 PM.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    Welcome to the forums infinitenothing. My daughter lived in Lancaster, PA. worked at Donnelley's. Working with xml is all ways easier in PHP. Would have given the entire answer in PHP, but kept getting wrong answer in the multiplication so did use JS to do the math and display results.
    PHP Code:
    <?php
    $xml
    =simplexml_load_file("http://www.xmlcharts.com/cache/industrial-metals.php");
    $i 0;
    foreach (
    $xml as $currency[]){
        if (
    $currency[$i]->attributes()->access == "zar"){
            if(
    $currency[$i]->price->attributes()->access == "aluminum"){
                
    $first $currency[$i]->price;
                echo 
    $currency[$i]->price;  //  So you see what we're working with
            
    }
        }
        
    $i++;
    }
    ?>

    <script type="text/javascript">
        var first = <?php echo $first?>;
        var next = .029982;
        alert(first*next);   // Don't use alert in your final page use a div.
    </script>
    When you said
    for the latest spot of aluminum
    I took it to mean <currency access="zar"> If not just change the zar.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    one easy and simple method is to use YQL:

    Code:
    <script>function yqlCB(data){
       alert( Number( data.query.results.prices
           .filter(function(price){return price.currency.access=='usd'})[0].currency.price
           .filter(function(price){return price.access=='aluminum'})[0].content ) * 0.029982 );
    }
    </script>
    <script src="http://query.yahooapis.com/v1/public/yql?q=select%20currency%20%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.xmlcharts.com%2Fcache%2Findustrial-metals.php'&format=json&callback=yqlCB"></script>
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    infinitenothing (01-05-2014)

  • #5
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Wow! Thanks so much sunfighter and rnd_me for the help. Now let me display the limit to my knowledge of both codes mentioned.

    Sunfighter, your coding seems to be exactly the kind of direction I need. Only problem is I do not know how to make use of the coding. Apparently I cannot plug this right in html(html&css are pretty much the limit of my coding experience). My best guess is that I create a separate php file. Any clues you could offer? That's neat about your daughter though. My brother-in-law works there now. Great company

    Rnd_me, your script provides a calculated total as a popin on my screen. I have learned a little about document.write. Would I add this into the script you provided to produce a written result for my visitor?

    BTW, I owe you both a beer either way, pm me so I can paypal you both. To anyone else offering replies to this question, these are the only two beers I am buying for this solution.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,570
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    The point--and solution by Sunfighter--is that you can *NOT* access a FILE on another server (domain, actually!) via JavaScript. So he is using PHP code to get the file contents, processes the XML in PHP, and then just dumps data into the HTML/JavaScript code.

    Another alternative to this is to use a "proxy server". That would be a very small PHP page you would run on your server that would go grab the contents from the "foreign" domain and serve them up AS IS so that they appear to be coming from your own domain.

    Finally, NEVER NEVER NEVER USE DOCUMENT.WRITE! Treat it as poison.

    Rnd_Me's script uses the YQL processor and TELLS that processor what to do with the data. To wit:&format=json&callback=yqlCB

    That is, you are telling YQL to return the results in JSON format and, when the data is ready, to call the function yqlCB. (You could use any name you want there, in place of yqlCB.) RndMe's use of alert( ) is only for illustrative purposes. You would normally, instead, use DOM methods to put the resultant value in place on your page. You can *NOT* use document.write() to do so.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    You all are so extremely helpful. Thanks for saving me so many future hours of brain racking search.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    you can replace my "alert" with "document.getElementById('outputdiv').innerHTML=", where outputdiv is the id of the element you want to contain the Al price for one can.

    with yql you get safety and simplicity in one go, but you probably get more flexibility doing it yourself in php and assuming the security responsibility yourself.
    Last edited by rnd me; 01-01-2014 at 03:14 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    I found what I was doing wrong with the multiplication. You can do the whole thing without JS. Make your .html file into a .php file:
    Code:
    <?php
    $xml=simplexml_load_file("http://www.xmlcharts.com/cache/industrial-metals.php");
    $i = 0;
    foreach ($xml as $currency[]){
        if ($currency[$i]->attributes()->access == "zar"){
    	        if($currency[$i]->price->attributes()->access == "aluminum"){
                            $first = $currency[$i]->price;
    			$alum_price = (float)$first * .029982;
                   }
    	}
    	$i++;
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <div id="outputdiv"><?php echo $alum_price; ?></div>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    infinitenothing (01-05-2014)

  • #10
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, despite everyone being so helpful I am lost in how to use either code on my site properly. It is a mediawiki php site similar to wordpress. The javascript produced the alert so I changed it to
    Code:
    <script>function yqlCB(data){
       document.getElementById("outputdiv").innerHTML=( Number( data.query.results.prices
           .filter(function(price){return price.currency.access=='usd'})[0].currency.price
           .filter(function(price){return price.access=='aluminum'})[0].content ) * 0.029982 );
    }
    </script>
    <script src="http://query.yahooapis.com/v1/public/yql?q=select%20currency%20%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.xmlcharts.com%2Fcache%2Findustrial-metals.php'&format=json&callback=yqlCB"></script>
    
    <div id="outputdiv"></div>
    I am also still clueless how to use the php html combo on my site. Please keep in mind the only coding I am versed in is html. I apologize for taking so much of everyone's time and have spent several more hours trying to figure this out myself so I could avoid doing so. Much coffee and many cigarettes later and I am still lost. Even a link to something that might guide me through this would be appreciated.
    Last edited by vinyl-junkie; 01-02-2014 at 03:03 AM. Reason: Corrected code tags

  • #11
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    put the output div above the script tag - that way the code will know where to put the results when they come in.

  • #12
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks so much xelawho. That solved that problem.

  • #13
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by infinitenothing View Post
    Thanks so much xelawho. That solved that problem.
    yay. it's nice to get something working after a long time. Sounds like you need to paypal yourself a beer or two after all that! glad you got something working...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #14
    New Coder
    Join Date
    Dec 2013
    Location
    Lancaster County, PA
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So now I am in the process of learning javascript that way I do not have so many questions. I would like to limit the resulting number to two decimals and add a "$" sign to the final output. Again, the code I'm starting with is
    Code:
    <script>function yqlCB(data){
       document.getElementById("outputdiv").innerHTML=( Number( data.query.results.prices
           .filter(function(price){return price.currency.access=='usd'})[0].currency.price
           .filter(function(price){return price.access=='aluminum'})[0].content ) * 0.029982 );
    }
    </script>
    <script src="http://query.yahooapis.com/v1/public/yql?q=select%20currency%20%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.xmlcharts.com%2Fcache%2Findustrial-metals.php'&format=json&callback=yqlCB"></script>
    
    <div id="outputdiv"></div>
    My best guess based on what I have learned is something like
    Code:
    yqlCB = ("$" + yqlCB);
       yglCB = yqlCB.toFixed(2);
    Admittedly I still have not learned how the function above works exactly so I am not sure what variable, if any I would assign this to, or where I would put the coding I am attempting to add.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,027
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Code:
    <div id="outputdiv"></div>
    
    <script type = "text/javascript">
    
    function yqlCB(data) {
       var result =( Number( data.query.results.prices
           .filter(function(price){return price.currency.access=='usd'})[0].currency.price
           .filter(function(price){return price.access=='aluminum'})[0].content ) * 0.029982 );
    result = result.toFixed(2);  // makes the value a string with 2 dp
    result = "$" + result;  // prepend the $ sign
    document.getElementById("outputdiv").innerHTML=result;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    infinitenothing (01-05-2014)


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •