Jump to content


Photo

Slight help needed with a calendar


  • Please log in to reply
6 replies to this topic

#1 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 08 September 2006 - 01:28 PM

I have a calendar set up that will read the posts from the DB, store the days of the month on which posts were made, and then provide links to the search page that will display posts made on the day you click.  Right now I have two problems, one which I don't quite understand:
   $sql = "SELECT post_time FROM posts ORDER BY postid asc";
   $query = mysql_query($sql);
   while ($row = mysql_fetch_assoc($query)) {
      $value = strtotime($row['post_time']);
      if(date("n", $value) == $current_month) {
         $date_holder .= date("j", $value) . "|";
      }
   }
   $date_holder2 = explode("|", $date_holder);
   $date_holders = array_unique($date_holder2);
   $date_counter = count($date_holders) - 1;
   echo $date_counter . "<br />";
   echo implode(" ", $date_holders);

Outputs:
2
7 8

It should output
1
7 8

There are only posts made on the 7th (yesterday) and today.  I subtract one because I use $date_counter in a loop later on that starts from zero.  But why does it have the value 2 after I've subtracted 1 from it when there are only two values in the array?

The second problem in my matching of dates where there are posts and the auto printing of the calendar.  The link appears on the 7th okay, but nothing shows up on the 8th.  Then, every date is printed twice.  I'll show the part that is giving me the problem, and then what it's outputting:

   for($days; $days <= $days_in_month; $days++) {
      if ($new_row == 0) {
         echo "<tr>";
      }
      echo "<td>";
      if($date_counter > 0) {
      /* Are there posts for this month */
         for($j = 0; $j < $date_counter; $j++) {
         /* loop through as many times as there are values in the array */
            if($days == $date_holders[$j]) {
               echo "<a href=search.php?search_date=" . $current_month . "-" . $days . ">" . $days . "</a>";
            } else {
            /* Problem with double dates is here.  It goes through the for loop twice (for now) */
               echo $days;
            } 
         }
      } else {
         echo $days;
      }
      echo "</td>";
      $new_row++;
      if ($new_row == 7) {
         echo "</tr>";
         $new_row = 0;
      }   
   }

This outputs:
Sun	Mon	Tue	Wed	Thu	Fri	Sat
					11	22
33	44	55	66	77	88	99
1010	1111	1212	1313	1414	1515	1616
1717	1818	1919	2020	2121	2222	2323
2424	2525	2626	2727	2828	2929	3030
The first 7 is a link like I want, the second is not.  Neither of the 8s show up as links.

Can anyone help me come up with a way to match the dates better and avoid double writing dates?  Thing is, the calendar itself works perfectly, it's just that when I had to start adding loops for seeing if the dates matched the day being written that I had problems.  Any help would be wonderful

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 08 September 2006 - 02:25 PM

First problem would be due to the fact you are exploding the date_holder string using the "|" which is fine, but the thing is, you are concatenating that onto the end of the string twice (because your while loop executes twice), so when explode() splits it, it splits it into three strings, the last one being an empty one.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 08 September 2006 - 02:34 PM

So what do I do to get the third value not to appear?



#4 °°Ben³

°°Ben³
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationGermany / Remagen

Posted 08 September 2006 - 02:52 PM

You can remove the last array entry by using array_pop.

Regards, Ben.
desktop and web development ;)
---
English is not my native language. Please excuse if I made some errors in expression.

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 08 September 2006 - 02:57 PM

So what do I do to get the third value not to appear?


I just wouldn't bother building the concatenated string if you don't need it for anything else.
<?php

   $sql = "SELECT post_time FROM posts ORDER BY postid asc";
   $query = mysql_query($sql);
   $date_holder2 = array();
   while ($row = mysql_fetch_assoc($query)) {
      $value = strtotime($row['post_time']);
      if(date("n", $value) == $current_month) {
         $date_holder2[] = date("j", $value);
      }
   }
   $date_holders = array_unique($date_holder2);
   $date_counter = count($date_holders) - 1;
   echo $date_counter . "<br />";
   echo implode(" ", $date_holders);


?>

You may find this has a bearing on your second problem too, if you fix it.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 08 September 2006 - 03:27 PM

Okay, thank you.  That's much nicer and cleaner than what I had going on.  One final question.  The $date_holders now has the two values correctly.  I switched the code for printing the days to not use as many loops, and instead it now looks like:

   for($days; $days <= $days_in_month; $days++) {
      if ($new_row == 0) {
         echo "<tr>";
      }
      echo "<td>";
      if($date_counter > 0) {
        	if(array_search($days, $date_holders)) {
        		echo "<a href=search.php?search_date=" . $current_month . "-" . $days . ">" . $days . "</a>";
       	} else {
       		echo $days;
       	}
      } else {
      	echo $days;
      }
      echo "</td>";
      $new_row++;
      if ($new_row == 7) {
         echo "</tr>";
         $new_row = 0;
      }   
   }

However... Only the 8 is showing up as a link.  The array has the 7 and the 8 now, no extra value, and they're in the 0 and 1 position of the array.  Why doesn't the 7 show up as a link?

#7 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 08 September 2006 - 05:34 PM

Bump




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users