Wintergreen Posted September 8, 2006 Share Posted September 8, 2006 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:27 8It should output17 8There 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 2233 44 55 66 77 88 991010 1111 1212 1313 1414 1515 16161717 1818 1919 2020 2121 2222 23232424 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 https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/ Share on other sites More sharing options...
king arthur Posted September 8, 2006 Share Posted September 8, 2006 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 https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88403 Share on other sites More sharing options...
Wintergreen Posted September 8, 2006 Author Share Posted September 8, 2006 So what do I do to get the third value not to appear? Link to comment https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88405 Share on other sites More sharing options...
°°Ben³ Posted September 8, 2006 Share Posted September 8, 2006 You can remove the last array entry by using [url=http://www.php.net/manual/en/function.array-pop.php]array_pop[/url].Regards, Ben. Link to comment https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88421 Share on other sites More sharing options...
king arthur Posted September 8, 2006 Share Posted September 8, 2006 [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 https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88424 Share on other sites More sharing options...
Wintergreen Posted September 8, 2006 Author Share Posted September 8, 2006 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 https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88434 Share on other sites More sharing options...
Wintergreen Posted September 8, 2006 Author Share Posted September 8, 2006 Bump Link to comment https://forums.phpfreaks.com/topic/20117-slight-help-needed-with-a-calendar/#findComment-88548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.