Jump to content

Slight help needed with a calendar


Wintergreen

Recommended Posts

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:
[code]
  $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);
[/code]

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:

[code]
  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;
      } 
  }
[/code]

This outputs:
[code]
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
[/code]
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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=Wintergreen link=topic=107346.msg430590#msg430590 date=1157726053]
So what do I do to get the third value not to appear?
[/quote]

I just wouldn't bother building the concatenated string if you don't need it for anything else.
[code]
<?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);


?>
[/code]

You may find this has a bearing on your second problem too, if you fix it.
Link to comment
Share on other sites

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:

[code]
  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;
      } 
  }
[/code]

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.