Jump to content

stevieontario

Members
  • Posts

    108
  • Joined

  • Last visited

Everything posted by stevieontario

  1. I never worry about that, I just use the built-in php function stc_tear_prevent()
  2. thanks to both of you -- well that would explain it. Now that puts two identical datetimes into my db (which is how I discovered the issue in the first place). The creator of the .csv cares not about DST -- they just give hourly data, hour 1 through hour 24. I guess I have to figure out something involving getOffset().
  3. probably a basic problem but I've been banging my head against it for an hour and a half. I'm trying to combine date and hour strings from a .csv file into a MySQL datetime variable. So 2 a.m. on March 13 2016 is proving a bit of a challenge: $date = '13-Mar-16'; $hour = 2; $startdate = $date.' '.$hour.':00:00'; $datehour = new Datetime($startdate); $datehour = $datehour->format('Y-m-d H:i:s'); echo 'datehour is '.$datehour; returns How did hour 02 become 03? Also, change '13-Mar-16' to '13-Mar-15' and the above code will return hour 02, i.e. the "proper" hour. Any idea what I'm doing wrong here?
  4. Dark Administrator (aka requinix ): Good grief. Can't believe I was approaching the problem like that -- scared of objects, exactly as you say. I had wondered why simplexml is called simplexml. It's simple. Yes, your first example was incredibly easy. Thanks!
  5. I want to use a custom function called object2array to turn an xml string into an array. The function works, but gives this warning: Here is the function with the call to get_object_vars: function object2array($object) { $return = NULL; if(is_array($object)) { foreach($object as $key => $value) $return[$key] = object2array($value); } else { $var = get_object_vars($object); if($var) { foreach($var as $key => $value) $return[$key] = object2array($value); } else return strval($object); } return $return; } Here is my xml string, which is in the file file.xml: <?xml version="1.0" encoding="UTF-8"?><resultset> <row> <name>Happy</name> <age>20</age> </row> <row> <name>Harry</name> <age>25</age> </row> </resultset> ... and here is where I call the function: foreach(glob("/path_to_file/*.xml") as $filename) { $xmlname = basename($filename); $xml_file = simplexml_load_file($filename) or die("no data loaded"); } $xml_file = object2array($xml_file); echo '<pre>'; print_r($xml_file); echo '</pre>'; It's confusing to me, because print_r($xml_file) says it is an object. Obviously it's an issue with the get_object_vars call in object2array, or with its scope in the function. But I can't figure it out. Any insight into why this is happening and how I could get the function to work without throwing the warning?
  6. good answer, thanks. I was hoping I wouldn't have to revisit the whole approach, but I think you've persuaded me to do that. You are right -- why put zillions of rows into a database when I can just store a small amount of data (i.e., the constants that define the $eggs_price and $bread_price) and apply them to date/hour. thanks for your time and advice.
  7. Morning freaks, I have pulled multiple rows out of a mysql db and now I'm in a while loop, trying to add a couple values to each result and insert those into another table. It's not working. My current code snippet looks like so: $sql1 = "SELECT date, hour FROM table1 where date = '$somedate'"; $result1 = mysqli_query($cxn, $sql1) or die ("sql1 transpo in test failed: " . mysqli_error($cxn)); while ($row1 = mysqli_fetch_assoc($result1)){ extract($row1); $weekday = date("w", strtotime($date)); if ($weekday ==0 ) { //create variables to add to hour/date variables, and insert each corresponding set of values into another table switch($hour){ case $hour =="01": $eggs_price = $eggs_cost*0.567*$sunday_hour_01/62; $bread_price = $bread_cost*0.567*$sunday_hour_01/62; break; case $hour =="02": $eggs_price = $eggs_cost*0.567*$sunday_hour_02/62; $bread_price = $bread_cost*0.567*$sunday_hour_02/62; break; case $hour =="03": $eggs_price = $eggs_cost*0.567*$sunday_hour_03/62; $bread_price = $bread_cost*0.567*$sunday_hour_03/62; break; case $hour =="04": $eggs_price = $eggs_cost*0.567*$sunday_hour_04/62; $bread_price = $bread_cost*0.567*$sunday_hour_04/62; break; /////etc., up to hour = 24///// } //////// put query results into food table $sql2 = "INSERT IGNORE into food (date, hour, eggs_price, bread_price) VALUES ('$date', '$hour', '$eggs_price', '$bread_price')"; $result2 = mysqli_multi_query($cxn, $sql2) or die ("sql2 failed, for this reason: " . mysqli_error($cxn)); echo "<pre>hour: "; echo $hour." ".$eggs_price; echo "</pre>"; } The echoes at the end output the proper number of rows for whatever date/time I put in, which I thought would ensure that the insert would iterate through the same loop in the same way. But the insert only puts one row (the first hour) into the food table, and that's it. What am I doing wrong?
×
×
  • 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.