William Orellana
William Orellana

Reputation: 127

Getting array from database

I'm using this class to display a calendar: https://www.phpclasses.org/package/8426-PHP-Generate-HTML-to-display-month-event-calendars.html

I need to populate dates from a database like this one:

$calendar->events=array("2014-01-16"=>1,"2014-01-19"=>4,);

I got the following but is not working.

Any help will be appreciated. Thanks

$number = 1;    
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $response[] = array("$row['date']" => $number);
    $number++;
}
$dates = json_encode($response);
$calendar->events=array($dates);

The events are no showing on the calendar, but if I manually type the dates, like below, the events will show on the calendar.

$calendar->events=array("2014-01-16"=>1,"2014-01-19"=>4,);

Upvotes: 0

Views: 51

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

If you are adding the values keyed by the dates, then you need something like...

$number = 1; 
$response = [];   
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $response[$row['date']] = $number++;
}

Then if you want the result encoded, you may need to use...

$calendar=json_encode([ 'events' => $dates]);

Upvotes: 1

Related Questions