Jump to content

get value into array


Lassie
Go to solution Solved by 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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.