Jump to content

stevieontario

Members
  • Posts

    108
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

stevieontario's Achievements

Member

Member (2/5)

0

Reputation

  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?
  8. psycho, thanks -- your suggestion is getting me close to the mark I think -- it's producing a results table similar in structure to what I need. But the totals are not working. If I just include the "sum(if(type =.... " and exclude the "sum(output)..." then it gives the right sums, same if I include the "sum(output)..." and exclude the "sum(if(type...." I gave a confusing list of tables, can't figure out this forum's formatting to show the tables clearly. Let me paste as pdfs, give me a minute...
  9. hmm.... the tables were nicely formatted when I posted this, but now (viewing on a different machine with a different browser) they appear to not be formatted at all. Let me try again. This is T1 and this is T2 and I need a result that looks like this: and the first query is SELECT filename, hour, SUM(IF(type='x', production, 0)) AS x_total, SUM(IF(type='y', production, 0)) AS y_total from T1 group by filename, hour and the second query is: SELECT filename, hour , SUM( production ) AS k_total, FROM T1 INNER JOIN T2 ON T2.unitname = T1.unitname WHERE T2.subtype = 'k' GROUP BY filename, hour
  10. Morning freaks, I have two tables, T1 and T2. I'm trying to perform two queries, both on T1, but the second one gets information from T2 using an inner join. I want to join these two queries to get a single set of results. The queries work individually, but I was wondering if I can combine them into a single query. This is T1: filename hour unitname type production cost filename1.xls 1 unit_A x 10 100 filename1.xls 2 unit_A x 10 101 filename1.xls 1 unit_B y 20 50 filename1.xls 2 unit_B y 15 50 filename1.xls 1 unit_C y 30 89 filename1.xls 2 unit_C y 15 88 filename1.xls 1 unit_D y 30 54 filename1.xls 2 unit_D y 15 88 ... and this is T2: unitname type subtype unit_A x h unit_B y h unit_C y k unit_D y k I want to query these tables so I get a result like this: filename hour x_total y_total k_total filename1.xls 1 10 80 60 filename1.xls 2 10 45 30 I have been trying to somehow combine two queries. The first query is SELECT filename, hour, SUM(IF(type='x', production, 0)) AS x_total, SUM(IF(type='y', production, 0)) AS y_total from T1 group by filename, hour ... and the second query is SELECT filename, hour , SUM( production ) AS k_total, FROM T1 INNER JOIN T2 ON T2.unitname = T1.unitname WHERE T2.subtype = 'k' GROUP BY filename, hour Each query works on its own, but I am just stumped over how to combine them into a single query. I have tried to join them but cannot get around the complication raised by the inner join in the second one. Any ideas?
  11. Psycho -- thanks, that worked perfectly. For some reason none of my attempts to solve this on the query side involved the if statement. thanks very much!
  12. whoops, my most recent reply was not the raw output, but what I WANT to be the raw output. So, what I want is this: ... and what I'm actually getting is this:
  13. sorry, that was a cut-and-paste from a spreadsheet which didn't work out. Here's the raw output: day1, 1, 20, 450 day2, 2, 9, 650
  14. hello freaks, I have a four-element array, which is the result of a mysql query. The array is like below. (This is of course just a snippet to show the structure; the hours of course go up to 24, then repeat; there are about ten days' worth of data.) I want to loop through this data and output each hour's data in a separate line, like below (I included the headings just for clarity): ... and can't seem to make this happen. Here's the code I've written: $prevday = ''; while ( $row = mysql_fetch_assoc($result)) { extract($row); $currentday = $row['dayname']; $currenthour = $row['hour']; $currentwid = $row['widtype']; while($currentday !== $prevday){ for($currenthour = 1; $currenthour <=24; $currenthour++){ if($row['widtype'] == 'type1'){ $type1_total = $row['Output'];} if($row['fuel'] == 'type2'){$type2_total = $row['Output'];} print "<pre>"; echo "$currentday, $currenthour, $type1_total, $type2_total"; print "</pre>"; } $prevday = $currentday; } } ... and here's what it outputs: Clearly I have written this loop wrong but have banged my head against it for a while and wonder if it's just not possible to do what I want. Any suggestions would be hugely appreciated!
×
×
  • 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.