Link to home
Create AccountLog in
Avatar of Bob Conklin
Bob ConklinFlag for United States of America

asked on

Debug PHP Script

Greetings...
I have a PHP script file that was written by some one else who no longer is around to ask how to fix the issue. I do not have a lot of experience, practical, or otherwise in decoding this type of scripting.

The basic process as I understand it, is to take data from a generated tide table, figure out the time to the next tide, either high or low, and display that on a web page with an appropriate graphic. The script has not worked correctly for me. Either it fails totally, or instead of accurately telling the time to the next tide, it will tell the time to the next tide 24 hours away.

I do know that the script does work on other websites, but I can't seem to be able to troubleshoot it in my instance.
Here is the code....(no private data is in this code snip)
the Variable $S3C2 is set in the configuration page, the value of 2 tells the displaying page to use the tide script.

<?php if (($S3C2 == 2) or ($S3C2 == 5)) {  //  Show Tide, Lightning will over-ride ?>
                        <?php
                        $tide = tide_data(0);
                        $tide1 = tide_data(1);
                        //print_r($tide);
                        //print_r($tide1);
                        $next_tides = array();
                        if (is_array($next_t1 = next_tide_info($tide[1], 0)))
                            $next_tides = $next_tides + $next_t1;
                        if (is_array($next_t2 = next_tide_info($tide[2], 0)))
                            $next_tides = $next_tides + $next_t2;
                        if (is_array($next_t3 = next_tide_info($tide[3], 0)))
                            $next_tides = $next_tides + $next_t3;
                        if (is_array($next_t4 = next_tide_info($tide[4], 0)))
                            $next_tides = $next_tides + $next_t4;
                        if (is_array($next_t5 = next_tide_info($tide[5], 0)))
                            $next_tides = $next_tides + $next_t5;
                        if (is_array($next_t1 = next_tide_info($tide1[1], 1)))
                            $next_tides = $next_tides + $next_t1;
                        if (is_array($next_t2 = next_tide_info($tide1[2], 1)))
                            $next_tides = $next_tides + $next_t2;
                        if (is_array($next_t3 = next_tide_info($tide1[3], 1)))
                            $next_tides = $next_tides + $next_t3;
                        if (is_array($next_t4 = next_tide_info($tide1[4], 1)))
                            $next_tides = $next_tides + $next_t4;
                        if (is_array($next_t5 = next_tide_info($tide1[5], 1)))
                            $next_tides = $next_tides + $next_t5;
                        // figure out wich tide is the next one
                        ksort($next_tides);
                        //print_r ($next_tides);
                        //print '<br>';
                        foreach ($next_tides as $k => $v) {
                            if ($k >= 0) {
                                $mins_to_next_tide = $k;
                                $tide_type = $v;
                                break;
                            }
                            }
                        echo "<tr><td style='text-align:center;'>";
                        $next_tide_time = date('h:i A T', strtotime("+$mins_to_next_tide minutes"));
                        echo "<p>" . time_till_tide($mins_to_next_tide * 60) . " to " .
                        $tide_type . " tide at<br />" . $next_tide_time . "</p>";
                        $mins_to_next_tide_rounded = round_tide_time($mins_to_next_tide);
                        $tide_image = select_tide_image($mins_to_next_tide_rounded, $tide_type);
                        echo "<img src=\"tides/$tide_image\" width=\"150\" alt=\"\"/><br />";
                        echo "</td></tr>";
                        //echo $nexttidequarter;
                        ?>
<?php } // end $useTide ----------------------------------------- ?>              

I am not sure what else in the way of information is needed to help debug this...
Thank you
-Bob
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey Bob,

Your script is calling quite a few user functions and without knowing what they do, what arguments they're supposed to take, and what data they return it's going to be very difficult to give you any useful information.

These are the functions that we know nothing about

tide_data()
next_tide_info()
time_till_tide()
round_tide_time()
select_tide_image()

Somewhere in your code these functions will be defined (maybe as part of another, included file). You'll need to post that code if you need further assistance.

