Change my IF statement to a loop

Reply

Join Date: Dec 2007
Posts: 6
Reputation: gooky9 is an unknown quantity at this point 
Solved Threads: 0
gooky9 gooky9 is offline Offline
Newbie Poster

Change my IF statement to a loop

 
0
  #1
Feb 23rd, 2008
Hi,

I am currently using the follwing IF statement:

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);
}

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

Thanks,
Steve
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,742
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Change my IF statement to a loop

 
0
  #2
Feb 23rd, 2008
to loop ? Which loop ? Put the whole thing in whatever loop you want.
  1. <?php
  2. for($i=0;$i<10;$i++){
  3. if ($daybooked == 1){
  4. print sprintf('<td align="center"
  5. bgcolor="%s">&nbsp;%d&nbsp;</td>',$opts['today_color'],$day);
  6. }
  7. else{
  8. print sprintf('<td align="center"><font color="#999999">&nbsp;%d&nbsp;</font></td>',$day);
  9. }
  10. } //this will loop 10 times.
  11. ....
  12. ?>

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

Cheers,
Naveen
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: gooky9 is an unknown quantity at this point 
Solved Threads: 0
gooky9 gooky9 is offline Offline
Newbie Poster

Re: Change my IF statement to a loop

 
0
  #3
Feb 24th, 2008
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:

  1. <?php
  2. function pc_calendar($month,$year,$opts = '') {
  3.  
  4.  
  5. if (! isset($opts['month_link'])) {
  6. $opts['month_link'] =
  7. '<a href="Manager_calendar16.php?month=%d&year=%d">%s</a>';
  8. }
  9.  
  10.  
  11. list($prev_month,$prev_year) =
  12. split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));
  13. $prev_month_link =
  14. sprintf($opts['month_link'],$prev_month,$prev_year,'&lt;');
  15. list($next_month,$next_year) =
  16. split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));
  17. $next_month_link =
  18. sprintf($opts['month_link'],$next_month,$next_year,'&gt;');
  19. ?>
  20. <table width="50%" border="1" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF">
  21. <tr>
  22. <td align="left" colspan="7">
  23. <?php print $prev_month_link ?>
  24. </td>
  25. <td align="center" colspan="16">
  26. <?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year));
  27. ?>
  28. </td>
  29. <td align="right" colspan="6">
  30. <?php print $next_month_link ?>
  31. </td>
  32. </tr>
  33. <?php
  34. $totaldays = date('t',mktime(0,0,0,$month,1,$year));
  35. // print out days of the week
  36. print "<tr>";
  37. include("db_Conn.php");
  38. $names = mysql_query("SELECT * FROM `staff` WHERE Manager_Num = '1'");
  39.  
  40.  
  41.  
  42. while ($row= mysql_fetch_array($names)) {
  43.  
  44. $staff_id = $row['Staff_ID'];
  45. $f_name = $row['F_Name'];
  46. $l_name = $row['L_Name'];
  47. print "<td>$f_name $l_name</td>";
  48.  
  49.  
  50.  
  51. for ($day = 1; $day <= $totaldays; $day++) {
  52. $daybooked = 0;
  53.  
  54. $sql= mysql_query("SELECT * FROM `Leave` where Staff_ID = $staff_id");
  55. $numrows=mysql_num_rows($sql);
  56.  
  57. while ($row2= mysql_fetch_array($sql)) {
  58. $leave_id = $row2['Staff_ID'];
  59. $leave_date = $row2['Leave_Date'];
  60. $return_date = $row2['Return_Date'];
  61.  
  62. $first_date = strtotime($leave_date);
  63. $last_date = strtotime($return_date);
  64.  
  65. $new_date = date('d',$first_date);
  66. $new_month = date('m',$first_date);
  67. $new_year = date('Y',$first_date);
  68.  
  69. $ret_date = date('d',$last_date);
  70. $ret_month = date('m',$last_date);
  71. $ret_year = date('Y',$last_date);
  72.  
  73. $day_highlight = (($new_month == $month) && ($new_year == $year));
  74. $ret_highlight = (($ret_month == $month) && ($ret_year == $year));
  75.  
  76.  
  77. if (($day_highlight && ($day < $new_date)) || ($ret_highlight && ($day >= $ret_date)) || !($ret_highlight) && !($day_highlight)) {
  78. $daybooked = 0;}
  79. else{
  80. $daybooked = 1;
  81. }
  82. echo $daybooked;}
  83.  
  84.  
  85.  
  86. if ($daybooked == 1){
  87. $mycol = '#FFFF00';
  88. }
  89. else{
  90. $mycol = '#FFFFFF';
  91. }
  92.  
  93. print sprintf('<td align="center" bgcolor="'.$mycol.'">&nbsp;'.$day.'&nbsp;</td>',$day);
  94. }
  95.  
  96. print '</tr>';
  97.  
  98. }
  99. print '</table>';
  100. }
  101. ?>
  102.  
  103. <?php
  104. //print the calendar for the current month
  105. $month=$_GET['month'];
  106. $year=$_GET['year'];
  107. if ($month=="") {
  108. list($month,$year) = explode(',',date('m,Y'));
  109. pc_calendar($month,$year);
  110. }
  111. else{
  112. pc_calendar($month,$year);
  113. }
  114. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,742
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Change my IF statement to a loop

 
0
  #4
