I am working on a project in which to show booking in caledar form is required.I myself build the calendar.it is showning the booking correctly in choosen color .but when i see calendar next months it shows booked from date 1 to date 12 booked every month .the main part of the code is

<? 
                            $oquery="SELECT * FROM rt_user WHERE rt_user_type='O'"; 
                            $oresult=  mysql_query($oquery);
                            While($orow=  mysql_fetch_assoc($oresult))
                            {
                                $uquery="SELECT * FROM rt_unit WHERE rt_owner_id='".$orow['rt_user_id']."'";
                                $uresult=  mysql_query($uquery) or die(mysql_error());
                                $num_rows=  mysql_num_rows($uresult);
                                IF($num_rows>=1)
                                {



                                    echo '<tr>';




                                      $count=1;
                                                        $columns=$no_of_days+2;
                                                     while($count<=$columns)
                                                        {

                                                            if($count==1)
                                                            {
                                                                echo "<td style='border: 2px solid;' colspan='5' rowspan='".$num_rows."'>".$orow['rt_user_name']."</td>";
                                                            }

                                                            if($num_rows==1)
                                                            {
                                                                 while ($urow=  mysql_fetch_assoc($uresult))
                                                                    {
                                                                        $bquery="SELECT * FROM rt_booking WHERE rt_unit_id='".$urow['rt_unit_id']."'";
                                                                        $bresult=  mysql_query($bquery);
                                                                        $bnum_rows=  mysql_num_rows($bresult);
                                                                        if ($bnum_rows>=1)
                                                                        {

                                                                            $bcount=0;
                                                                            while ($brow=  mysql_fetch_assoc($bresult)){
                                                                                $year1=date('Y',$brow['rt_start_date']);
                                                                                $month1=date('m',$brow['rt_start_date']);
                                                                                $date1=date('d',$brow['rt_start_date']);
                                                                                $year2=date('Y',$brow['rt_end_date']);
                                                                                $month2=date('m',$brow['rt_end_date']);
                                                                                $date2=date('d',$brow['rt_end_date']);

                                                                                $bsd=array();
                                                                                $bed=array();

                                                                                if ($year1==$year2)
                                                                                {
                                                                                    if ($month1<=$month && $month2>=$month)
                                                                                    {
                                                                                        if ($month1<$month)
                                                                                        {
                                                                                            //$bsd[$bcount]=1;
                                                                                          array_push($bsd,'1_'.$brow['rt_booking_color']);
                                                                                         }
                                                                                            elseif ($month1=$month) {
                                                                                          // $bsd[$bcount]=$date1;
                                                                                         array_push($bsd,$date1.'_'.$brow['rt_booking_color']);
                                                                                        }
                                                                                        elseif ($month2>$month) {
                                                                                                  //$bed[$bcount]=31;                                                                                 
                                                                                            array_push($bed,'31');

                                                                                            }
                                                                                        elseif ($month2=$month) {
                                                                                            //$bed[$bcount]=$date2;
                                                                                           array_push($bed,$date2);
                                                                                        }

                                                                                    }


                                                                                }
                                                                                elseif($year1<$year2)
                                                                                {
                                                                                    if($year1==$year)
                                                                                    {
                                                                                        if($month1<$month)
                                                                                        {
                                                                                            //$bsd[$bcount]=1;
                                                                                           // $bed[$bcount]=31;
                                                                                            array_push($bsd,$bcount,'1_'.$brow['rt_booking_color']);
                                                                                           array_push($bed,$bcount,'31');

                                                                                        }
                                                                                        elseif($month1==$month)
                                                                                        {
                                                                                            //$bsd[$bcount]=$date1;
                                                                                            //$bed[$bcount]=31;
                                                                                           array_push($bsd,$date1.'_'.$brow['rt_booking_color']);
                                                                                           array_push($bed,'31');

                                                                                        }
                                                                                    }
                                                                                    elseif($year2=$year)
                                                                                    {
                                                                                       // $bsd[$bcount]=1;
                                                                                        array_push($bsd,'1_'.$brow['rt_booking_color']);
                                                                                        if ($month2>$month)
                                                                                        {
                                                                                            //$bed[$bcount]=31;
                                                                                        array_push($bed,'31'); 
                                                                                        }
                                                                                      elseif($month2=$month) {
                                                                                          //$bsd[$bcount]=1;
                                                                                         // $bed[$bcount]=$date2;
                                                                                          array_push($bsd,'1_'.$brow['rt_booking_color']);
                                                                                          array_push($bed,$date2);

                                                                                      }
                                                                                    }
                                                                                }
                                                                                elseif($year2>$year && $year1<$year){
                                                                                    //$bsd[$bcount]=1;
                                                                                    //$bed[$bcount]=31;
                                                                                    array_push($bsd,'1_'.$brow['rt_booking_color']);
                                                                                    array_push($bed,'31');
                                                                                }

                                                                            }  
                                                                        }

                                                                        SORT($bsd);
                                                                        SORT($bed);

                                                                     $column=$no_of_days+1;
                                                                     $tsc=1;
                                                                     $acount=0;
                                                                     if(isset($bed[0]))
                                                                     {
                                                                     list($bsd1,$cid1)=explode('_',$bsd[0]);
                                                                     $bsds=array($acount=>$bsd1); $cid=array($acount=>$cid1);
                                                                     }

                                                                     while ($tsc<$column) {

                                                                       if(isset($bed[$acount]))
                                                                       {
                                                                         if ($tsc>1)
                                                                         {
                                                                             if($bsds[$acount]<$tsc && $tsc<$bed[$acount])
                                                                             {
                                                                                 $color_id=$cid[$acount];

                                                                                 $ccquery="SELECT rt_color_code FROM rt_color WHERE rt_color_id='".$color_id."'";
                                                                                 ?>

<?                                                                               
                                                                                 $ccresult=  mysql_query($ccquery);
                                                                                 $ccrow=  mysql_fetch_assoc($ccresult);
                                                                                 $color=$ccrow['rt_color_code'];

                                                                             }
                                                                             elseif ($bsds[$acount]<$tsc && $tsc>$bed[$acount]) {
                                                                                  $acount++;
                                                                                  list($bsd1,$cid1)=explode('_',$bsd[$acount]);
                                                                     $bsds=array($acount=>$bsd1); $cid=array($acount=>$cid1);
                                                                                   if($bsds[$acount]<$tsc && $tsc<$bed[$acount])
                                                                                   {
                                                                                 $color_id=$cid[$acount];

                                                                                 $ccquery="SELECT rt_color_code FROM rt_color WHERE rt_color_id='".$color_id."'";
                                                                                 $ccresult=  mysql_query($ccquery);
                                                                                 $ccrow=  mysql_fetch_assoc($ccresult);
                                                                                 $color='';
                                                                                    }
                                                                         } 
                                                                         elseif ($bsds[$acount]>$tsc && $bed[$bcount]>$tsc) {
                                                                                 $color='';    
                                                                     } 
                                                                         }  
                                                                     }else
                                                                     {
                                                                         $color='';
                                                                     }

                                                                        if ($tsc==1) {
                                                                            echo "<td style='border: 2px solid;' colspan='5'>".$urow['rt_unit_name']."</td>";
                                                                           }
                                                                         else {
                                                                             $cdate=$tsc-1;
                                                                             echo "<td style='width:30px; border: 2px solid; background-color:#".$color.";'>".$cdate."</td>";
                                                                         }
                                                                        $tsc++;
                                                                     } 



                                                                    }
                                                            }

Recommended Answers

All 4 Replies

It has one more problem.i have booked a flat for january then it is showning booked in every january of any year.

$year,$month,$date is for current calendar month,and $year1,$month1 and $date1 is used for bookin starting date.and $year2,$month2,$date2 is used for booking end date.

if it's swapping month and day (sorry I skipped the code) then try looking at the format inside your db?

Member Avatar for diafol

I think this code is a lot more complicated than it needs to be.

Could you give use cases for your code?

Show your DB tables
Show a screenshot of expected (if possible) or a screenshot of what you're actually getting

The code is a bit difficult to follow, not least because it is extremely indented.

A calendar display routine could be placed into a class, where you could simplify it by creating methods and simple client calls. Just a thought.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.