Jump to content

Recommended Posts

Hi there,

 

I just got a project to work, and want to know where I could have improved the code. Thanks for taking a look:

 

This is a Cron every hour and sets up the database:

<?php

// displays all the file nodes
$xml=simplexml_load_file('http://www.weather.gov/xml/current_obs/KBOS.xml');
if ($xml) {
//sets up all the variables I need for the SQL addendum
$weather = $xml->weather;
$temp = $xml->temp_f;
$wind = $xml->wind_mph;
$date = $xml->observation_time;
}
//error catching
else {
echo "Oops, bad input";
}
$db = mysql_connect('blah, blah, blah') or die(mysql_error());
mysql_select_db('table') or die(mysql_error());

mysql_query("INSERT INTO weatherdata (date, weatherstr, temp, wind) 
VALUES ('$date', '$weather', '$temp', '$wind')");
mysql_close($db);

?>

 

And the next part fetches the data I want and makes it as 45, 48, 59, 64.... for the last 35 values.

<?php
//open connection
$db = mysql_connect(blah, blah, blah) or die(mysql_error());
mysql_select_db('tables') or die(mysql_error());

$result = mysql_query("SELECT * FROM weatherdata ORDER BY id DESC LIMIT 35");

$row = (mysql_fetch_array($result));

while($row = (mysql_fetch_array($result))){
$output[] = $row['temp'];
}

$reverse = (array_reverse($output));
$csv = implode(", ", $reverse);
?>

 

I want to know if this leaves memory open, if I could have done this more elegantly, or anything like that. I have the FUNCTION down, now I'm worried about learning good FORM.

 

Thanks!

 

-Alex

 

Link to comment
https://forums.phpfreaks.com/topic/149548-efficient-coding/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.