Feb 24th, 2008
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..
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: gooky9 is an unknown quantity at this point 
Solved Threads: 0
gooky9 gooky9 is offline Offline
Newbie Poster

Re: Change my IF statement to a loop

 
0
  #5
Feb 24th, 2008
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!

  1. <?php
  2. function pc_calendar($month,$year,$opts = '') {
  3.  
  4.  
  5. if (! isset($opts['month_link'])) {
  6. $opts['month_link'] =
  7. '<a href="Manager_calendar16.php?month=%d&year=%d">%s</a>';
  8. }
  9.  
  10.  
  11. list($prev_month,$prev_year) =
  12. split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));
  13. $prev_month_link =
  14. sprintf($opts['month_link'],$prev_month,$prev_year,'&lt;');
  15. list($next_month,$next_year) =
  16. split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));
  17. $next_month_link =
  18. sprintf($opts['month_link'],$next_month,$next_year,'&gt;');
  19. ?>
  20. <table width="50%" border="1" cellspacing="1" cellpadding="2" align="center" bgcolor="#FFFFFF">
  21. <tr>
  22. <td align="left" colspan="7">
  23. <?php print $prev_month_link ?>
  24. </td>
  25. <td align="center" colspan="16">
  26. <?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year));
  27. ?>
  28. </td>
  29. <td align="right" colspan="6">
  30. <?php print $next_month_link ?>
  31. </td>
  32. </tr>
  33. <?php
  34. $totaldays = date('t',mktime(0,0,0,$month,1,$year));
  35. // print out days of the week
  36. print "<tr>";
  37. include("db_Conn.php");
  38. $names = mysql_query("SELECT * FROM `staff` WHERE Manager_Num = '1'");
  39.  
  40.  
  41.  
  42. while ($row= mysql_fetch_array($names)) {
  43.  
  44. $staff_id = $row['Staff_ID'];
  45. $f_name = $row['F_Name'];
  46. $l_name = $row['L_Name'];
  47. print "<td>$f_name $l_name</td>";
  48.  
  49.  
  50.  
  51. for ($day = 1; $day <= $totaldays; $day++) {
  52. $daybooked = 0;
  53.  
  54. $sql= mysql_query("SELECT * FROM `Leave` where Staff_ID = $staff_id");
  55. $numrows=mysql_num_rows($sql);
  56.  
  57. while ($row2= mysql_fetch_array($sql)) {
  58. $leave_id = $row2['Staff_ID'];
  59. $leave_date = $row2['Leave_Date'];
  60. $return_date = $row2['Return_Date'];
  61.  
  62. $first_date = strtotime($leave_date);
  63. $last_date = strtotime($return_date);
  64.  
  65. $new_date = date('d',$first_date);
  66. $new_month = date('m',$first_date);
  67. $new_year = date('Y',$first_date);
  68.  
  69. $ret_date = date('d',$last_date);
  70. $ret_month = date('m',$last_date);
  71. $ret_year = date('Y',$last_date);
  72.  
  73. $day_highlight = (($new_month == $month) && ($new_year == $year));
  74. $ret_highlight = (($ret_month == $month) && ($ret_year == $year));
  75.  
  76.  
  77. if (($day_highlight && ($day < $new_date)) || ($ret_highlight && ($day >= $ret_date)) || !($ret_highlight) && !($day_highlight)) {
  78. $daybooked = 0;}
  79. else{
  80. $daybooked = 1;
  81.  
  82. echo $daybooked;}
  83.  
  84.  
  85.  
  86. if ($daybooked == 1){
  87. $mycol = '#FFFF00';
  88. }
  89. else{
  90. $mycol = '#FFFFFF';
  91. }
  92. //this will loop 10 times.
  93.  
  94. print sprintf('<td align="center" bgcolor="'.$mycol.'">&nbsp;'.$day.'&nbsp;</td>',$day);
  95. }
  96. }
  97.  
  98. print '</tr>';
  99.  
  100. }
  101. print '</table>';
  102. }
  103. ?>
  104.  
  105. <?php
  106. //print the calendar for the current month
  107. $month=$_GET['month'];
  108. $year=$_GET['year'];
  109. if ($month=="") {
  110. list($month,$year) = explode(',',date('m,Y'));
  111. pc_calendar($month,$year);
  112. }
  113. else{
  114. pc_calendar($month,$year);
  115. }
  116. ?>

Thanks,

Steve
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,742
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Change my IF statement to a loop

 
0
  #6
Feb 24th, 2008
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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: gooky9 is an unknown quantity at this point 
Solved Threads: 0
gooky9 gooky9 is offline Offline
Newbie Poster

Re: Change my IF statement to a loop

 
0
  #7
Feb 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,742
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Change my IF statement to a loop

 
0
  #8
Feb 26th, 2008
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. )
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC