2

First, I'm not experienced with PHP whatsoever. I have 2 arrays - minimumpressure and maximumpressure. They both access data from a database and to create a simple xml file as you can see below:-

<pressuremaxhourly>1010,1009,1009,1009,1008,1008,1007,1005,1004,1003,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1001,1001,1001</pressuremaxhourly>
<pressureminhourly>1001,1009,1009,1008,1008,1006,1005,1004,1003,1002,1002,1002,1002,1002,1002,1002,1002,1002,1002,1001,1001,1001,1001,1001</pressureminhourly>

The problem is, as you can see, the 1st value of the minimum array is always equal to the last value of the maximum array ( in this case "1001"). These arrays are overwritten every half hour or so with new values, yet the the problem still exists. Here's the code I use:-

$MaxPressure="select hour(datetime) AS hour, max(BarometricPressure) as MAXBP from minute WHERE DATETIME
BETWEEN (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)
AND ((CURDATE() + INTERVAL (SELECT hour(NOW()))hour))
group by hour
order by (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)";
$MaxPressureResult = mysql_query($MaxPressure) or  die('Failed to query'.mysql_error());

while ($row = mysql_fetch_object($MaxPressureResult)) {
    $maxpressure[]=$row->MAXBP;
}

$MinimumPressure="select hour(datetime) AS hour, min(BarometricPressure) as MINBP from minute WHERE DATETIME
BETWEEN (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)
AND ((CURDATE() + INTERVAL (SELECT hour(NOW()))hour))
group by hour
order by (CURDATE() + INTERVAL (SELECT hour(NOW())) hour - INTERVAL 24 hour)";
$MinimumPressureResult = mysql_query($MinimumPressure) or  die('Failed to query'.mysql_error());


while ($minrow = mysql_fetch_object($MinimumPressureResult)) {
    $minimumpressure[]=$minrow->MINBP;
}

And here's the code used to create the xml file:-

$dom = new DOMDocument(); 
$root = $dom->createElement( "weatherdata" );
$dom->appendChild( $root );  
$item = $dom->createElement( 'item', "" );  
$root->appendChild($item);

$maxpressure = $dom->createElement( 'pressuremaxhourly', "$maxpressure[0],$maxpressure[1],$maxpressure[2],$maxpressure[3],$maxpressure[4], etc....for all values );  
$item ->appendChild($maxpressure);
$minimumpressure = $dom->createElement( 'pressureminhourly', "$minimumpressure[0],$minimumpressure[1],$minimumpressure[2],$minimumpressure[3],$minimumpressure[4], etc....for all values );  
$item ->appendChild($minimumpressure);
$dom->formatOutput = true;
$dom->save('Hourly.xml') 

Note: I've checked the data in the database and it's correct, so it's clearly the script that's the problem. Sorry for the long question and code sample, hopefully someone can help.

#####$$$$$ Update $$$$$$####### It seems the SQL script isn't the problem. I've left the interval at 24, and when I run the SQL script in my SQL workbench it's loading the correct results.

However when it's run from the script above, the first element is always equal to the last element. IE. (minpressure[0] == minpressure[23])

Any further thoughts?

10
  • The SQL's are returning the correct results and the arrays contain the correct elements right? Can you check it? Commented May 7, 2012 at 20:59
  • Ok I've run the SQL's and they're returning the same result as the above, so it's obviously the SQL script... Again, not very experienced with SQL, but I've a feeling "hour - INTERVAL 24 hour" should be 23 instead? Commented May 7, 2012 at 21:29
  • 1
    Yes, since you are using BETWEEN, it'll include the first and last boundaries too. Try and check the results. Commented May 7, 2012 at 21:42
  • See update above. Basically, the script runs fine with the correct results in my SQL workbench, but not from the script... Commented May 8, 2012 at 12:12
  • Did you use my code in my answer? Commented May 8, 2012 at 18:28

1 Answer 1

3

How about creating the xml file like this :

$dom = new DOMDocument(); 
$root = $dom->createElement( "weatherdata" );
$dom->appendChild( $root );  
$item = $dom->createElement( 'item', "" );  
$root->appendChild($item);

$maxpressure = $dom->createElement( 'pressuremaxhourly', implode(",",$maxpressure) );  
$item ->appendChild($maxpressure);
$minimumpressure = $dom->createElement( 'pressureminhourly', implode(",",$minpressure));  
$item ->appendChild($minimumpressure);
$dom->formatOutput = true;
$dom->save('Hourly.xml') 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm using the above to create the xml file now as it's not as long, but it still doesn't help my problem. I've updated my question, could you have a look? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.