3 Hopefully Simple questions..

Hello! I’m preparing for the launch of a website I am working on, and there are a few problems I have ran into that I haven’t yet solved. The website is a fairly simple one, it keeps track of local events in my town. Anyone can get online and submit an event, then I see a form where I can approve or deny them (no spam!), and then they appear on the webpage for everyone to see. I have a couple different categories, such as night life, local bands, etc. I’m doing all of this with php and mysql.
Here are the things I am having issues with. I want the events list something like

Monday, December 5
event1
event2
Tuesday, December 6
event3
event4

My problem lies in getting the bolded dates to appear when all the events for a day have been queried. Should I use an if then command? If so, can someone give me an example as to what that would look like? I can show you the query I have now if needed.
The other problem I have is when I am going to limit the number of events per page to about 20, and I know I can do this with the LIMIT SQL command. But, how do I get the bolded date to appear again on the next page, so the user will know what date they are looking at.
And my final problem is about user event submission. Currently the user will have to enter the event’s date in the SQL understood format, something like 2011-12-20. But I would rather the user see a small calendar where they can click the date and I could then submit it to my database.
Any help would be greatly appreciated!

For the date field I’d suggest to use jQuery UI Datepicker. There are many other javascript calendar solutions, but jQuery is my favorite.

You can display events list by date in a loop, like this:
[php]<?php
$prev_date = 0;

$r = mysql_query(“select EventName, EventDate from MyEvents order by EventDate limit 20”);
for($i=0;$i<mysql_num_rows($r);$i++){
$f = mysql_fetch_array($r);

// display date, if it differs from previous event's date
if($f['EventDate']!=$prev_date){
  echo '<b>'.date('l, F j',$f['EventDate']).'</b><br>';
  $prev_date=$f['EventDate'];
}

echo $f['EventName'].'<br>';

}
?>[/php]

Great, thank you! I’m going to try it out today, and I will let you know how it works for me. I really appreciate the help.

Thank you for the code, it seemed to work at first but I’m running into a very strange error. I’ve been fiddling with it for over an hour, it may be something you just know off the top of your head. I have 3 items in my database right now (1 on halloween this year 2011-10-31, 2 on 2011-12-20). When I use the code that you made I get the following:

Wednesday, December 31
Halloween

Wednesday, December 31
Colton’s Birthday
Test 2nd Event

Of course the date is messed up on the second date, but I can’t seem to figure out why. So I just had it echo $row[‘startdate’] (my event date) before the bolded text would appear. Here is what I got:

2011-10-31
Wednesday, December 31
Halloween

2011-12-20
Wednesday, December 31
Colton’s Birthday
Test 2nd Event

So it is reading the event date properly, but for some reason, after it converts the first date, it won’t do any more. I tried converting the event date to a php string, and got the same results. Please help!

I was assuming you’re storing date in your MySQL database as a timestamp, but if you’re using DATETIME or DATE field types, you need to convert it to timestamp first. There are two ways you can do this:

  1. Convert your date to timestamp in the sql:
select EventName, UNIX_TIMESTAMP(EventDate) as Dt from MyEvents order by Dt limit 20
  1. Use strtotime() php function (no need to change sql in this case):
    [php] if($f[‘EventDate’]!=$prev_date){
    echo ‘’.date(‘l, F j’,strtotime($f[‘EventDate’])).’
    ’;
    $prev_date=$f[‘EventDate’];
    }[/php]

Great! Thank you, it worked fantastically. This was the last major hurdle (besides some logo stuff) I needed to get sorted out! I really appreciate the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service