Something that may get you started on your own is to turn on error reporting. Right at the very start of your script, add the following:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Open in new window

That may at least give you some useful information when things go wrong.
Avatar of Bob Conklin

ASKER

Hi Chris
I can supply the source of the data that the functions are looking for, the data is from a tide chart that is an html file. That file is generated at 02:00 each day and is placed in the default web directory on the web server.
I will insert the debug code you supplied tonight in hopes that at 02:00 tomorrow it will point me somewhere!
The first photo shows the end result on my web site. I have circled, in blue, the error I am trying to fix. At most the time shown should be only 6 hours apart. The second picture is the tide source data.

User generated image
User generated image
Thank you for the quick response.

-Bob
$next_tide_time = date('h:i A T', strtotime("+$mins_to_next_tide minutes"));

Open in new window

Where is $mins_to_next_tide calculated?

I am guessing this has something to do with time zones - you are probably doing a calculation for a destination that is 9 hours away from your server or something like that. If so you would need to correct for it by setting the timezone for your application
Either
DateTime::setTimezone
date_default_timezone_set
Hey Bob,

Having the data file would be a good start, but as I said, your code is using some user defined functions that work against that data, so we could need to see those to figure out what is going on. You've only posted part of your code, so somewhere in the parts that you haven't posted will be the code we need to see. It will probably be in form of an include(), include_once(), require() or require_once().
Hi Chris
I will post the files that comprise the components to make this work, I can post them as attachments. Within these files are all the settings that create the web site.
As there is a large east coast storm brewing, I did not activate the debug commands last night as I had planned.

Julian...
I have contacted the Web hosting company to verify the server time zone, but as all the rest of the time on the site appear to work, not saying that somewhere in these files I have missed some setting concerning times and offsets.


ajax-dashboard6.php

AltAjaxDashboardConfig6.php

tides_processor.php

Settings.php

Settings-weather.php

common.php


Thanks!!!
Thanks Bob,

Can you post the data file as well, so I can do some digging and testing
OOOPS.....sorry about that :)

tideprediction.html
Hey Bob,

Had a quick look at the files you've posted, and I've got to say, they're pretty atrocious! It's basically a huge jumble of spaghetti code.

I've isolated the core functions needed for your script to run, and it looks like the format of your tideprediction.html file does not match what the script is expecting in several ways. Unfortuntately, there's absolutely zero error checking in the scripts, and error_reporting is turned off, so it will just fail silently.

You have a couple of options really. You could check on how and where you get tideprediction.html file from and find out why the format is no what the script is expecting. You could go to the original authors of the script and ask them if they have a more up-to-date version of it, or you could rewritet the core function of your script in a more robust way. There are only 5 functions that would need to be re-worked, but it will depend on you knowing EXACTLY what format your tideprediction.html data is stored in.
Hi Chris
I was afraid of that, I have a bit of knowledge in scripting but when I dove into this mess I was totally lost. I have reached out to the original writer of the script but all my correspondence is getting bounced. Am guessing he has decided not to support this any longer. I have also posted help requests on the forums that are run by the folks that sell the core software, and they try to help but as this was a "custom" script they have washed their hands of it.
I have one other option, not one I want to pursue, and that is to abandon the tide function all together.
When you mention the format that the tidepredictionfile is in, I want to make sure I am clear on this, are you asking what the file format is as it is generated by the Tide creation program?


Julian....
According to the ISP, the time zone of the server that is hosting my web site and where these scripts run is the same TZ as my source computers, Eastern Standard Time.


Thank you!!

-Bob
Hey Bob,

When I talk about the format of the prediction file, I mean the actual data that's stored in it. Because this is essentially a flat text file, you would need to know exactly how the data is laid out in order to parse it into meaningful data.

