0

I've got an array like so:

Array (
[0] => stdClass Object ( [ID] => 1 [user_id] => 1 [post_id] => 929,924,875,839,850,720,642,1109,917,935,911,1174,1173,1172,903,892,1189,865,1386 ) 
[1] => stdClass Object ( [ID] => 2 [user_id] => 75 [post_id] => 903,704,477,9,455 ) 
[2] => stdClass Object ( [ID] => 3 [user_id] => 78 [post_id] => 917,911 ) 
[3] => stdClass Object ( [ID] => 4 [user_id] => 80 [post_id] => 903,1109,642 )
)

I wrote a foreach loop to loop through each of the elements:

$apples = $wpdb->get_results("SELECT ID, user_id, post_id FROM table_name" );

foreach ( $apples as $apple ) 
    {
        $user_info = get_userdata($apple->user_id);
        $post_title = get_the_title($apple->post_id);

        echo '<ul><li>' . $user_info->user_login . '</li>';
        echo '<li>' . $post_title . '</li>';
        echo '</ul>';
    }

How do I loop through each of the ID's found in [post_id]? Right now it is only displaying the title of the first digit in [post_id]. I need it to loop through each of them, though. Any ideas?

1
  • Can you post the code for get_userdata asnd get_the_title ? Commented Dec 30, 2012 at 19:10

2 Answers 2

2

Something like:

<?php
...
foreach( explode( ',', $apple->post_id ) as $post_id ) {
// Do your stuff
}

Would do

Sign up to request clarification or add additional context in comments.

Comments

0

You can try

foreach ( $apples as $apple ) {
    $user_info = get_userdata($apple->user_id);
    echo '<h4>' . $user_info->user_login . '<h4>';
    echo "<ul>";
    foreach ( explode(",", $apple->post_id) as $postID ) {
        printf('<li>%s</li>', get_the_title($postID));
    }
    echo '</ul>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.