4 Replies - 551 Views - Last Post: 02 October 2012 - 03:41 PM Rate Topic: -----

#1 jack13580  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 11

I have completely working code but it is ugly as $@#%*

Posted 01 October 2012 - 02:32 PM

so could anyone give me suggestions on how to fix up my code below
<?php include "./src/scripts/top.php"; ?>
<div id="content"> 
  <div id="calendar-logo">Troop 557 Calendar</div>
    <?php
	  if (isset($_GET['day'])){
		$day = $_GET['day'];
	  } else {
		$day = date("j");
	  }
      if(isset($_GET['month'])){
		$month = $_GET['month'];
      } else {
		$month = date("n");
	  }
	  if(isset($_GET['year'])){
	    $year = $_GET['year'];
	  }else{
	    $year = date("Y");
	  }
	  if($month > 12){
	    $month = 12;
	  }
	  if($month < 1){
	    $month = 1;
	  }
	  if(strlen($year) != 4){
	    $year = date("Y");
	  }

	  if($month == 1){
	    $year_back_button = ($year-1);
		$month_back_button = 12;
		$back_month = 12;
	  }else {
	    $month_back_button = ($month-1);
		$year_back_button = $year;
		$back_month = ($month-1);
	  }
	  if($month == 12){
	    $year_forward_button = ($year+1);
		$month_forward_button = 1;
		$next_month = 1;
	  }else {
	    $month_forward_button = ($month+1);
		$year_forward_button = $year;
		$next_month = ($month+1);
	  }
	  $currentTimeStamp = strtotime( "$day-$month-$year");
	  $backTimeStamp = strtotime( "$day-$back_month-$year");
	  $nextTimeStamp = strtotime( "$day-$next_month-$year");
	  $backmonthName = date("M", $backTimeStamp);
	  $nextmonthName = date("M", $nextTimeStamp);
	  $monthName = date("F", $currentTimeStamp);
	  $numDays = date("t", $currentTimeStamp);
	  $counter = 0;
	  
	  $con = mysql_connect("*****","******","*****") or die(mysql_error());
	  $db = mysql_select_db("*****", $con);	  
	?>
    <?php if(isset($_GET['day'])){
	  $sql = "SELECT * FROM `calendar` WHERE `year_of`='".$year."' AND `month_of`='".$month."' AND `day_of`='".$day."'";
	  $res = mysql_query($sql) or die(mysql_error());
	  
	  $sqlcheck = "SELECT count(*) AS amount FROM `calendar` WHERE `year_of`='".$year."' AND `month_of`='".$month."' AND `day_of`='".$day."'";
	  $rescheck = mysql_query($sqlcheck) or die(mysql_error());
	  
	  $is_event_counter = 0;
	  $current_day = date("j", $currentTimeStamp);
	  $current_month = date("n", $currentTimeStamp);
	  $current_year = date("Y", $currentTimeStamp);
	  $rowcheck = mysql_fetch_assoc($rescheck);
	  if(mysql_num_rows($res) > 0){
	      echo "There is ".$rowcheck['amount']." Events for this day, ".$current_month."/".$current_day."/".$current_year.".<br /><br />";
		  while($row = mysql_fetch_assoc($res)){
			$type_of_event = $row['type_of_event'];
			$day_of = $row['day_of'];
			$month_of = $row['month_of'];
			$year_of = $row['year_of'];
			$event_title = $row['event_title'];
			$event_description = $row['event_description'];
			$is_event_counter++;
			
			if($day == $day_of){
			  echo "Event ".$is_event_counter."<br />Title: ".$event_title."<br />Type of Event: ".$type_of_event."<br /><div class=\"description\">Event Description: ".htmlspecialchars_decode(nl2br2($event_description))."</div><br /><br />";
			}else {
			  echo "There are no events for this day, ".$current_month."/".$current_day."/".$current_year.".";
			}
		  }
	  }else {
	    echo "There are no events for this day, ".$current_month."/".$current_day."/".$current_year.".";
	  }
	  echo "<div id=\"back-to-calendar\"><a href=\"?month=".$month."&amp;year=".$year."\">Go Back?</a></div>";
	  
	}else { ?>
  <table id="calendar-table" cellspacing="3" cellpadding="3">
    <tr id="top-controls">
      <th colspan="2" id="back-button"><?php echo "<a href=\"?month=".$month_back_button."&amp;year=".$year_back_button."\">"; ?><span style="font-size: 16px;">&laquo;</span>&nbsp;&nbsp;<?php echo $backmonthName; echo " $year_back_button"; ?></a></th>
      <th colspan="3"><?php echo $monthName." ".$year; ?></th>
      <th colspan="2" id="forward-button"><?php echo "<a href=\"?month=".$month_forward_button."&amp;year=".$year_forward_button."\">"; ?><?php echo $nextmonthName; echo " $year_forward_button";  ?>&nbsp;<span style="font-size: 16px;">&raquo;</span></a></th>
    </tr>
    <tr>
      <th>Sun</th>
      <th>Mon</th>
      <th>Tue</th>
      <th>Wed</th>
      <th>Thu</th>
      <th>Fri</th>
      <th>Sat</th>
    </tr>
    <?php 
	  echo "<tr>\r\n";
	  for($i = 1; $i < $numDays+1; $i++, $counter++){
	    $monthstring = $month;
		$monthlength = strlen($monthstring);
		$daystring = $i;
		$daylength = strlen($daystring);
	    $timeStamp = strtotime("$year-$month-$i");
		$timeStamp_before = strtotime("$year-$month_before-$i");
		$month_before = ($month == 1) ? "12" : $month-1;
		$year_before = ($month == 1) ? $year-1 : $year;
	  
	    $firstDay = date("w", $timeStamp);
	    $days_before = date("j", $timeStamp_before);
	    $days_before_thisMonth = $days_before - $firstDay;
	    $days_before_thisMonth = $days_before_thisMonth+1;
		
		for($if1=0;$if1!=1 && $i < 2;$if1++){
	      $if_goes_over_check = ($days_before - $days_before_thisMonth) + 1;
		}
		
		if($i == 1) {
		  //$firstDay = ($firstDay > 4) ? $firstDay = 4 : $firstDay;
		  
		  for($fd = 0; $fd < $firstDay; $fd++, $counter++, $days_before_thisMonth++) {
		      echo "<td>";
		      $sql = "SELECT * FROM `calendar` WHERE `year_of`='".$year_before."' AND `month_of`='".$month_before."'";
			  $res = mysql_query($sql) or die(mysql_error());
			  $sqlcheck = "SELECT count(*) AS amount FROM `calendar` WHERE `year_of`='".$year_before."' AND `month_of`='".$month_before."' AND `day_of`='".$days_before_thisMonth."'";
	          $rescheck = mysql_query($sqlcheck) or die(mysql_error());
			  echo "<a href=\"?day=".$days_before_thisMonth."&amp;month=".$month_before."&amp;year=".$year_before."\" class=\"calendar-link\"";
		      while($rowcheck = mysql_fetch_assoc($rescheck)){
		        $amount_of_events = $rowcheck['amount'];
			    echo " onmouseover=\"showhint('There is ".$amount_of_events." event(s) for this day.', this, event, '300px')\"";
		      }
		      echo ">".$days_before_thisMonth."</a><br />";
	  
			  while($row = mysql_fetch_assoc($res)){
				$type_of_event = $row['type_of_event'];
				$day_of = $row['day_of'];
				$month_of = $row['month_of'];
				$year_of = $row['year_of'];
				$event_title = $row['event_title'];
				$event_description = $row['event_description'];
				if($days_before_thisMonth == $day_of){
		          echo "<a href=\"?day=".$day_of."&amp;month=".$month_of."&amp;year=".$year_of."\" class=\"calendar-link\" onmouseover=\"showhint('Event Title: ".$event_title."&lt;br /&gt;Type of Event: ".$type_of_event."&lt;br /&gt;Description: ".nl2br2($event_description)."', this, event, '300px')\"><img src=\"images/event.gif\" alt=\"\" />"; if((strlen($event_title)>10)){ echo "".substr($event_title, 0, 10)."..."; }else echo $event_title; echo "</a><br />";
				}
			  }
			  echo "</td>\r\n";
		  }
		}
		$days_in_all = $if_goes_over_check + $i;
		if($counter % 7 == 0 && $days_in_all < 35){
		  echo"</tr>\r\n<tr>\r\n";
		}
		if($counter % 7 == 0 && $days_in_all > 35){
		   echo"</tr>\r\n<tr class=\"over\">\r\n";
		}
		if($monthlength <= 1){
		  $monthstring = "0".$monthstring;
		}
		if($daylength <=1){
		  $daystring = "0".$daystring;
		}
		$current_month = date("n");
		$current_year = date("Y");
		if($days_in_all > 35){ $this_day_goes_over = $i;
		  $amount_of_extras = $days_in_all - 35;
		}
		echo "<td";
		if($this_day_goes_over){ $counter_days_over = 1; if($amount_of_extras > 1){ echo " class=\"day-thats-over2\""; }else { echo " class=\"day-thats-over\""; }}else echo " class=\"months-days\"";
		echo ">";
		  $sql = "SELECT * FROM `calendar` WHERE `year_of`='".$year."' AND `month_of`='".$month."'";
		  $res = mysql_query($sql) or die(mysql_error());
		  if($this_day_goes_over){ echo "<hr />"; }
		  if($day == $i && $month == $current_month && $year == $current_year){ echo "<div id=\"current-day\"></div>"; }
		  $sqlcheck = "SELECT count(*) AS amount FROM `calendar` WHERE `year_of`='".$year."' AND `month_of`='".$month."' AND `day_of`='".$i."'";
	      $rescheck = mysql_query($sqlcheck) or die(mysql_error());
		  echo "<a href=\"?day=".$i."&amp;month=".$month."&amp;year=".$year."\" class=\"calendar-link\"";
		  while($rowcheck = mysql_fetch_assoc($rescheck)){
		    $amount_of_events = $rowcheck['amount'];
			echo " onmouseover=\"showhint('There is ".$amount_of_events." event(s) for this day.', this, event, '300px')\"";
		  }
          echo ">".$i."</a><br />";
		  $event_stopper = 0;
		  while($row = mysql_fetch_assoc($res)){
		    $type_of_event = $row['type_of_event'];
		    $day_of = $row['day_of'];
		    $month_of = $row['month_of'];
		    $year_of = $row['year_of'];
		    $event_title = $row['event_title'];
		    $event_description = $row['event_description'];
			if($i == $day_of){
			  $fix_this_day = ($this_day_goes_over == 30) ? null : "23";
			  $fix_this_day2 = ($this_day_goes_over == 31) ? null : "24";
			  if($this_day_goes_over == $day_of || $fix_this_day == $day_of || $fix_this_day2 == $day_of){
			    if($event_stopper < 1){
		          echo "<a href=\"?day=".$day_of."&amp;month=".$month_of."&amp;year=".$year_of."\" class=\"calendar-link\" onmouseover=\"showhint('Event Title: ".$event_title."&lt;br /&gt;Type of Event: ".$type_of_event."&lt;br /&gt;Description: ".nl2br2($event_description)."', this, event, '300px')\"><img src=\"images/event.gif\" alt=\"\" />"; if((strlen($event_title)>10)){ echo "".substr($event_title, 0, 10)."..."; }else echo $event_title; echo "</a><br />";
				  $event_stopper++;
			    }
			  }else {
			    echo "<a href=\"?day=".$day_of."&amp;month=".$month_of."&amp;year=".$year_of."\" class=\"calendar-link\" onmouseover=\"showhint('Event Title: ".$event_title."&lt;br /&gt;Type of Event: ".$type_of_event."&lt;br /&gt;Description: ".nl2br2($event_description)."', this, event, '300px')\"><img src=\"images/event.gif\" alt=\"\" />"; if((strlen($event_title)>10)){ echo "".substr($event_title, 0, 10)."..."; }else echo $event_title; echo "</a><br />";
			  }
			}
		  }
		  echo "</td>\r\n";
		  $is_leap_year = date('L',$currentTimeStamp);
		  if($is_leap_year == 1){
		    if($month == 2){
			  $sql_tds = "SELECT `days` FROM `months_days` WHERE `month`='".$month."' AND `is_leap_month`='1'";
			}else {
			  $sql_tds = "SELECT `days` FROM `months_days` WHERE `month`='".$month."' AND `is_leap_month`='0'";
			}
		  }else {
		    $sql_tds = "SELECT `days` FROM `months_days` WHERE `month`='".$month."' AND `is_leap_month`='0'";
		  }
		  $td_query = mysql_query($sql_tds) or die(mysql_error());
		  while($td_amount_to = mysql_fetch_assoc($td_query)){
		    $days_in_month_for_td = $td_amount_to['days'];
		  }
		  $td_amount = $if_goes_over_check + $days_in_month_for_td;
		  if($td_amount == 36){
		    if($counter % 7 == 0 && $days_in_all > 35){
		      for($td_fix=1;$td_fix<7;$td_fix++){
		        echo "<td class=\"not-used\">&nbsp;</td>\r\n";
			  }
			}
		  }else if($td_amount == 37){
			if($counter % 6 == 0 && $days_in_all > 36){
				for($td_fix=1;$td_fix<6;$td_fix++){
			      echo "<td class=\"not-used\">&nbsp;</td>\r\n";
				}
		    }
		  }
	  }
	  $days_after = $firstDate+$counter;
	  $days_after_thisMonth = 35 - $days_after;
	  if($days_after_thisMonth) {
	      //|| $days_after_thisMonth == -1 || $days_after_thisMonth == -2      old way of doing this, this goes inside the above if <
	      //if($days_after_thisMonth == -1){ $days_after_thisMonth = 6; }else if($days_after_thisMonth == -2){ $days_after_thisMonth = 5; } <
          $counter_last_day = 1;
		  for($ld = 0; $ld < $days_after_thisMonth; $ld++, $counter_last_day++) {
		    echo "<td>";
			$month_after_this = ($month == 12) ? "1" : $month+1;
			$year_after_this = ($month == 12) ? $year+1 : $year;
		    $sql = "SELECT * FROM `calendar` WHERE `year_of`='".$year_after_this."' AND `month_of`='".$month_after_this."'";
			$res = mysql_query($sql) or die(mysql_error());
			$sqlcheck = "SELECT count(*) AS amount FROM `calendar` WHERE `year_of`='".$year_after_this."' AND `month_of`='".$month_after_this."' AND `day_of`='".$counter_last_day."'";
	        $rescheck = mysql_query($sqlcheck) or die(mysql_error());
			echo "<a href=\"?day=".$counter_last_day."&amp;month=".$month_after_this."&amp;year=".$year_after_this."\" class=\"calendar-link\"";
		    while($rowcheck = mysql_fetch_assoc($rescheck)){
		      $amount_of_events = $rowcheck['amount'];
			  echo " onmouseover=\"showhint('There is ".$amount_of_events." event(s) for this day.', this, event, '300px')\"";
		    }
		    echo ">".$counter_last_day."</a><br />";
	  
			while($row = mysql_fetch_assoc($res)){
			  $type_of_event = $row['type_of_event'];
			  $day_of = $row['day_of'];
			  $month_of = $row['month_of'];
			  $year_of = $row['year_of'];
			  $event_title = $row['event_title'];
			  $event_description = $row['event_description'];
			  if($counter_last_day == $day_of){
		        echo "<a href=\"?day=".$day_of."&amp;month=".$month_of."&amp;year=".$year_of."\" class=\"calendar-link\" onmouseover=\"showhint('Event Title: ".$event_title."&lt;br /&gt;Type of Event: ".$type_of_event."&lt;br /&gt;Description: ".nl2br2($event_description)."', this, event, '300px')\"><img src=\"images/event.gif\" alt=\"\" />"; if((strlen($event_title)>10)){ echo "".substr($event_title, 0, 10)."..."; }else echo $event_title; echo "</a><br />";
			  }
			}
			echo "</td>\r\n";
		  }
		}
      echo "</tr>\r\n";
	?>
  </table>
  <?php 
    if($this_day_goes_over){
      echo "<div id=\"bottom-fix-for-calendar\"></div>\r\n";
    }
  ?>
  <?php } ?>
