Hi,

I am currently using the follwing IF statement:

if ($daybooked == 1){
print sprintf('<td align="center"
bgcolor="%s">&nbsp;%d&nbsp;</td>',$opts,$day);
}
else{
print sprintf('<td align="center"><font color="#999999">&nbsp;%d&nbsp;</font></td>',$day);
}

I would like for this statement to loop. Any ideas?

Thanks,
Steve

Recommended Answers

All 7 Replies

to loop ? Which loop ? Put the whole thing in whatever loop you want.

<?php
for($i=0;$i<10;$i++){
if ($daybooked == 1){
print sprintf('<td align="center"
bgcolor="%s">&nbsp;%d&nbsp;</td>',$opts['today_color'],$day);
}
else{
print sprintf('<td align="center"><font color="#999999">&nbsp;%d&nbsp;</font></td>',$day);
}
} //this will loop 10 times.
....
?>

If thats not what you looking for, then please explain in detail.

Cheers,
Naveen

I think it will be better for me to explain everything to you. I am creating a page for a Manager to be able to see what members of staff have booked days off in calendar sort of format. The colour of the cell will be different if a staff member has booked the day off. At the moment it works until a staff member books more thn one holiday. If they have booked more than once it will only colour the days of the most rescent entry to the database. The code I have used is below:

<?php
function pc_calendar($month,$year,$opts = '') {


if (! isset($opts['month_link'])) {
$opts['month_link'] =
'<a href="Manager_calendar16.php?month=%d&year=%d">%s</a>';
}


list($prev_month,$prev_year) =
split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));
$prev_month_link =
sprintf($opts['month_link'],$prev_month,$prev_year,'&lt;');
list($next_month,$next_year) =
split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));
$next_month_link =
sprintf($opts['month_link'],$next_month,$next_year,'&gt;');
?>
<table width="50%" border="1" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF">
<tr>
<td align="left" colspan="7">
<?php print $prev_month_link ?>
</td>
<td align="center" colspan="16">
<?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year));
?>
</td>
<td align="right" colspan="6">
<?php print $next_month_link ?>
</td>
</tr>
<?php
$totaldays = date('t',mktime(0,0,0,$month,1,$year));
// print out days of the week
print "<tr>";
include("db_Conn.php");
$names = mysql_query("SELECT * FROM `staff` WHERE Manager_Num = '1'");



while ($row= mysql_fetch_array($names)) {

$staff_id = $row['Staff_ID'];
$f_name = $row['F_Name'];
$l_name = $row['L_Name'];
print "<td>$f_name $l_name</td>";



for ($day = 1; $day <= $totaldays; $day++) {
$daybooked = 0;

$sql= mysql_query("SELECT * FROM `Leave` where Staff_ID = $staff_id");
$numrows=mysql_num_rows($sql);

while ($row2= mysql_fetch_array($sql)) {
$leave_id = $row2['Staff_ID'];
$leave_date = $row2['Leave_Date'];
$return_date = $row2['Return_Date'];

$first_date = strtotime($leave_date);
$last_date = strtotime($return_date);

$new_date = date('d',$first_date);
$new_month = date('m',$first_date);
$new_year = date('Y',$first_date);

$ret_date = date('d',$last_date);
$ret_month = date('m',$last_date);
$ret_year = date('Y',$last_date);

$day_highlight = (($new_month == $month) && ($new_year == $year));
$ret_highlight = (($ret_month == $month) && ($ret_year == $year));


if (($day_highlight && ($day < $new_date)) || ($ret_highlight && ($day >= $ret_date)) || !($ret_highlight) && !($day_highlight)) {
$daybooked = 0;}
else{
$daybooked = 1;
}
echo $daybooked;}


      
      if ($daybooked == 1){
	  $mycol = '#FFFF00';
      }
      else{
      $mycol = '#FFFFFF';
      }
	  
  print sprintf('<td align="center" bgcolor="'.$mycol.'">&nbsp;'.$day.'&nbsp;</td>',$day);    
}

print '</tr>';

}
print '</table>';
}
?>

<?php
//print the calendar for the current month
$month=$_GET['month'];
$year=$_GET['year'];
if ($month=="") {
list($month,$year) = explode(',',date('m,Y'));
pc_calendar($month,$year);
}
else{
pc_calendar($month,$year);
}
?>

Asking for a loop to put in there was a last attempt with my abilities. Can you shed any light on this for me?

Thanks,

Steve

I think

