Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

and thanks too all people who helped me here ...AlbinAbel and jswolf19...
thanks a lot...

this is what i came up with and so far its working ...but i have a little thing to ask ...how i can check weekend ...
i have a table name weekends in database and it have two field one for day and type
day is from 1-7 representing mon-sun and other field type which can be set by admin for weekends whatever day admin likes to be as weekend ...

how i can check display as weekend which are set by admin...with background color

<pre lang="xml"><table >
<?php
while ($row = mysql_fetch_array($rest, MYSQL_NUM)) {
    $request_id      = $row[1];
    $leave_status    = $row[2];
    $leave_comments  = $row[3];
    $leave_demp      = $row[4];
    $leave_type_name = $row[5];
    $leave_color     = $row[6];
    $emp_dep         = $row[7];
    $dbD = date("d", strtotime($row[0]));
    $dbM = date("m", strtotime($row[0]));
    $dbY = date("Y", strtotime($row[0]));
    $dbuser = date("Y-m-d", mktime(0, 0, 0, $dbM, $dbD, $dbY));
    $userDates[$dbuser] = array('comments' => $leave_comments, 'status' => $leave_status,'color' => $leave_color,'empdep' => $emp_dep);
}
$cYear=2011;
$Months = array(1, 2, 3, 4,5,6,7,8,9,10,11,12);
$monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
foreach($Months as $M) { ?>
<tr>
<td width="18%" colspan="2"><strong><?php echo $monthNames[$M-1]; ?></strong></td>
<td width="12%" colspan="2"><strong><?php echo $userDates[$dbuser]['empdep'] ?></strong></td>
<?php
    $num = cal_days_in_month(CAL_GREGORIAN, $M, $cYear); // 31
    $arrydats = array_fill (1,$num,"");
    $i=0;
                foreach($arrydats as $d)
                {
                    $i++;
                    $dates = date("Y-m-d", mktime(0, 0, 0, $M,$i, $cYear));
                    $checkdate = strtotime($dates);
                                 if(isset($userDates[$dates]))
                                   {
                                    echo "<td  width='26' title='".$userDates[$dates]['comments']."' height='20px' bgcolor='#".$userDates[$dates]['color']."' align='center' valign='middle' >";
                                    if($userDates[$dates]['status'] == -1)
                                        { echo "R"; }
                                    elseif ($userDates[$dates]['status'] == 0 )
                                        { echo "C"; }
                                    elseif ($userDates[$dates]['status'] == 1 )
                                        { echo "PA"; }
                                    elseif ($userDates[$dates]['status'] == 2 )
                                        { echo "A"; }
                                    elseif ($userDates[$dates]['status'] == 3 )
                                        { echo "T"; }
                                    elseif ($userDates[$dates]['status'] == 4 )
                                        { echo "W"; }
                                    elseif ($userDates[$dates]['status'] == 5 )
                                        { echo "SH"; }
                                        }
                                    else {
                                            echo "<td width='26' height='20px' align='center' valign='middle' >".$i."</td>";
                                            }

                }
}
?>
</tr>
</tabl

e>


Posted
Updated 21-Feb-11 22:56pm
v7
Comments
vicky87 19-Feb-11 10:32am    
is there anyone how can help me ..here
Albin Abel 19-Feb-11 23:18pm    
Can you separate the business logic from the markup? It is a old style having a mixer like this. now the clarity is not very good or explain more what you need to do. Your question is about optimize those loops or any error? If error point that error specifically.

Also are you getting employee id inside the loop? if not why this query running again and again..

$sql ="SELECT leave_date,leave_status,leave_comments,leave_type_id,emp_depment FROM `hs_hr_leave` WHERE employee_id = $emplid ";

move this out of the loop. Always it is not better advice running queries inside the loop. Use joins, load the result in memory and use your business logic to handle records.

So two optimizations you need to do before optimizing the loops.

1) For clarity separate markup from business logic, otherwise you are going to struggle in many places finding out bugs

2) Do not keep on calling your database inside loops.

Once heard from you what exactly need to do here, I ll give you the answer.
vicky87 20-Feb-11 5:31am    
thx ...i am making a leave calender for whole year it check that dates from database and if found and leave of the employee background color will change .... the first FOR loop is for months and second is for numbers of DAYS in that month ...now now i have to match the days which are coming from database ....the problem is that $userDates only have one value (example 1-1-2011) it compare with all 365 days the loop stops and i only one got one record on the calender and there are about 15 records in database ...and i also have to make weekends....

plz plz plz plz help me out here i am trying my best ..to do it ..i am try everything i know ...but still cant solve it ...
jswolf19 20-Feb-11 8:27am    
You only get one date because you're only storing one date. If you want all 15 dates, then you need to store your dates in an array. Your while loop on your mysql_fetch_array stores its results into the same variable each time, thus overwriting the previous row's results.