</div>
<?php include "./src/scripts/bottom.php"; ?>



nl2br2 function
function nl2br2($string) { 
  $array = array(
    '\n',
    '\r',
    '\n\r',
    '\r\n'
  );
  $string = str_replace($array, "&lt;br /&gt;", $string); 
  return $string; 
}



css
#calendar-logo {
  font-size: 18pt;
  padding-bottom: 5px;
  border-bottom: 1px solid #000;
  width: 98%;
}

#calendar-table {
  background: #f9f9f9;
  border-left: #aaa 1px solid;
  border-right: #aaa 1px solid;
  border-top: #aaa 1px solid;
  border-bottom: #aaa 1px solid;
  border-collapse: collapse;
  margin: 1em 1em 1em 0px;
  width:98%;
  font-family: 'Courier New';
}

#calendar-table th {
  text-align: center;
  border-left: #aaa 1px solid;
  border-right: #aaa 1px solid;
  border-top: #aaa 1px solid;
  border-bottom: #aaa 1px solid;
  padding: 0.2em;
  font-weight: bold;
  background: #480;
  font-size: 12pt;
  color: #fff;
}
#calendar-table td {
  border-left: #aaa 1px solid;
  border-right: #aaa 1px solid;
  border-top: #aaa 1px solid;
  border-bottom: #aaa 1px solid;
  padding-left: 0.2em;
  padding-right: 0.2em;
  font-size:10pt;
  height: 80px;
  text-align: left;
  width: 100px;
  color: red;
  background-color: #EEE;
  vertical-align: text-top;
}

