Too much code (help with elegant loop) ?
Posted: Thu Jul 08, 2010 6:47 am
Hi all,
I've inherited a project that I'm trying to make sense of and it's full of huge code blocks which I
can't help but think could be reduced by using more "elegant" code - just wondering if anyone can help with
this example: (it refers to the first of seven checkboxes representing the days of the week). This code is
repeated seven times on the page (once for each checkbox).
any help at reducing the code by the elegant use of loops much appreciated.
best wishes
Monty
I've inherited a project that I'm trying to make sense of and it's full of huge code blocks which I
can't help but think could be reduced by using more "elegant" code - just wondering if anyone can help with
this example: (it refers to the first of seven checkboxes representing the days of the week). This code is
repeated seven times on the page (once for each checkbox).
any help at reducing the code by the elegant use of loops much appreciated.
best wishes
Monty
Code: Select all
<td class="day">
<?php
if (empty($_POST['datepicker'])) {
$day_name=date('D');
$date_spread="".date('m')."/".date('d')."/".date('Y')."";
$date_format=explode("/", $date_spread);
$date_spread="".$date_format[0]."/".$date_format[1]."/".$date_format[2]."";
}
else {
$date_format=explode("/", $_POST['datepicker']);
$date_spread="".$date_format[0]."/".$date_format[1]."/".$date_format[2]."";
} ?>
<span><?php
if (!isset($_POST['refresh_s'])) {
$day1= mktime(0, 0, 0, $date_format[0], $date_format[1], $date_format[2]);
}
else {
$day1= mktime(0, 0, 0, $date_format[0], $date_format[1], $date_format[2]);
$f="0";
if (date('D', $day1)=="Thu") {
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Fri") {
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Sat") {
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Sun") {
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Mon") {
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Tue") {
if ($_POST['tue']!="on") { $f=$f+1;
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
if (date('D', $day1)=="Wed") {
if ($_POST['wed']!="on") { $f=$f+1;
if ($_POST['thu']!="on") { $f=$f+1;
if ($_POST['fri']!="on") { $f=$f+1;
if ($_POST['sat']!="on") { $f=$f+1;
if ($_POST['sun']!="on") { $f=$f+1;
if ($_POST['mon']!="on") { $f=$f+1;
if ($_POST['tue']!="on") { $f=$f+1;
}
}
}
}
}
}
}
}
$day1= mktime(0, 0, 0, $date_format[0], ($date_format[1]+$f), $date_format[2]);
}