Lassie Posted January 7, 2014 Share Posted January 7, 2014 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; } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 (edited) To add each event_date to to the $events array you'd use foreach( $result as $results ) { $events[] = $results['event_date']; } Alternatively you could just do $events = $wpdb->get_results($query,ARRAY_N); Edited January 7, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author Share Posted January 7, 2014 Hi, thanks This gives me Array ( [0] => [1] => [2] => ) Should I see the values? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 (edited) Try my code again as I had an error in my code when I posted it. Also post the output of print_r($result); Edited January 7, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author Share Posted January 7, 2014 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 ) ) Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author Share Posted January 7, 2014 Ok print_r $events is Array ( [0] => 2014-01-7 ) Array ( [0] => 2014-01-7 [1] => 2014-01-16 ) Array ( [0] => 2014-01-7 [1] => 2014-01-16 [2] => 2014-01-16 ) I have I got too many arrays? Drop ARRAY_A maybe? Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author Share Posted January 7, 2014 Dropping ARRAY_A gives fatal error Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Lassie Posted January 7, 2014 Author Solution Share Posted January 7, 2014 Thank you. Outputs Array([0] => 2014-01-21[1] => 2014-01-7[2] => 2014-01-16[3] => 2014-01-16) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.