Page 1 of 1

Gettin' it done

Posted: Wed Aug 07, 2002 2:45 am
by 9902468
Ok. i have about 10 000 entries on average in db, every entry has two timestamps, one for beginning time and one for stopping time. I have to draw images based on that info for given time interval. One picture reprisents one day, the picture is 24 pixels high (one for each hour) The idea is that you can see the usage of the item from the picture, ie. if item has been requested from 12.12.2002 14.00 (beginning time) to 12.12.2002 16.00 (stopping time) and one want's to see that items usage 12.12.2002 - 14.12.2002: I would have to draw 3 pictures, two that are empty (no item requests for 13th and 14th) and one where two pixel rows are drawn. (the 12th, specifically rows 14 and 15 should be drawn...) Boss said that this scene should cover anything from one week to one month (7 - 30 pictures) and several items (5 - 10 is a rough estimate) so this gives a top total 300 pictures in one page that should be drawn. (per page request) Is php up to it?

Any thoughts how to do this? (How it should be done)

I thought that I should do db search for every item, sort results, draw pictures day by day, and move to next item. That sounds easy, but the difference between the two timestamps can be anything from 10 minutes to one month.... so, am I forced to go throug all results of the item every time I draw next day? This part gets me confused...

If u have done this already, experiences are appreciated.

thx

-9902468

Posted: Wed Aug 07, 2002 8:41 am
by darkshine
some line of your code would be appreciate... and helpful for u :)

Posted: Wed Aug 07, 2002 9:32 am
by 9902468
I'll drop some code tomorrow... It's coming along pretty nicely. (I think)

Posted: Fri Aug 09, 2002 1:31 am
by 9902468
Here is the code Darkshine!!

Code: Select all

/* $start , $end as unix-timestamp */

        function seg_val($start, $end){
                $ret_val = 0;

                $starth = intval(($start % 86400) / 3600);
                $endh = intval(($end % 86400) / 3600);

                for ( $i=$starth;$i<=$endh;$i++) &#123;
                        $ret_val += pow(2,$i);

                &#125;
                return $ret_val;
        &#125;




    /* $starttime as unix-timestamp
   $endtime   as unix-timestamp
   $times as array(array($start,$end),array($start,$end),....)
*/


        function times2table ( $starttime , $endtime, $times) &#123;
                
                $days = array();

                $startday = intval($starttime / 86400);
                $endday = intval(($endtime ) / 86400);
                $numdays = $endday - $startday;
          
                for ($i=0;$i<$numdays;$i++)&#123;
                        array_push ($days,0);
                &#125;

                /* check times */
                foreach($times as $this) &#123;

                        if ( $this&#1111;0] < $starttime) &#123;
                             $this&#1111;0] = $starttime;
                             &#125;
                        if ( $this&#1111;1] > $endtime)&#123;
                              $this&#1111;1] = $endtime;
                          &#125;
                

                        $seg_startd = floor($this&#1111;0]/86400);
                        $seg_endd = floor($this&#1111;1]/86400);
                     
                        for ($j=$seg_startd; $j<=$seg_endd ;$j++) &#123;
                            

                                /* get the end hour of the day */
                                if ($j==$seg_endd) &#123;
                                        $seg_endt = $this&#1111;1];
                                &#125; else &#123;
                                        $seg_endt = (($j ) * 86400) + 86399;
                                &#125;

                                /* get the begin hour of the day */
                                if ($j>$seg_startd) &#123;
                                        $seg_startt = $j * 86400;
                                &#125; else &#123;
                                        $seg_startt = $this&#1111;0];
                                &#125;

                                $days&#1111;$j-$startday] = seg_val($seg_startt, $seg_endt);                            

                        &#125;
                &#125;
                return $days;
&#125;

 function do_time_scene($r, $i)&#123;

                require_once __DIR_INCLUDE."all_db_login.inc";
                require_once __DIR_INCLUDE."draw_img.inc";
                $interval = explode(",", $i);
                $rooms = explode(",", $r);


                //Begin the table
                print ("<table class="times">");

                //Loop through all selected rooms
                foreach ($rooms as $room) &#123;

                        $times = array();

                        print("<tr>");
                        //query all reservations in db

                        $scene_query = "select extract ('epoch' from tila_varaus_rivit.alkaa_aika) as alkaa_aika, extract('epoch' from tila_varaus_rivit.loppuu_aika) as loppuu_aika where tila_varaus_rivit.alkaa_aika > '".date("Y-m-d", $interval&#1111;0])."' and tila_varaus_rivit.loppuu_aika < '".date("Y-m-d", $interval&#1111;1])."' and tila_varaus_rivit.tila_id = '".$room."' order by tila_varaus_rivit.alkaa_aika asc" ;
                  
                        $scene_result = pg_query($all_link, $scene_query);

                        if($scene_result)&#123;

                                        // Construct an array that contains all the times...
                                        $row = 0;
                                        while ($data = @pg_fetch_array($scene_result, $row, PGSQL_ASSOC)) &#123;
                                 
                                                $joku = array(  $data&#1111;"alkaa_aika"],$data&#1111;"loppuu_aika"]);
                                                array_push($times,$joku);

                                        $row++;
                                        &#125;// while ends                                       

                                        foreach(times2table ($interval&#1111;0] , $interval&#1111;1], $times) as $hour)&#123;

                                       
                                              print ("<td>");
                                               do_image( decbin($hour) );
                                              print ("</td>");
                                        &#125;



                        &#125;else&#123;//scene_result testing if's else

                                alya_error("__ERR_DATB_READ");
                        &#125;

                        print ("</tr>");
                //foreach ends
                &#125;

        print("</table>");
        &#125;//function ends


OK Here is the code, (Sorry about the lack of comments) Basically function do_time_scene takes two unix timestamps (The time interval that user wants to display) as parameters separated with comma (,) and list of item id numbers. (The items user wants to display) Then, I search the db as many times as there are item id numbers, every search return 0 to _big_number_ time intervals, (time pairs, begin and end time) and the function times2table calculates how the images should be drawn. It returns array containing numbers (My bad english skills, I dont't know the word for that you know that base 2 thing first one is 1, second 2 third 4 etc...) that can be interpeted as a row of 0000 and ones. (with decbin) every string contains ones and zeros and this string is fed to do_image:

Code: Select all

function do_image ($data)&#123;


                $base = "000000000000000000000000";

                $base = substr_replace ( $base, $data, 24-strlen($data));

               // print $base."<BR>";

                $picture = imagecreate(1,24);

                $white = imagecolorallocate ($picture, 255, 255, 255);
                $black = imagecolorallocate ($picture, 0, 0, 0);
                imagefill ( $picture, 0, 0, $black);



                for ($i = 0; $i < 24; $i++)&#123;


                        if( substr ($base, $i, 1) == 1)&#123;

                                imagesetpixel($picture, 1, $i , $white);
                        &#125;

                &#125;

                header("Content-type: image/png");
                imagepng ($picture);
                print("<BR>");


        &#125; //function ends.
Here, the ones and zeros are substr_replaced into a string of 24 0's, so the outcome is a string reprisenting one day and it's every hour. 0 means the item is not requested and 1 yes. (of course) So everything is made item by item (for user row by row), and request by request. This is still very early, so do_time_scene doesn't yet print the actual dates in <th> </th>, etc.

Btw. Actually I can't get any pictures out at the moment because mozilla tells me that cannot open picture ... because it contains errors, so what am I doing wrong in do_image? Also if u see some mistakes feel free to share...

Thanks for reading.

-9902468