if ($daybooked == 1){
      $mycol = '#FFFF00';
      }
      else{
      $mycol = '#FFFFFF';
      }
  print sprintf('<td align="center" bgcolor="'.$mycol.'">&nbsp;'.$day.'&nbsp;</td>',$day); 

should be inside the while loop. It will obviously show the recent entry, because, its executed only after the while loop is executed. So, it will have only the last $daybooked value. Put it inside while loop and try again..

Ok, I moved the "}" so that the IF statement is in the loop. All holidays that are booked now are coloured. The problem is that there are two table cells for each day of the month being shown. I have included my latest code, that only has the change of include in the IF statement within the loop. Thanks for you help so far. Hope you can solve it!

<?php
function pc_calendar($month,$year,$opts = '') {


if (! isset($opts['month_link'])) {
$opts['month_link'] =
'<a href="Manager_calendar16.php?month=%d&year=%d">%s</a>';
}


list($prev_month,$prev_year) =
split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));
$prev_month_link =
sprintf($opts['month_link'],$prev_month,$prev_year,'&lt;');
list($next_month,$next_year) =
split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));
$next_month_link =
sprintf($opts['month_link'],$next_month,$next_year,'&gt;');
?>
<table width="50%" border="1" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF">
<tr>
<td align="left" colspan="7">
<?php print $prev_month_link ?>
</td>
<td align="center" colspan="16">
<?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year));
?>
</td>
<td align="right" colspan="6">
<?php print $next_month_link ?>
</td>
</tr>
<?php
$totaldays = date('t',mktime(0,0,0,$month,1,$year));
// print out days of the week
print "<tr>";
include("db_Conn.php");
$names = mysql_query("SELECT * FROM `staff` WHERE Manager_Num = '1'");



while ($row= mysql_fetch_array($names)) {

$staff_id = $row['Staff_ID'];
$f_name = $row['F_Name'];
$l_name = $row['L_Name'];
print "<td>$f_name $l_name</td>";



for ($day = 1; $day <= $totaldays; $day++) {
$daybooked = 0;

$sql= mysql_query("SELECT * FROM `Leave` where Staff_ID = $staff_id");
$numrows=mysql_num_rows($sql);

while ($row2= mysql_fetch_array($sql)) {
$leave_id = $row2['Staff_ID'];
$leave_date = $row2['Leave_Date'];
$return_date = $row2['Return_Date'];

$first_date = strtotime($leave_date);
$last_date = strtotime($return_date);

$new_date = date('d',$first_date);
$new_month = date('m',$first_date);
$new_year = date('Y',$first_date);

$ret_date = date('d',$last_date);
$ret_month = date('m',$last_date);
$ret_year = date('Y',$last_date);

$day_highlight = (($new_month == $month) && ($new_year == $year));
$ret_highlight = (($ret_month == $month) && ($ret_year == $year));


if (($day_highlight && ($day < $new_date)) || ($ret_highlight && ($day >= $ret_date)) || !($ret_highlight) && !($day_highlight)) {
$daybooked = 0;}
else{
$daybooked = 1;

echo $daybooked;}


      
      if ($daybooked == 1){
	  $mycol = '#FFFF00';
      }
      else{
      $mycol = '#FFFFFF';
      }
       //this will loop 10 times.
	  
  print sprintf('<td align="center" bgcolor="'.$mycol.'">&nbsp;'.$day.'&nbsp;</td>',$day);    
}
}

print '</tr>';

}
print '</table>';
}
?>

<?php
//print the calendar for the current month
$month=$_GET['month'];
$year=$_GET['year'];
if ($month=="") {
list($month,$year) = explode(',',date('m,Y'));
pc_calendar($month,$year);
}
else{
pc_calendar($month,$year);
}
?>

Thanks,

Steve

I think its printing twice because it has to go through 2 loops. 1 the for loop and the 2nd, while loop. I don't know how your query works, so I can't do much about it. for loop repeats $totaldays times. And a staffmember can have 'n' number of leaves. So, the resultset might be more than 1 ! What you can do is, put the whole thing (which you want to output) in an array, with $day being the key. If $day is already in the array, then don't insert, else, insert. Then print the output array.

I'm not very good with array's as I haven't had much to do with them to date. Would you be able to help here? If you can what information would you need?

Thanks,

Steve

Do you have date field in table staff ? If yes, you can add the day field in the query. If you dont have it, try putting the for loop inside the 2nd while loop. So, for 1 staffmember, it will check all the days. (here, in this case, its checking for ($day = 1; $day <= $totaldays; $day++) { condition for each staff. )

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.