As an example, one of the functions reads the tide time from the file. It then takes the first 2 characters as the hour and the last 2 characters as the minute. This would be fine if the time in the file was in 'military' format, such as 0417 (which it looks like it's expecting). However, in your prediction file, it's stored as 4:17. The program fails because it thinks the hour is 4: instead of 04.

The code also seems to assume that a tide block (info about a given day) will have 9 or 11 lines in it, where as in your file, each day has 6 or 7 lines (depending on the number of tides in a day). With no error checking in place it will fail.

I'm guessing that the code was written to parse a very specific layout of the prediction file and that layout has now changed.

If you have a very clear idea on the layout of the file, then parsing it wouldn't be particularly difficult.
Hi Chris
I see exactly what you are saying now thanks to your excellent description of what is going on.
What we have is an issue between two scripts.

The tides_processor.php script, which parses the data and places it into the tables seen in the second screen shot above,  will fail when the tide data is set to display in 24hour time. The author of that script has said he does not plan on writing an update to his script that will parse the data correctly to table format when it is presented in 24 hour time.

So I have the tide creation program set to display the data in 12 hour time with the day, date and month but no year, I am guessing that not displaying the year could contribute to the code block being shorter.

The script that you are looking at, as you have so kindly decoded, wants the time in 24 hour format.
Now the question is this, which script will be easier to tweak??

-Bob
At a guess, it would be easier to tweak the tides_processor script to work with 24 hour time. There's less code to tweak, and dealing with 24-hour time just makes more sense generally speaking.
Hi Chris
I agree, in the scripts I normally deal with, I like to edit the ones that make the most sense to get the job done.
I believe I uploaded that tides_processor.php script.


-Bob
I've just looked at the tides_processor script and there's nothing in there that deals with the time, so not sure what difference it makes whether you use 12 or 24 hours time. What problems are you experiencing when using it with 24 hours times.

It looks to me like the same problems exist in both scripts - they're both hard-coded to deal with a different format of prediction file.
Hi Chris
If I change the time format to 24 hour time, I either get no data being parsed to the tide tables or I will get the script returning the message "Data is being updated, please refresh the page"

Mostly I get blanks in the tables.

-Bob
Hmmm. Can you send me the file in 24 hours format. I'm slightly confused because the tides_processor doesn't appear to do anything with the times, so currently can't see why it would fail. Need to do some more digging, but I need the 24 hour format file you're using.
Hi Chris
I will remote into the system this morning and run the process to create a tide data page in 24 hour time, I will send you a copy of the data file and a screen shot of the output page.


-Bob
Thanks Bob
Hi Chris
Ok, You will think I have lost my mind, (actually I think I have), I reset the tide program to use 24 hour time. I could NOT break the tide chat page. Meaning that no matter how I tried the tides_processor script did exactly what it was supposed to do and populate the tide charts with data in 24 hour format. (In the 8 weeks we have been fighting this issue it has NEVER done that in the past)

User generated image
That was the "bad" news, the "good" news is changing to 24 hour time format has made the front page script behave correctly...

User generated image

Here are the 2 data files, first one is in 12 hour time and the second is in 24 hour format


12hr_tideprediction.html


24hr_tideprediction.html

Besides putting my head down on my desk and whimpering, I am at a loss as to why all of a sudden this mish-mash of code has started to work.

Chris....
would you be interested in being hired to re-write the 2 scripts to be more useful/proper/functional?

-Bob
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi Chris,
I do understand what you are saying in regards to programming and scripting. I can not sufficiently express my gratitude to you for your insight and assistance in making a bit of sense in tracking down the problems. Your explanations of what you found were very informative.

I checked the output of the scripts this morning and knock on wood, both pages are doing what they are supposed to do.

I appreciate you honesty in not being able to work on the scripts. Yes you are totally correct in that the AJAX Dashboard does need a serious rework, but for now it does the job albeit not in the most efficient manner :)

I guess I will go ahead and close the question and award the points to you.


Cheers Chris!!

Thank you again!!

-Bob
Thank you Chris!
No worries Bob. Glad it's all working for you.

And if you're feeling adventurous, why not have a go a re-working the scripts yourself. With a bit of help from Google and EE, you could probably start to understand exactly what's going on, and re-work it into a better solution - but start with processor - not the AJAX Dashboard :)

Good luck with it.