This should be cleaned up a lot, but it should work for you. You seem to be selecting things from the database that you're not even using, among other things... I've removed some of the code to shorten the post some, so this won't work as a straight copy and paste job.

...
            while ($row = mysql_fetch_array($res, MYSQL_NUM))
        {
            //echo $time2=date("Y-n-j",strtotime ($row[0])).'<br/>';
            //here you have to seprate the year month and day coming from db and pas it to mktime function as doing above
            ///like $dates = date("Y-n-d", mktime(0, 0, 0, $Month, $i, $cYear));
                 $status   = $row[1];
                 $comments = $row[2];
                 $typeid   = $row[3];
                 $depment  = $row[4];
            $dbD = date("d", strtotime($row[0]));
            $dbM = date("m", strtotime($row[0]));
            $dbY = date("Y", strtotime($row[0]));
            $dbuser = date("Y-m-d", mktime(0, 0, 0, $dbM, $dbD, $dbY));
//            $userDates = strtotime($dbuser);
            $userDates[$dbuser] = array('comments' => $comments,
                                        'status' => $status);
            //echo $dbuser.'<br/>';
            //echo $dates.'<br/>';
        }

...

                    $dates = date("Y-m-d", mktime(0, 0, 0, $Month, $i, $cYear));
                    $checkdate = strtotime($dates);
                                //loop to print dates from database
                                //echo $checkdate ."==". $userDates.'<br/>';
//                                    if($checkdate == $userDates)
                               if(isset($userDates[$dates]))
                                   {

                                                    echo "<td  width='26' title='".$userDates[$dates]['comments']."' height='20px' bgcolor='#".$color[1]."' align='center' valign='middle' >";
                                    if($userDates[$dates]['status'] == -1)
                                        { echo "R"; }
                                    elseif ($userDates[$dates]['status'] == 0 )
                                        { echo "C"; }
                                    elseif ($userDates[$dates]['status'] == 1 )
                                        { echo "PA"; }
                                    elseif ($userDates[$dates]['status'] == 2 )
                                        { echo "A"; }
                                    elseif ($userDates[$dates]['status'] == 3 )
                                        { echo "T"; }
                                    elseif ($userDates[$dates]['status'] == 4 )
                                        { echo "W"; }
                                    elseif ($userDates[$dates]['status'] == 5 )
                                        { echo "SH"; }

...
 
Share this answer
 
Comments
vicky87 20-Feb-11 9:11am    
thx for your reply ......i have clean the code ...now ...
vicky87 20-Feb-11 9:19am    
i am so much confused here...i have three tables one for record leaves , second for leaves type third for weeks ...how i can check which day it is sat, sun mon all seven days ...
jswolf19 20-Feb-11 9:28am    
You can get the day of week using the same function you used to get the day, month, and year: http://www.php.net/manual/en/function.date.php
vicky87 20-Feb-11 9:59am    
Thanks for pointing that out ..i will try and let you know ...
Hi
I give the pesudocode steps here.

Take out these two queries out of all the loops

SQL
$sql ="SELECT leave_date,leave_status,leave_comments,leave_type_id,emp_depment FROM `hs_hr_leave` WHERE employee_id = $emplid ";
   $res = mysql_query($sql);

$cSql = "SELECT leave_type_name,leave_color FROM hs_hr_leavetype WHERE leave_type_id = '$typeid' 


Merge these queries in to a join query. You can see there is a common column typeid. Using that you can join that query. I ASSUME both these tables have one to one relationship as hs_hr_leavetype is just a list type, so one row in hs_hr_leave has only one leave type id.

Populate this result in a name value pair array employee_leaves<leave_date,>. This object is class having two members say leave status,leave_color.

Populate this array and keep in handy.

now go through your loop
<br />
for each month in that year<br />
    for each days in that month<br />
         Check is that date (m-d-y you can get here) is available in the employee_leaves array<br />
         if yes then <br />
               get the status and color from the employee_leaves array for the date in question and mark that day.<br />
         else<br />
              simply display the day<br />
         end the if<br />
    end month<br />
end year.<br />

Thats all simple thing, convert this pseudo code to PHP.
 
Share this answer
 
v2
Comments
vicky87 21-Feb-11 7:05am    
Thanks for the reply i will convert this to php and try ...before i do i just want to ask one thing ...
to fill array for months i have to take an array and put 12 months in it..but
for days can i use $days = array_fill(1, $numOfDays, ''); and foreach loop the display the dates.
Albin Abel 26-Feb-11 0:03am    
sure, you can :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900