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.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts

    writing or recording javascript output?

    i have borrowed a java script to produce a weather forecast using data from my weather station.

    //code to get your variables goes here
    //I've assumed they are:
    // $sager_wind = wind direction
    // $sager_rumbo = wind change
    // $sager_hpa = Baro value
    // $sager_trend = Baro trend
    // $sager_nubes = Sky condition

    //now we can prepare them for the Sager script

    //the sager script wants uppercase, so just to be sure...
    $sager_wind = strtoupper($sager_wind);

    //has to be uppercase
    $sager_rumbo = strtoupper($sager_rumbo);

    //allow for either of mB or Hg input
    if ($sager_hpa > 50) { $sager_hpa = $sager_hpa * 0.0295300;}

    //enumerate the Baro
    switch ($sager_hpa) {

    case ($sager_hpa > 30.4):
    $sager_hpa = 1;
    break;

    case ($sager_hpa > 30.1):
    $sager_hpa = 2;
    break;

    case ($sager_hpa > 29.9):
    $sager_hpa = 3;
    break;

    case ($sager_hpa > 29.7):
    $sager_hpa = 4;
    break;

    case ($sager_hpa > 29.5):
    $sager_hpa = 5;
    break;

    case (sager_hpa > 29.2):
    $sager_hpa = 6;
    break;

    case (sager_hpa > 28.:
    $sager_hpa = 7;
    break;

    case (sager_hpa > 28.0):
    $sager_hpa = 8;
    break;

    }


    //enumerate the Baro trend
    if (strcasecmp($sager_trend, 'Rising Rapidly') == 0) { $sager_trend = 1; }
    if (strcasecmp($sager_trend, 'Rising Slowly') == 0) { $sager_trend = 2; }
    if (strcasecmp($sager_trend, 'Steady') == 0) { $sager_trend = 3; }
    if (strcasecmp($sager_trend, 'Falling Slowly') == 0) { $sager_trend = 4; }
    if (strcasecmp($sager_trend, 'Falling Rapidly') == 0) { $sager_trend = 5; }

    //enumerate the Sky condition
    if (strcasecmp($sager_nubes, 'Clear') == 0) { $sager_nubes = 1; }
    if (strcasecmp($sager_nubes, 'Partly Cloudy') == 0) { $sager_nubes = 2; }
    if (strcasecmp($sager_nubes, 'Mostly Overcast') == 0) { $sager_nubes = 3; }
    if (strcasecmp($sager_nubes, 'Overcast') == 0) { $sager_nubes = 4; }
    if (strcasecmp($sager_nubes, 'Rain') == 0) { $sager_nubes = 5; }


    ?>

    <html>
    <!-- standard header stuff goes here -->
    <!-- and don't forget to include the sager script -->
    <script src="sager_cast.js" type="text/javascript"></script>
    <body>




    <br />
    Sager =
    <span id="forecast">forecast will appear here</span>





    <script type="text/javascript" language="javascript">
    <!--
    {

    //we could do some fancy escaping to directly insert the php variables
    //into the javascript function call, but this is harder to break.

    z_wind = "<?php echo $sager_wind?>";
    z_rumbo = "<?php echo $sager_rumbo?>";
    z_hpa = <?php echo $sager_hpa?>;
    z_trend = <?php echo $sager_trend?>;
    z_nubes = <?php echo $sager_nubes?>;
    document.getElementById('forecast').innerHTML = sager_cast( z_wind, z_rumbo, z_hpa, z_trend, z_nubes);
    }
    //-->
    </script>


    </body>
    </html>
    this basically turns my PHP variables intoa forecast by using the script above and also sager_cast.js (which crunches the numbers).

    my question is how can i record the output to a text file

    ie. this bit

    <br />
    Sager =
    <span id="forecast">forecast will appear here</span>
    i know how to run a cron job on my host server but whenever i try to use php to geta at it, it fails.

    i understand that php and JS and side server / client server / host server different, but surely there must be a way to record the output using my host and the script that i have.

    my goal is to catch the forecast every 15 mins (with a time stamp) and then look back at the (text) file to see how it has changed.

    can anyone please help?

    one suggestion is to turn the number crunching JS script into php so i can 'get at it' but there are thousands of lines of code!

    thanks in advance, and hello from a newbie poster!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    if you use IE and you have your security settings low enough, you can use something like this:

    Code:
    <html>
    <head>
    <script language="javascript">
    
    var files=0;
    //setInterval(WriteToFile,900000)
     
    function WriteToFile() {
    files++
    var date= new Date()
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile("C:\\Forecast "+files+".txt", true);
    s.WriteLine(date);
    s.Close();
    }
    </script>
    </head>
    <body onLoad="WriteToFile()">
    
    </body>
    </html>
    I don't know if there's a cross-browser method, but the above will create a new file (Forecast_1, Forecast_2, etc) every 15 minutes on your C drive - I'm guessing that if you want to change any of those things it's obvious enough how to do so
    Last edited by xelawho; 12-30-2011 at 08:58 PM. Reason: added date stamp

  • Users who have thanked xelawho for this post:

    muppet77 (12-30-2011)

  • #3
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    thanks for the rapid help!

    i didn't perhaps explain clearly enough that I'd like a forecast to be 'grabbed' 24/7 and not to my machine (which won't run 24/7).

    I'd like a cron job to launch a script from my host server (GoDaddy) that somehow records the forecast at the point the script is run.

    in PHP I would use fwrite commands to write PHP variables to a text file on my host server.

    I don't know how to do this as my final output is in JS.

    Hope that is clearer.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    GoDaddy won't let you run scheduled tasks unless you have at least semi-dedicated hosting.

    So you would have to invoke the code on GoDaddy from seme other computer. Which might mean that your home computer needs to run 24/7 just to hit GoDaddy every so often to do the updates.

    Having said the above... If you are running on GoDaddy Linux, possibly they do allow CRON jobs. I doubt it, but at least you could ask them.

    Having said that...

    Why do you *need* to run 24/7 unless your web site is actually hit 24/7?

    Why not simply record the time of the last update and, if a user hits your site and it has been more than an hour since the last update, you go and perform an update right then. The only "trick" to this will be ensuring that two users hitting your site at the same time don't both trigger an update.
    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.

  • Users who have thanked Old Pedant for this post:

    muppet77 (12-30-2011)

  • #5
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yes i am using GoDaddy LInux and yes they DO allow cron jobs - i use them all the time.

    i want to see how the forecast changes as my weather information changes so i'd like a regular forecast record to match the regulular weather variable record.

    my issue is how do i save the forecast from JS?

  • #6
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    the forecast is in theory automatically updated every 1 minute - it is generated by the console and sent to the pc.

    this is why i am keen to see it changing with the changing conditions - it's not as if it is run every 3hrs or so - it's minute by minute.

    this will give me an insight into which factors from my weather station control it more than others.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    You don't save from JS.

    Oh, you could save some data on the user's machine, using either cookies or local data, but you can't save the data so it is visible to all users using JS.

    To do that, you must use some kind of server-side coding. Since you are using Linux, that almost surely means you will need to use PHP to do this.

    But you said "whenever i try to use php to geta at it, it fails". In what way does it fail?

    It looks to me like you are saying that the only thing that fails, using PHP alone, is the call toe sager_cast( ). Is that right? Is so, that makes sense, since sager_cast() is JS code.

    I suppose what you could do is perform the number crunching with JS and then use an AJAX call--or even a simple <form> post--to ask PHP to save the results?

    Ugh...but that means you have to have JS running 24/7, as you said.

    How hard would it be to read the JS code for sager_cast() and convert it to PHP??
    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.

  • #8
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    it fails as it saves the actual script code itself rather than the output.

    the js code to convert is many many if statements.

    see it at

    www.maidenerleghweather.com/sager_cast.js

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Quote Originally Posted by muppet77 View Post
    it fails as it saves the actual script code itself rather than the output.
    My head hurts. I have no idea why it would do that. But on the other hand, I have no idea how you could get PHP to execute JavaScript code.

    It still looks to me like the answer is to use a browser to do the calculations and then send the results back to a PHP page to have it save them in a database or file.
    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.

  • #10
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I used the php command file_get_contents

    http://www.php.net/manual/en/functio...t-contents.php

    in a php script to grab the java output, but as i said it grabbed the entire code rather than the output.

    Any other ideas? I can see that dead end fast approaching....

  • #11
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You aren't understanding what he is telling you. Doing a file_get_contents on the JS file will not get you what you want. He said you need to use AJAX to post back to PHP the output of the JS call to sager_cast(). Don't do what post #2 said. There is no cross browser method. Again as stated you now have the problem of multiple users accessing the forecast at the same time. This isn't really an issue but you can have time stamps. The issue though is how often you will be doing the AJAX call back to your php script. Doing it every minute will take a lot of processing power from your server and slow down your load times quite a bit. Once an hour on a cron job is doable but PHP can't parse javascript. So what I would do is do the AJAX call back to PHP whenever a user accesses the weather report. Have your PHP check to see if the last file created is older than an hour. If it is, then create a new file, if not then don't do anything. In your AJAX call you will send post data back to your PHP processing script. This post data will be the output of your sager_cast() call e.g.
    Code:
    weather_output = sager_cast( z_wind, z_rumbo, z_hpa, z_trend, z_nubes);
    // do your AJAX post here back to a php processing script.
    document.getElementById('forecast').innerHTML = weather_output;
    I don't know how much experience you have with PHP but this isn't going to be easy for a beginner. You may want to consider hiring a professional who can do this.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    muppet77 (12-31-2011)

  • #12
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    you're right - i have no idea what to do now.

    many thanks.

    i have just got to grips with the basics of php, so taking on ajax myself may be tricky.


  •  

    Posting Permissions

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