| | |
Change my IF statement to a loop
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
Hi,
I am currently using the follwing IF statement:
if ($daybooked == 1){
print sprintf('<td align="center"
bgcolor="%s"> %d </td>',$opts['today_color'],$day);
}
else{
print sprintf('<td align="center"><font color="#999999"> %d </font></td>',$day);
}
I would like for this statement to loop. Any ideas?
Thanks,
Steve
I am currently using the follwing IF statement:
if ($daybooked == 1){
print sprintf('<td align="center"
bgcolor="%s"> %d </td>',$opts['today_color'],$day);
}
else{
print sprintf('<td align="center"><font color="#999999"> %d </font></td>',$day);
}
I would like for this statement to loop. Any ideas?
Thanks,
Steve
to loop ? Which loop ? Put the whole thing in whatever loop you want.
If thats not what you looking for, then please explain in detail.
Cheers,
Naveen
php Syntax (Toggle Plain Text)
<?php for($i=0;$i<10;$i++){ if ($daybooked == 1){ print sprintf('<td align="center" bgcolor="%s"> %d </td>',$opts['today_color'],$day); } else{ print sprintf('<td align="center"><font color="#999999"> %d </font></td>',$day); } } //this will loop 10 times. .... ?>
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*
*PM asking for help will be ignored*
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
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:
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
PHP Syntax (Toggle Plain Text)
<?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,'<'); 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,'>'); ?> <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.'"> '.$day.' </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 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..
•
•
•
•
if ($daybooked == 1){
$mycol = '#FFFF00';
}
else{
$mycol = '#FFFFFF';
}
print sprintf('<td align="center" bgcolor="'.$mycol.'"> '.$day.' </td>',$day);
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
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!
Thanks,
Steve
PHP Syntax (Toggle Plain Text)
<?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,'<'); 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,'>'); ?> <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.'"> '.$day.' </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.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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*
*PM asking for help will be ignored*
![]() |
Similar Threads
- Very basic de/encrypt program (Python)
- C++ nest loop program - Help (C++)
- Fill a combobox (Visual Basic 4 / 5 / 6)
- arrays and functions (C++)
- Array troubles? (C++)
- Help with File Reading loop (C++)
- I can't figure out how to change my jbutton font and color ( heres the code ) (Java)
- loop in main function to an "if" statement (C++)
Other Threads in the PHP Forum
- Previous Thread: Cookies problem - not working
- Next Thread: Comment Box
| Thread Tools | Search this Thread |
.htaccess alerts apache api archive array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database dataentry date display duplicates dynamic echo email emptydisplayvalue error execute explodefunction file files firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include insert ip javasciptvalidation javascript joomla keywords limit link login mail matching menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect remote script search securephp server sessions shot sms source space sql subscription syntax system table tutorial tutorials update upload url validator variable video web youtube






