开发者

Trying to print all the events from db.(looking for suggestion)

I'm building calendar events in php. I have stored all the events in database. And displaying calendar events according to current month. If the current month is feb then this script will print all feb month events. Here is query:

$result = $db->query("SELECT * FROM EVENTS WHERE user_id = '".$_SESSION['userid']."');

here is my code

$curren开发者_JAVA百科t_month = date('m');
$current_year = date('Y');
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
if($current_month == date("m", strtotime($row['date'])) && $current_year == date("Y", strtotime($row['date']))) {
print current month events      
}

But problem is if current month has not any events. Then what would I have to do? Please help me thanks. If you found any bug in my code. please point me my bug. thanks you :-) EDIT - But problem is if current month has not any events. Then I want to print next month events. How it is possible?


But problem is if current month has not any events. Then what would I have to do?

You could try adding a few events for the current month.


Edit: but, no kidding, your code isn't that well. At first there's no query, so we have to guess your query is correct. Have you printed all results of the query? Are there any events that should be displayed?

Second, you apparently don't use the powerful "WHERE" of SQL, you simply retreive all events and filter them using php (the if statement). You shouldn't. What if there are tens of thousands of events in the database?

Edit: answer was incorrect, adding months and years goes a little further than a single counter. You could change your if-statement and add a counter there, but if it's december 2011 and there are no events, which month and year will you query? ;-) You should take a look at ADDDATE() and use that in your query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