#calendar-table #top-controls th {
  border: 0px;
  height: 35px;
  color: #fff;
  color: #20C;
  background-color: #5984BB;
  font-size: 18pt;
}

#calendar-table #top-controls #back-button {
  color: #fff;
  font: bold 12px arial;
  text-align: left;
  padding-left: 15px;
  background-color: #5984BB;
}

#calendar-table #top-controls #back-button a {
  background-color: transparent;
  color: #fff;
}

#calendar-table #top-controls #forward-button {
  color: #fff;
  font: bold 12px arial;
  text-align: right;
  padding-right: 15px;
  background-color: #5984BB;
}

#calendar-table #top-controls #forward-button a {
  background-color: inherit;
  color: #fff;
}

#calendar-table .months-days {
  background-color: #8EF;
}

.description {       
  word-wrap: break-word;
  width: 98%;
  padding-right: 3px;
}

#back-to-calendar {
  height: 50px;
}

#calendar-table .calendar-link {
  font: normal 12px arial;
  position: relative;
  z-index: 1;
}

#calendar-table #current-day {
  position: absolute;
  width: 90px;
  height: 75px;
  margin: 1px;
  opacity:0.5;
  background-image: url('http://troop557.vacau.com/images/transparent-yellow.png');
}

#calendar-table tr.over td.day-thats-over {
  background-color: #8EF;
  height: 43px;
  width: 91px;
  border: none;
  position: absolute;
  top: 735px;
}

