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++) {
$ret_val += pow(2,$i);
}
return $ret_val;
}
/* $starttime as unix-timestamp
$endtime as unix-timestamp
$times as array(array($start,$end),array($start,$end),....)
*/
function times2table ( $starttime , $endtime, $times) {
$days = array();
$startday = intval($starttime / 86400);
$endday = intval(($endtime ) / 86400);
$numdays = $endday - $startday;
for ($i=0;$i<$numdays;$i++){
array_push ($days,0);
}
/* check times */
foreach($times as $this) {
if ( $thisї0] < $starttime) {
$thisї0] = $starttime;
}
if ( $thisї1] > $endtime){
$thisї1] = $endtime;
}
$seg_startd = floor($thisї0]/86400);
$seg_endd = floor($thisї1]/86400);
for ($j=$seg_startd; $j<=$seg_endd ;$j++) {
/* get the end hour of the day */
if ($j==$seg_endd) {
$seg_endt = $thisї1];
} else {
$seg_endt = (($j ) * 86400) + 86399;
}
/* get the begin hour of the day */
if ($j>$seg_startd) {
$seg_startt = $j * 86400;
} else {
$seg_startt = $thisї0];
}
$daysї$j-$startday] = seg_val($seg_startt, $seg_endt);
}
}
return $days;
}
function do_time_scene($r, $i){
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) {
$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ї0])."' and tila_varaus_rivit.loppuu_aika < '".date("Y-m-d", $intervalї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){
// Construct an array that contains all the times...
$row = 0;
while ($data = @pg_fetch_array($scene_result, $row, PGSQL_ASSOC)) {
$joku = array( $dataї"alkaa_aika"],$dataї"loppuu_aika"]);
array_push($times,$joku);
$row++;
}// while ends
foreach(times2table ($intervalї0] , $intervalї1], $times) as $hour){
print ("<td>");
do_image( decbin($hour) );
print ("</td>");
}
}else{//scene_result testing if's else
alya_error("__ERR_DATB_READ");
}
print ("</tr>");
//foreach ends
}
print("</table>");
}//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){
$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++){
if( substr ($base, $i, 1) == 1){
imagesetpixel($picture, 1, $i , $white);
}
}
header("Content-type: image/png");
imagepng ($picture);
print("<BR>");
} //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