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; } Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/ Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 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); Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464226 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? Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464227 Share on other sites More sharing options...
Ch0cu3r Posted January 7, 2014 Share Posted January 7, 2014 Try my code again as I had an error in my code when I posted it. Also post the output of print_r($result); Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464228 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 ) ) Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464229 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? Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464230 Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author Share Posted January 7, 2014 Dropping ARRAY_A gives fatal error Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464231 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. Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464234 Share on other sites More sharing options...
Lassie Posted January 7, 2014 Author 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) Link to comment https://forums.phpfreaks.com/topic/285161-get-value-into-array/#findComment-1464237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.