Jump to content

stevieontario

Members
  • Posts

    108
  • Joined

  • Last visited

Posts posted by stevieontario

  1. 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().

  2. 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

     

     

    datehour is 2016-03-13 03:00:00

     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?

  3. I want to use a custom function called object2array to turn an xml string into an array. The function works, but gives this warning:

     

    Warning: get_object_vars() expects parameter 1 to be object, string given in <program.php>.

     

    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?

  4. 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.

  5. 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?

  6. 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...

  7. 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
  8. 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?

  9. 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.)

     

     

     

    Array
    (
    [dayname] => day1
    [hour] => 1
    [widtype] => type1
    [Output] => 20
    )
    Array
    (
    [dayname] => day1
    [hour] => 2
    [widtype] => type1
    [Output] => 9
    )
    Array
    (
    [dayname] => day1
    [hour] => 1
    [widtype] => type2
    [Output] => 450
    )
    Array
    (
    [dayname] => day1
    [hour] => 2
    [widtype] => type2
    [Output] => 650
    )

     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):

     

     

    Day Hour Type1_total Type2_total day1 1 20 450 day1 2 9 650

     

     

    ... 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:

     

    day1, 1, 20, ,
    day1, 2, 20, ,

     

    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!

  10. I put "localhost/ubuntu/myfolder" into the browser address bar, hit Enter, and got:

     

    Access forbidden!

    You don't have permission to access the requested object. It is either read-protected or not readable by the server.

    If you think this is a server error, please contact the webmaster.

    Error 403 localhost
    Apache/2.4.7 (Unix) OpenSSL/1.0.1f PHP/5.5.9 mod_perl/2.0.8-dev Perl/v5.16.3

    ... which is weird, since in the folder its permission is given as drwx.

     

    I contacted the webmaster, and he said he has no clue what's going on. (Sorry, it's been a long day and I thought I'd throw in a joke; the "webmaster" is me.)

  11. thanks everyone for your help. What a puzzle.

     

    trq, I own myfolder (at least I think I do)... I own the computer and aside from a user account I set up for my girlfriend I'm the only user.

     

    Jazzman -- I assume you mean "httpd-userdir.conf"? There are two of them in my system for some reason. (The file "httpd.conf" refers to this file in the userdir section.)

     

    But neither indicated that UserDir has been disabled.

  12. trq, you are right there seems to be no point for the foreach. That part of the script is a historical vestige of my original problem. The folder myfolder actually holds thirty or forty of xml files.

     

    Works fine in Ubuntu 11.10 with PHP 5.4.7. But on my Ubuntu 12.04 system with PHP 5.5.9 the foreach was not yielding any of the info I wanted.

     

    So I backtracked and tried file_exists() just to see if PHP was recognizing anything in that folder. I copied/pasted one of the files into the code just to be sure.

     

    mac, I did as suggested and put the ini_set code right after the opening tag and got the same result.

  13. well, I copied the file name (first from the Ubuntu folder and then from the Linux terminal, just to be sure) and pasted it into the code.

     

    Neither works. It recognizes the path, but doesn't see the files inside.

     

    Should have mentioned before, in case it matters: the sandbox in which the script runs fine is installed in Ubuntu 11.10; the wonky one is in Ubuntu 12.04.

  14. I wondered about that, and tried on the basis of searches for fixes to similar problems to use

    eio_chmod('/home/ubuntu/public_html/myfolder/', 0777);
    

    which returned "Fatal error: Call to undefined function eio_chmod()".

     

    Tried

    chmod('/home/ubuntu/public_html/myfolder/', 0777);
    

    which returned "The file [$filemoniker] does not exist."

  15. Morning Freaks,

     

    Wierd little problem -- I have set up xampp sandboxes on two different computers. On one, the following script finds an xml file inside a folder and var_dump() outputs info about the file. On the other, nothing.

    <?php
    include "Functions1.php";
    include "Functions2.php";
    
    $filemoniker = '/home/ubuntu/public_html/myfolder/xmlfile.xml';
    if (file_exists($filemoniker))
    {echo "The file $filemoniker exists";} else {echo "The file $filemoniker does not exist";}
     foreach(glob('/home/ubuntu/public_html/myfolder/xmlfile.xml') as $filename) 
     {
     	 	$xmlname = basename($filename);
    	echo "<pre>"; 
     	echo $xmlname;
     	echo "</pre>";
     $xml_file = simplexml_load_file($filename) or die("no data loaded");
     $rr = object2array($xml_file);
      print "<pre>";
      var_dump($rr);
     print "</pre>";
    }
    ?>
    

    As you can see, I wrote in a test script on lines 5-7. On one sandbox -- php version 5.4.7 -- that script echoes "The file [$filemoniker] exists."

     

    In the second sandbox -- php v. 5.5.9 -- it echoes "The file [$filemoniker] does not exist."

     

    I have checked the paths and the folder myfolder, and the file does exist. Needless to say, the rest of the script from foreach down returns nothing but a blank screen.

     

    Any ideas?

×
×
  • 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.