Jump to content

get value into array


Lassie

Recommended Posts

I have a sql query  that gets all the events from a specified month and should put the values into an array.print _r shows the values are being pulled from the database.

I can't get this to work.

Any suggestions appreciated.

Code

$events = array();
global$wpdb;
 
echo $month;
echo $year;
 
$query=("SELECT title, DATE_FORMAT(event_date,'%Y-%m-%e')AS event_date FROM sw_appointments  WHERE YEAR(event_date) = $year AND MONTH(event_date) = $month");
 
$result = $wpdb->get_results($query,ARRAY_A);
print_r($result);
foreach( $result as $results ) {
        
        $events=$results->event_date;
        echo $events;
        
 }
    
Link to comment
https://forums.phpfreaks.com/topic/285161-get-value-into-array/
Share on other sites

That outputs

Array ( [0] => Array ( [title] => Viewing [event_date] => 2014-01-7 ) [1] => Array ( [title] => Viewing [event_date] => 2014-01-16 ) [2] => Array ( [title] => Viewing [event_date] => 2014-01-16 ) )

 

then

$query=("SELECT title, DATE_FORMAT(event_date,'%Y-%m-%e')AS event_date FROM sw_appointments  WHERE YEAR(event_date) = $year AND MONTH(event_date) = $month");
 
$results = $wpdb->get_results($query,ARRAY_A);

$events = array();
foreach( $results as $result ) {
        
        $events[] = $result['event_date']; 
}

printf('<pre>%s</pre>', print_r($events, true));

Should work fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.