#calendar-table tr.over td.day-thats-over hr {
  border-left: none;
  border-right: none;
  border-top: #aaa 1px solid;
  border-bottom: none;
  /*width: 103px;*/
}

#calendar-table tr.over td.day-thats-over2 {
  background-color: #8EF;
  height: 43px;
  width: 91px;
  border: none;
  position: absolute;
  top: 735px;
  margin-left: 100px;
  /*left: 523px;*/
}

#calendar-table tr.over td.day-thats-over2 hr {
  border-left: none;
  border-right: none;
  border-top: #aaa 1px solid;
  border-bottom: none;
  /*width: 103px;*/
}

#calendar-table tr.over td.not-used {
  position: absolute;
  top: 5px;
  left: 5px;
  width: 0px;
  height: 0px;
  background-color: #303030;
  border: none;
}

#bottom-fix-for-calendar {
  height: 2px;
  width: 99%;
  background-color: inherit;
  position: relative;
  top: -19px;
}



just to tell you this advanced calendar application is made for google chrome but on my site below I have it working in all browsers due to some css tricks and php tricks

my site
http://troop557.vacau.com/calendar.php

Is This A Good Question/Topic? 0
  • +

Replies To: I have completely working code but it is ugly as $@#%*

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: I have completely working code but it is ugly as $@#%*

