1

I'm building a calendar and I'm really dumb when it comes to arrays. First of all, I need to query my database, so:


$events = mysql_query ("SELECT id,title,date WHERE date BETWEEN 2009-01-01 AND 2009-01-31")
or die(mysql_error());

So, now I need to order those events so, when I'm echoing my calendar table, I can check this array for events. In this great Calendar written by David Walsh, he does a query for every day, but I suppose it would be a performance nightmare. So...any ideas how can I do this?

3 Answers 3

3

First up, make sure you're comparing dates with dates. I think your code should be (i.e. with single quotes around date):

$events = mysql_query ("SELECT id,title,date WHERE date 
BETWEEN '2009-01-01' AND '2009-01-31' order by date asc")or die(mysql_error());

assuming date is a date column.

Sign up to request clarification or add additional context in comments.

3 Comments

yes, I wrote the sql query without the single quotes, and yes, I'm comparing dates with dates. My problem is the next step: how can I order the array and then print only the events for the day X.
Do you want to output the events for a certain day X or just print all events for day 1, then print all events for day 2,.... ?
The second one. I'm printin a calendar so I need to echo the events for each. What I'm trying to do is avoid making a query for each day.
2

i would do that in a elegant for loop

for($i=0;$i<30;$i++)
{
$events[] =  mysql_fetch_array(mysql_query ("SELECT id,title,date WHERE date BETWEEN 2009-01-01 AND 2009-01-{$i}"));
}

now you have an array do somethingg with it like this

for($i=0;$i<count($events);$i++)
{ 
  echo $events["id"];
}

will return each id for events in that array

5 Comments

but in this case event[0] may content an array with one element (day 1), event[1] contents an array with two elements (day 1 and day 2) and so on... $event[29]contents all days. And you should change the iterator from 1 to at least <= 30 (or 31).
not realy there is an array containing all day from one to 30 so event[0] is day one an soon
and, what if a single day has 5 events?
you have this: BETWEEN 2009-01-01 AND 2009-01-{$i}, so first iteration gives you BETWEEN 2009-01-01 AND 2009-01-0 which gives you nothing. Second iteration gives you BETWEEN 2009-01-01 AND 2009-01-1 which might not even work because it needs to be 01 (as for all numbers < 10). Third iteration gives you BETWEEN 2009-01-01 AND 2009-01-2 and so on... see what I mean? First 1 day, than 2 days.... the next array always contains all days from the previous array. Or am I really that much on the wrong track?
You're right felix, this solution won't work. Did you have a better idea? I'm driving crazy with this..it seem so simple at first look...
1

Ok regarding your comments you can do something like this: Get the events like Davek proposed (note the ORDER BY!)

$events = mysql_fetch_assoc(mysql_query ("SELECT id,title,date WHERE date BETWEEN '2009-01-01' AND '2009-01-31' ORDER BY date asc"));

Then you got the events ordered by the date. To output it you can do this:

$last_date = null;
foreach($event in $events) {
    if($last_date !== $event['date']) {
        echo 'Events for day ' . $event['date'] . ": \n";
        $last_date = $event['date'];
    }

    echo $event['title'] . "\n"

}

Note: This is just a rough sketch, you have to adjust the output of course, but it should give you the right idea.

Output would look like this (in my example):

Events for 2009-01-01: 
Event 1
Event 2
Events for 2009-01-02: 
Event 1
.
.
.

Edit after comment:

You can write your own function:

function get_events($date, $events) {
    $result = array();
    foreach($event in $events) {
        if($event['date'] == $date) {
            $result[] = $event;
        }
    }
    return $result;
}

But this way you search the the complete array over and over again for each day. You can improve it, if you remove the events you already searched for from the $events array. So every time you search in a smaller array. But I would only do this if there is a performance issue.

1 Comment

@Felix: Thanks, you're on the right track. I wonder: is there any way to build a function in order I could call get_events($day,$events), and search the array $events and print the events for that specific $day?? Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.