Posted 01 October 2012 - 02:42 PM

  • Limit yourself to breaking out of php one time per file (you really don't need one)
  • Use strtotime and date on the $_GET variables. There is no need to do:
  • $x = $_GET['x'];
    

  • Use nl2br().

This post has been edited by CTphpnwb: 01 October 2012 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 jack13580  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 11

Re: I have completely working code but it is ugly as $@#%*

Posted 01 October 2012 - 08:28 PM

where you are getting

Quote

$x = $_GET['x'];
from as its not in my code?



I'm using my custom function nl2br2 because the normal function nl2br wasn't doing what I wanted

and you left 3 blank
Was This Post Helpful? 0
  • +
  • -

#4 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 245
  • View blog
  • Posts: 719
  • Joined: 24-November 08

Re: I have completely working code but it is ugly as $@#%*

Posted 02 October 2012 - 02:43 AM

View Postjack13580, on 02 October 2012 - 04:28 AM, said:

where you are getting

Quote

$x = $_GET['x'];
from as its not in my code?


He was using it as an example. It's the same as:

$year = $_GET['year'];
$day = $_GET['day'];
$month = $_GET['month'];



which is in your code.
Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 801
  • View blog
  • Posts: 1,700
  • Joined: 30-January 09

Re: I have completely working code but it is ugly as $@#%*

Posted 02 October 2012 - 03:41 PM

I haven't got time for a long reply, but look into Code Separation. We have a tutorial on Code Separation here at DIC.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1